-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub_open_compare_diff.js
More file actions
60 lines (56 loc) · 1.92 KB
/
github_open_compare_diff.js
File metadata and controls
60 lines (56 loc) · 1.92 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
javascript:(() => {
if (!isActiveCompare()) setEvent();
/* ------------------------ 以下関数群 ------------------------ */
function isActiveCompare() {
return document.querySelector('html').dataset.isActiveCompare === 'true';
}
function setEvent() {
document.querySelector('html').addEventListener('click', main);
document.querySelector('html').dataset.isActiveCompare = true
}
function removeEvent() {
document.querySelector('html').removeEventListener('click', main);
document.querySelector('html').dataset.isActiveCompare = false
}
function main(event) {
event.preventDefault();
const el = event.target;
/* コミットのリンクの場合 */
if (isCommitLink(el)) {
/* 選択状態にしたり外したり */
if (el.dataset.isDiffSelected === 'true') {
unselectCommit(el)
} else {
selectCommit(el);
}
const selectedNode = getSelectedNode();
if (selectedNode.length === 2) {
/* 新規タブ開く */
if (isActiveCompare()) window.open(buildCompareUrl(selectedNode[0], selectedNode[1]));
/* リセット */
removeEvent();
selectedNode.forEach(unselectCommit)
}
}
/* 遷移を止めるため */
return false;
}
function getSelectedNode() {
return document.querySelectorAll('a[data-is-diff-selected="true"]');
}
function selectCommit(el) {
el.dataset.isDiffSelected = true;
el.style['background-color'] = '#404040';
}
function unselectCommit(el) {
el.dataset.isDiffSelected = false;
el.style['background-color'] = '';
}
function isCommitLink(el) {
return el.classList.contains('commit-id')
}
function buildCompareUrl(startCommitEl, endCommitEl) {
const repositoryUrl = document.querySelector('.repohead-details-container > h1 > strong > a').href;
return `${repositoryUrl}/compare/${startCommitEl.innerText}...${endCommitEl.innerText}`;
}
})()