-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
224 lines (190 loc) · 6.29 KB
/
main.js
File metadata and controls
224 lines (190 loc) · 6.29 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// With help from:
// https://github.com/material-foundation/github-squash-and-merge-pr-descriptions/blob/develop/main.js
let currentURL;
function collapseNewlines(text) {
const result = [];
let prevWasNewline = false;
debugger;
for (const line of text.split('\n')) {
if (line.trim() === '') {
if (!prevWasNewline) {
result.push(line);
}
prevWasNewline = true;
} else {
result.push(line);
prevWasNewline = false;
}
}
return result.join('\n');
}
function process(text) {
text = text.trim() + `\n\nSee the original pull request here: ${currentURL}`;
text = collapseNewlines(text);
return text;
}
// These look like <img width="889" src=...>
function removeImgTags(line) {
if (!line) {
return line;
}
const tagStartInd = line.indexOf('<img');
if (tagStartInd === -1) {
return line;
}
const tagEndInd = line.indexOf('>', tagStartInd);
if (tagEndInd === -1) {
return line;
}
line = (
line.slice(0, tagStartInd).trim() +
' ' +
line.slice(tagEndInd + 1).trim()
).trim();
return removeImgTags(line);
}
// These look like 
// Literally anything can exist inside "title" except for [ and ]. We might not
// even want to bother checking for these ATM. When removing links, replace the
// link with the placeholder specified in []
function removeGifsOrLinks(line, gifsOrLinks) {
const startToken = gifsOrLinks === 'GIFS' ? '![' : '[';
if (!line) {
return line;
}
const startInd = line.indexOf(startToken);
if (startInd === -1) {
return line;
}
// Make sure that the [ doesn't come after a ! if we're just filtering links
if (gifsOrLinks === 'LINKS') {
if (startInd !== 0 && line[startInd - 1] === '!') {
// If we reached here, our line looks like "... ;
if (middleInd === -1) {
return line;
}
const placeholder = line.slice(startInd + 1, middleInd);
const endInd = line.indexOf(')', middleInd);
if (endInd === -1) {
return line;
}
let result = line.slice(0, startInd).trim() + ' ';
if (gifsOrLinks === 'LINKS') {
result += placeholder + ' ';
}
result += line.slice(endInd + 1).trim();
return removeGifsOrLinks(result.trim(), gifsOrLinks);
}
// There's definitely false positives in these cleaning functions (we might
// strip real content), but they're probably pretty contrived examples. The
// biggest thing is probably that we don't special-case code-formatting via ``
// and block-code formatting via ``` ```. It's probably somewhat common that
// people put <img> tags in their code.
function filterRichContent(text, imagesGifs = false, links = false) {
console.log('filtering');
const lines = text.split('\n');
for (let i = 0; i < lines.length; i++) {
if (imagesGifs) {
lines[i] = removeImgTags(lines[i]);
lines[i] = removeGifsOrLinks(lines[i], 'GIFS');
}
if (links) {
lines[i] = removeGifsOrLinks(lines[i], 'LINKS');
}
}
return collapseNewlines(lines.join('\n'));
}
function getMergeMessageField() {
return document.getElementById('merge_message_field');
}
function injectActions() {
const commitForm = document.querySelector('div.commit-form');
const commitFormActions = document.querySelector('div.commit-form-actions');
if (!commitForm || !commitFormActions) {
return;
}
const flippyContainerFound = document.querySelector('div.flippyContainer');
if (flippyContainerFound) {
return;
}
const container = document.createElement('div');
container.className = 'flippyContainer';
function makeButton() {
const button = document.createElement('button');
button.type = 'button';
button.className = 'btn flippyButton';
return button;
}
const imagesButton = makeButton();
imagesButton.appendChild(document.createTextNode('Remove Images/GIFs'));
const linksButton = makeButton();
linksButton.appendChild(document.createTextNode('Detach Links/Attachments'));
const flippyLink = document.createElement('a');
// TODO: add appropriate link
flippyLink.href = 'https://google.com';
flippyLink.target = '_blank';
const flippy = document.createElement('img');
flippyLink.appendChild(flippy);
flippy.src = chrome.runtime.getURL('./figtree.png');
flippy.width = 34;
imagesButton.addEventListener('click', () => {
const field = getMergeMessageField();
field.value = filterRichContent(field.value, true, false);
});
linksButton.addEventListener('click', () => {
const field = getMergeMessageField();
field.value = filterRichContent(field.value, false, true);
});
container.appendChild(imagesButton);
container.appendChild(linksButton);
container.appendChild(flippyLink);
commitForm.insertBefore(container, commitFormActions);
}
function updateMergeMessage() {
const mergeMessageField = getMergeMessageField();
const prText = document.getElementsByClassName('comment-form-textarea')[0]
.value;
mergeMessageField.value = process(prText);
}
// On PR pages, finds a "Squash and Merge" button and adds a click event
// listener that replaces the commit description with the PR description.
function scanForSquashAndMergeButtons() {
currentURL = 'https://' + document.location.host + document.location.pathname;
if (document.location.href.indexOf('/pull/') === -1) {
return;
}
const squashButton = document.querySelector(
'button.btn-group-squash[data-details-container=".js-merge-pr"]'
);
if (!squashButton || squashButton.getAttribute('flippyRegistered')) {
return;
}
console.log('registering');
squashButton.addEventListener('click', function () {
// GitHub populates the commit message with a bit of a delay, so we also
// need to delay our population of the message.
setTimeout(() => {
injectActions();
updateMergeMessage();
}, 50);
});
squashButton.setAttribute('flippyRegistered', true);
}
// If currently in the "confirm merge" flow
function scanForCommitForm() {
const commitForm = document.querySelector('div.commit-form');
if (!commitForm) {
return;
}
injectActions();
}
setInterval(scanForSquashAndMergeButtons, 800);
setInterval(scanForCommitForm, 800);