-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontentScript.js
More file actions
59 lines (52 loc) · 1.94 KB
/
Copy pathcontentScript.js
File metadata and controls
59 lines (52 loc) · 1.94 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
/**
* Updates the PR Merge message on page load
*/
/**
* Gets the Merge Button element
* @returns {HTMLButtonElement | undefined} Merge Button Element
*/
function getMergeButton() {
const collection = document.getElementsByClassName('merge-button');
if (collection && collection.length === 1 && collection[0].nodeName === 'BUTTON') {
return collection[0];
}
}
// find the Merge button and listen to its click event to update the merge message
const mergeButton = getMergeButton();
if (mergeButton) {
mergeButton.onclick = async function () {
const pr = await getPR();
updateMergeMessage(pr.id, pr.title, pr.description);
};
}
/**
* Content scripts are isolated from the underlying page, so we have to use the DOM or postMessage in order to
* communicate between the two, you can't just read variables (like `require`) directly from the page.
*
* So to extract the PR information, we inject a script into the page that puts the PR info in the body's dataset.
* Then we can read it from the content script and clean it all up.
*/
async function getPR() {
const scriptContent = `
(() => {
require(["@atlassian/clientside-extensions-registry"], function(b) {
b.registerExtension("pr-ettier-merge-message", (a, data) => {
document.body.dataset.pr = JSON.stringify(data.pullRequest);
}, {
location: "bitbucket.ui.pullrequest.overview.summary",
});
});
})();
`;
const script = document.createElement('script');
script.appendChild(document.createTextNode(scriptContent));
document.body.appendChild(script);
return new Promise((resolve) => {
setTimeout(() => {
const pr = JSON.parse(document.body.dataset.pr);
document.body.dataset.pr = undefined;
document.body.removeChild(script);
resolve(pr);
}, 100);
});
}