forked from dylang/jira-improved
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-script.js
More file actions
146 lines (134 loc) · 4.42 KB
/
content-script.js
File metadata and controls
146 lines (134 loc) · 4.42 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
function print(a, ...b) {
// console.log(`wCustomize:`, a , ...b);
}
let issueKeyToLinksMap = new Map();
function updateTicketWithPullRequestLinks(issueCard, links) {
var issueKey = issueCard.dataset.issueKey;
var ghLinks = issueCard.querySelector('.ghx-pull-requests');
if (!ghLinks) {
ghLinks = document.createElement('div');
issueCard.appendChild(ghLinks);
} else {
ghLinks.innerHTML = '';
}
ghLinks.className = 'ghx-pull-requests';
ghLinks.style.position = 'absolute';
ghLinks.style.width = '28px';
ghLinks.style.bottom = '8px';
ghLinks.style.left = '8px';
for (var link of links) {
var url = link.url.toString();
if (url && url.indexOf('/pull/') > 0 && url.indexOf('github.com/') > 0) {
var a = document.createElement('a');
ghLinks.appendChild(a);
a.href = link.url;
a.target = '_blank';
// stop click propogation to prevent Jira from opening the side panel
a.addEventListener('click', function (ev) {
ev.stopPropagation();
}, true);
var img = document.createElement('img');
a.appendChild(img);
img.title = link.summary;
var icon = link.icon['url16x16'];
if (icon) {
icon = icon.split('/').pop();
} else {
icon = 'gh_link_merged.png';
}
img.src = chrome.extension.getURL(`img/${icon}`);
img.style.width = '22px';
img.style.height = '22px';
}
}
}
function updateTicket(issueCard) {
var issueKey = issueCard.dataset.issueKey;
let entry = issueKeyToLinksMap.get(issueKey);
if (entry) {
if (entry.fetching) {
print(`already fetching ${issueKey}`);
return;
}
if (Date.now() - entry.time < (3/*minutes*/ * 60/*seconds per minute*/ * 1000/*ms per second*/) ) {
print(`cache hit ${issueKey}`);
updateTicketWithPullRequestLinks(issueCard, entry.links);
return;
}
print(`cache hit, but too stale ${issueKey}`);
}
print(`fetching PR links for ${issueKey}`);
issueKeyToLinksMap.set(issueKey, {
time: Date.now(),
links: null,
fetching: true
});
fetch(`https://jira.atl.workiva.net/rest/api/latest/issue/${issueKey}/remotelink`, { credentials: 'include' })
.then(toJson)
.then((jsonList) => jsonList.map((j) => j.object))
.then(function (links) {
print(`freshening cache for ${issueKey}`);
issueKeyToLinksMap.set(issueKey, {
time: Date.now(),
links: links,
fetching: false
});
updateTicketWithPullRequestLinks(issueCard, links);
}).catch((reason) => {
console.error(reason);
});
}
function intersectionChanged(entries, observer) {
entries.forEach(entry => {
if (entry.isIntersecting) {
updateTicket(entry.target);
print(`Intersecting: ${entry.target}`);
}
});
}
let issueMap = new Set();
function observeTickets() {
let issues = document.querySelectorAll('.ghx-issue');
print(issues);
let io = new IntersectionObserver(intersectionChanged);
issues.forEach((issue) => {
var issueKey = issue.dataset.issueKey;
print(`issueKey ${issueKey}`);
if (!issueMap.has(issueKey)) {
issueMap.add(issueKey);
io.observe(issue);
print('observing ',issueKey);
}
});
}
function toJson(response) {
var contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
return response.json();
}
throw new TypeError("Oops, we haven't got JSON!");
}
var _lastMutationTimeout;
function handleMutation() {
if (_lastMutationTimeout) {
return;
}
_lastMutationTimeout = setTimeout(function () {
print('handling mutation');
observeTickets();
_lastMutationTimeout = null;
}, 1000);
}
function init() {
var mo = new MutationObserver(handleMutation);
mo.observe(document.body, { attributes: true, childList: true });
handleMutation();
}
function waitToInit() {
if (document.readyState == 'complete' && document.body) {
setTimeout(init, 200);
} else {
setTimeout(waitToInit, 200);
}
}
waitToInit();