-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentscript.js
More file actions
232 lines (202 loc) · 8.66 KB
/
contentscript.js
File metadata and controls
232 lines (202 loc) · 8.66 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
function sourceCode() {
const TODAY_DATE = new Date();
let prData = {};
let tableObserver;
const repeatStr = (str, count) => Array(count).fill().map(() => str).join(' ');
const getIndicatorHtml = (count) => `
<div title="Created ${count} days ago"
class="bolt-table-two-line-cell-item flex-row scroll-hidden"
style="margin-top: -7px; margin-bottom: -10px;">
<div class="secondary-text body-s text-ellipsis">
${count === 0
? `<span style="font-size: 6px; color: #00fd00cc;">${repeatStr('⬤', 1)}</span>`
: `<span style="font-size: 6px; color: #abcde6;">${repeatStr('⬤', count)}</span>`
}
</div>
</div>
`;
const getPrNum = el => el.getAttribute('href').match(/[0-9]+$/)[0];
function convertToNode(strHTML) {
const temp = document.createElement('template');
temp.innerHTML = strHTML;
return temp.content.firstElementChild;
}
function getContainersDataFromDom() {
const containers = document.querySelectorAll(
'table.repos-pr-list > tbody > a > td:nth-of-type(3) > .bolt-table-cell-content'
);
const res = [];
for (let cont of containers) {
const prRow = cont.parentElement.parentElement;
const prNum = getPrNum(prRow);
res.push({
container: cont,
prNum,
});
}
return res;
}
function getPrCreationDate(prNum) {
const prCreationDateInSecs = prData[prNum].creationDate.match(/Date\(([0-9]+)\)/)[1];
return new Date(+prCreationDateInSecs);
}
function getDiffDays(laterDate, earlierDate) {
const oneDay = 24 * 60 * 60 * 1000; // hours * minutes * seconds * milliseconds
return Math.round(Math.abs((laterDate - earlierDate) / oneDay));
}
function getStaticPrsData() {
const scriptTag = document.querySelectorAll('#dataProviders')[0].childNodes[0];
const json = JSON.parse(scriptTag.textContent);
const staticPrs = json?.data?.["ms.vss-code-web.prs-list-data-provider"]?.pullRequests;
return staticPrs ? staticPrs : {};
}
function renderIndicator(container, prNum) {
const prCreationDate = getPrCreationDate(prNum);
const diffDays = getDiffDays(TODAY_DATE, prCreationDate);
container.appendChild(
convertToNode(
getIndicatorHtml(diffDays),
),
);
}
function renderAllIndicators() {
for (let data of getContainersDataFromDom()) {
renderIndicator(data.container, data.prNum);
}
}
function processPRs() {
if (!location.href.includes('/pullrequests?_a=mine')) {
return;
}
if (tableObserver) {
tableObserver.disconnect();
}
renderAllIndicators();
tableObserver = new MutationObserver(function azureImprovObserve(mutationsList) {
if (!location.href.includes('/pullrequests?_a=mine')) {
return;
}
for (let mut of mutationsList) {
const addedNodes = Array.from(mut.addedNodes);
if (!addedNodes.length) {
continue;
}
const node = addedNodes[0];
if (node.classList.contains('repos-pr-list')) {
// "repos-pr-list" is a table that contains all the prs in a section
renderAllIndicators();
} else if (node.getAttribute('href')) {
// handling scrolling
const prNum = getPrNum(node);
const container = node.querySelector('.bolt-table-cell-content.flex-column');
renderIndicator(container, prNum);
}
}
});
const el = document.querySelector('.page-content');
tableObserver.observe(el, { subtree: true, childList: true });
}
function handlePrData(data) {
let prs = data?.fps?.dataProviders?.data["ms.vss-code-web.prs-list-data-provider"]?.pullRequests;
if (prs && Object.keys(prs).length) {
prData = { ...prs, ...prData };
return;
}
prs = data?.dataProviders?.['ms.vss-code-web.prs-list-data-provider']?.pullRequests;
const newPrData = { ...prs, ...getStaticPrsData(), ...prData};
prData = newPrData;
// at this moment we've got all the data and can process and render the indicators
processPRs();
}
function shouldHandlePrData(reqData, response) {
const url = reqData instanceof Response ? reqData.url : reqData;
const contentType = response.headers.get('content-type');
const isUrlMatching = url?.includes('Contribution/HierarchyQuery/project') || url?.includes('pullrequests?__rt=fps&__ver=2');
const isContentTypeMatching = contentType?.includes('application/json');
return isUrlMatching && isContentTypeMatching;
}
oldfetch = window.fetch;
window.fetch = function(...args) {
const prom = oldfetch.call(window, ...args).then((res) => {
if (shouldHandlePrData(args[0], res)) {
const clone = res.clone();
clone.json().then(data => handlePrData(data));
}
return res;
});
return prom;
}
/** Backlog Effort Indicator Implementation */
let effortEl, gridObj;
function monkeyPatchResultGrid() {
const gridDefId = 'WorkItemTracking/Scripts/Controls/Query/QueryResultGrid';
window.LWLS.require([gridDefId], function(obj) {
const resultGridProto = obj.QueryResultGrid.prototype;
const orig = resultGridProto._createFilterManager;
resultGridProto._createFilterManager = function() {
gridObj = this;
return orig.apply(this);
};
});
}
function getFilteredRowsTotalEffort() {
const effortCol = gridObj._pageColumns.findIndex(col => col === 'Microsoft.VSTS.Scheduling.Effort');
const ids = gridObj._filterManager.filter();
const pageData = gridObj._pageData;
return ids.reduce((acc, id) => acc + (+pageData[id][effortCol]), 0);
}
const backlogElObserver = new MutationObserver((mutationsList) => {
if (document.body.contains(effortEl) && !mutationsList.some(mut => mut.target.classList.contains('grid-header'))) {
return;
}
console.count('updating filter header!!')
effortEl = $('div.grid-header > div.grid-header-canvas [aria-label="Effort"] > div.title')[0];
if (effortEl) {
effortEl.textContent = 'Effort = ' + getFilteredRowsTotalEffort();
}
});
const oldLog = console.log;
console.log = function(...args) {
if (typeof args[0] === 'string' && args[0].includes('Sprints.BacklogOpen')) {
const backlogEl = $('.productbacklog-grid-results.grid.backlog.has-header')[0];
backlogElObserver.disconnect();
backlogElObserver.observe(backlogEl, { childList: true, subtree: true });
}
oldLog.apply(console, args);
};
const observer2 = new MutationObserver((mutationsList) => {
const addedNodes = [...mutationsList].map(m => [...m.addedNodes]).flat();
for (const n of addedNodes) {
if (n.className === 'external-hub-shim-container flex absolute-fill') {
monkeyPatchResultGrid();
observer2.disconnect();
return;
}
}
});
observer2.observe(document.documentElement, { childList: true, subtree: true });
}
const observer = new MutationObserver(() => {
if (document.head) {
// inject and run the script
const scriptEl = document.createElement('script');
scriptEl.type = 'text/javascript';
const code = '(' + sourceCode.toString() + ')()';
scriptEl.appendChild(document.createTextNode(code));
document.head.appendChild(scriptEl);
// inject the css rules
const cssRules = `
table.repos-pr-list.bolt-table td > .bolt-table-cell-content {
padding-bottom: 13px;
}
table.repos-pr-list.bolt-table td > .bolt-table-cell-content .bolt-table-two-line-cell-item:nth-of-type(2) {
margin-bottom: 3px;
}
`;
const styleElement = document.createElement('style');
styleElement.appendChild(document.createTextNode(cssRules));
document.head.appendChild(styleElement);
observer.disconnect();
}
});
observer.observe(document.documentElement, { childList: true });