-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontentscript.js
More file actions
260 lines (234 loc) · 7.36 KB
/
contentscript.js
File metadata and controls
260 lines (234 loc) · 7.36 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
(function() {
let chrome = this['chrome'];
function makeStub(cmd, args) {
if (typeof args === 'string') {
args = args.split(/,\s*/);
}
return function() {
const options = { cmd };
args.forEach((arg, ix) => options[arg] = arguments[ix]);
return new Promise((resolve, reject) => chrome.runtime.sendMessage(
options,
function(response) {
if (response.error) {
reject(new Error(response.error));
} else {
resolve(response);
}
}
));
}
}
const openEditor = makeStub('openEditor', 'repo, ref, file, line, column');
const switchBranch = makeStub('switchBranch', 'repo, branch, branchRepo, pr');
function getRepo() {
const repoLink = document.querySelector('div.repohead-details-container>h1>strong[itemprop="name"]>a');
const repoHref = repoLink && repoLink.href;
return repoHref.replace(/^.*github.com\//, '');
}
function handleLinkClick(e) {
const target = e.target;
if (target && !e.altKey && e.ctrlKey && !e.shiftKey) {
const parent = target.parentNode;
if (!/\bfile-info\b/.test(parent.className)) return;
if (!target.title) return;
const repo = getRepo();
e.preventDefault();
e.stopPropagation();
openEditor(repo, getLocationRef(), target.title).then(null, err => flashMessage(err));
}
}
function getLocationRef(url) {
url = url || location.href;
const locRef = /github\.com\/[^/]+\/[^/]+\/(?:raw|blob|commit|tree|pull\/\d+\/files)\/([a-f0-9]+)(:?\/|$|#|\?)/.exec(url);
if (locRef) return locRef[1];
}
function handleDiffTableClick(e) {
let target = e.target;
while (target && target.nodeName !== 'TD') {
target = target.parentElement;
}
if (target && !e.altKey && e.ctrlKey && !e.shiftKey) {
const code = /\bblob-code\b/.test(target.className);
const num = /\bblob-num\b/.test(target.className);
if (!code && !num) return;
let numBefore, numAfter, line;
if (code) {
numAfter = target.previousElementSibling;
numBefore = numAfter.previousElementSibling;
} else {
const prev = target.previousElementSibling;
if (prev) {
numBefore = prev;
numAfter = target;
} else {
numBefore = target;
numAfter = target.nextElementSibling;
}
}
if (numAfter && numAfter.dataset.lineNumber) {
line = numAfter.dataset.lineNumber;
} else if (numBefore && numBefore.dataset.lineNumber) {
line = numBefore.dataset.lineNumber;
}
let file;
let ref = getLocationRef();
const locFile = /github\.com\/[^/]+\/[^/]+\/blob\/([^/]+)\/([^#?]*)/.exec(location.href);
let link;
if (locFile) {
ref = locFile[1];
file = locFile[2];
} else {
try {
link = link || target.parentNode.parentNode.parentNode.parentNode.parentNode.previousElementSibling.querySelector('div.file-info a');
} catch (err) {}
try {
link = link || target.parentNode.parentNode.parentNode.parentNode.parentNode.querySelector('a.file-info');
} catch (err) {}
try {
link = link || target.parentNode.parentNode.parentNode.parentNode.parentNode.querySelector('a[title][href*="/files/"]');
} catch (err) {}
if (!link) return;
file = link.title || link.innerText;
ref = getLocationRef(link.href) || ref;
}
if (!file) return;
const repo = getRepo();
e.preventDefault();
e.stopPropagation();
openEditor(repo, ref, file, line).then(null, err => flashMessage(err));
}
}
function okMessage(response, msg) {
if (response === 'OK') {
response = msg;
}
flashMessage(response);
}
function handleBranchClick(e) {
let target = e.target;
const parent = target.parentNode;
if (/\bcommit-ref\b/.test(parent.className)) {
target = parent;
}
if (target && !e.altKey && e.ctrlKey && !e.shiftKey) {
const repo = getRepo();
let branchRepo = repo;
let pr = undefined;
e.preventDefault();
e.stopPropagation();
let branch = target.innerText.trim();
if (/\bselect-menu-button\b/.test(parent.className) && parent.title && !/:|Switch branches/i.test(parent.title)) {
target = parent;
branch = target.title;
}
if (/:/.test(branch) && target.title) {
let split = target.title.trim().split(':');
branchRepo = split[0];
branch = split[1];
pr = /github\.com\/[^/]+\/[^/]+\/pull\/([^#?]*)/.exec(location);
if (pr) {
pr = pr[1];
}
}
switchBranch(repo, branch, branchRepo, pr).then(response => okMessage(response, `Switched to ${branch}`), err => flashMessage(err));
}
}
function query(...selectors) {
return Array.from(document.querySelectorAll(selectors.join(',')));
}
function attach() {
query(
'span.current-branch>span',
'span.commit-ref>span',
'div.branch-select-menu span.js-select-button',
'button.branch span.js-select-button',
'div.RecentBranches-item a.RecentBranches-item-link',
'a.sha',
'a.branch-name',
'span.sha',
'.branch-select-menu>.select-menu-button',
'.commit-ref>a'
).forEach(function(tag) {
if (tag.dataset.addOpen) return;
tag.dataset.addOpen = true;
tag.addEventListener('click', handleBranchClick);
});
query('a[title]').forEach(function(tag) {
if (tag.dataset.addOpen) return;
tag.dataset.addOpen = true;
tag.addEventListener('click', handleLinkClick);
});
query(
'table.diff-table',
'table.highlight'
).forEach(function(tag) {
if (tag.dataset.addOpen) return;
tag.dataset.addOpen = true;
tag.addEventListener('click', handleDiffTableClick);
});
}
let flashQueue = [];
let flashing = false;
function flashMessage(msg, error) {
flashQueue.push(msg);
showNextFlash();
}
function showNextFlash() {
if (flashing) return;
let message = flashQueue.shift();
if (!message) return;
let error = false;
if (message instanceof Error) {
message = message.message;
error = true;
}
flashing = true;
let div = document.createElement('div');
let style = div.style;
Object.assign(style, {
position: 'fixed',
top: '-50px',
left: '150px',
background: error ? '#822' : '#228',
color: 'white',
boxShadow: '1px 2px 5px rgba(0, 0, 40, 0.2)',
padding: '10px',
transition: 'top 0.5s ease-in',
zIndex: 99999999
});
div.innerHTML = message;
document.body.appendChild(div);
setTimeout(() => {
style.top = '50px';
style.transition= 'top 0.5s ease-out'
}, 1);
setTimeout(() => {
flashing = false;
showNextFlash();
style.top = '-50px';
setTimeout(() => {
div.remove();
}, 500);
}, 3000);
}
if (MutationObserver) {
let observer = new MutationObserver(function(mutations) {
attach();
});
//tiny delay needed for firefox
setTimeout(observe, 100);
function observe() {
if (!document.body) {
return setTimeout(observe, 100);
}
observer.observe(document.body, {
childList: true,
subtree: true
});
setTimeout(() => attach(), 1);
}
} else {
attach();
}
}());