-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
92 lines (79 loc) · 2.6 KB
/
content.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class URLPatternChecker {
static checkURLPattern(url, pattern) {
const urlPattern = new RegExp(pattern);
return urlPattern.test(url);
}
}
class Button {
constructor(text, onClick, parentElementSelector, pattern) {
this.text = text;
this.onClick = onClick;
this.pattern = pattern;
this.parentElementSelector = parentElementSelector;
this.button = document.createElement("button");
this.button.textContent = this.text;
this.button.classList.add("btn-sm", "btn");
this.button.style.marginLeft = "5px";
this.button.addEventListener("click", this.handleButtonClick.bind(this));
}
handleButtonClick() {
this.onClick();
}
appendToContainer() {
const parentElement = document.querySelector(this.parentElementSelector);
if (parentElement) {
parentElement.appendChild(this.button);
}
}
}
class ButtonManager {
constructor(buttons) {
this.buttons = buttons.map((button) => {
const { text, onClick, parentElementSelector, pattern } = button;
return new Button(text, onClick, parentElementSelector, pattern);
});
}
addButtons() {
const currentURL = window.location.href;
this.buttons.forEach((button) => {
if (URLPatternChecker.checkURLPattern(currentURL, button.pattern)) {
button.appendToContainer();
}
});
}
}
const parentElementSelector =
"#files_bucket > diff-file-filter > diff-layout > div.pr-toolbar.js-sticky.js-position-sticky.d-flex > div > div.flex-grow-0.flex-shrink-0.pr-review-tools";
const buttons = [
{
text: "Mark all as viewed",
pattern:
"^https:\\/\\/github\\.com\\/[\\w-]+\\/[\\w-]+\\/pull\\/\\d+\\/files",
onClick: function markAllAsViewed() {
const activeInputs = document.querySelectorAll(
"div div.js-replace-file-header-review.d-flex > form > label > input:not([checked])"
);
activeInputs.forEach((input) => input.click());
},
parentElementSelector: parentElementSelector,
},
{
text: "Mark all as unviewed",
pattern:
"^https:\\/\\/github\\.com\\/[\\w-]+\\/[\\w-]+\\/pull\\/\\d+\\/files",
onClick: function markAllAsUnviewed() {
const inactiveInputs = document.querySelectorAll(
"div div.js-replace-file-header-review.d-flex > form > label > input[checked]"
);
inactiveInputs.forEach((input) => input.click());
},
parentElementSelector: parentElementSelector,
},
];
const buttonManager = new ButtonManager(buttons);
function addButtons() {
buttonManager.addButtons();
}
addButtons();
window.addEventListener("turbo:render", addButtons);
console.log("Github PR helper loaded");