-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-pr-collapse-all-files.user.js
More file actions
76 lines (58 loc) · 2.13 KB
/
git-pr-collapse-all-files.user.js
File metadata and controls
76 lines (58 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// ==UserScript==
// @name PR Collapse All
// @namespace http://tampermonkey.net/
// @version 0.4
// @description Allows you to easily toggle the collapsed state of all
// @author You
// @include /https://git.*/
// @grant none
// https://git.rockfin.com/QLMS/qlms-toggle/pull/1/files
// ==/UserScript==
(function() {
'use strict';
function clickAll(selector) {
document.querySelectorAll(selector).forEach(function(button) {
button.click();
});
}
function createToggleAllButton() {
if(document.getElementById('expand-collapse-files')) { return; }
var button = document.createElement("button");
button.innerHTML = "Collapse All";
button.collapsed = false;
['btn', 'btn-sm', 'js-details-target'].forEach(function(className) {
button.classList.add(className);
});
button.id = 'expand-collapse-files';
button.onclick = function(e) {
button.disabled = true;
e.stopPropagation();
var selector = "button.btn-octicon[aria-expanded='" + !button.collapsed + "']";
clickAll(selector);
button.disabled = false;
button.collapsed = !button.collapsed;
button.innerHTML = button.collapsed ? 'Expand All' : 'Collapse All';
}
return button;
}
function getGitButtonGroup() {
var headerActions = document.querySelector('.gh-header-actions');
return headerActions;
}
function addFunctionalityToPage() {
var toggleAllButton = createToggleAllButton();
var headerActions = document.querySelector('.gh-header-actions');
headerActions && toggleAllButton && headerActions.appendChild(toggleAllButton);
}
function urlChangeListener() {
var currentPage = '';
var prFilesRegex = /https:\/\/git.*\/pull\/.*\/files.*/;
setInterval(function() {
currentPage = window.location.href;
if(currentPage.match(prFilesRegex)) {
addFunctionalityToPage();
}
}, 500);
}
urlChangeListener();
})();