-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-script.js
More file actions
52 lines (45 loc) · 1.88 KB
/
content-script.js
File metadata and controls
52 lines (45 loc) · 1.88 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
const commitUrlRe = /^(?<site>.+)\/(?<type>projects|users)\/(?<project>.+)\/repos\/(?<repo>.+)\/pull-requests\/(?<pr>\d+)\/commits\/(?<commit>[a-f0-9]+)(?:#.*)?$/;
// add a div to hold the message
let commitMessage = document.createElement("div");
commitMessage.className = "commit-message";
commitMessagePre = document.createElement("pre");
commitMessage.appendChild(commitMessagePre);
document.getElementById("pull-requests-container").insertBefore(commitMessage, document.querySelector("div.pull-request-tabs"));
// update the commit message if we are looking at a single commit and not a full diff
function UpdateCommitMessage(url)
{
const match = commitUrlRe.exec(url);
if(match !== null)
{
const site = match.groups.site;
const type = match.groups.type;
const project = match.groups.project;
const repo = match.groups.repo;
const commit = match.groups.commit;
apiCommit = `${site}/rest/api/1.0/${type}/${project}/repos/${repo}/commits/${commit}`;
fetch(apiCommit)
.then(response => response.json())
.then(function(data){
commitMessagePre.innerText = data.message;
});
}
else
{
commitMessagePre.innerText = "";
console.log(url);
}
}
// remember the previous location
var oldHref = document.location.href;
// create an DOM observer to watch for location changes and update the commit
var globalObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (oldHref != document.location.href) {
oldHref = document.location.href;
UpdateCommitMessage(oldHref);
}
});
});
globalObserver.observe(document.querySelector("body"), {childList: true, subtree: true});
// make sure we check once on a fresh load
window.addEventListener("load", () => {UpdateCommitMessage(oldHref);});