-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
64 lines (51 loc) · 1.79 KB
/
background.js
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
function extractIssueReferenceFromUrl(url) {
url = url.replace('https://github.com/', '');
url = url.replace('/issues', '');
url = url.replace('/pull', '');
const hashIndex = url.indexOf("#");
url = hashIndex < 0 ? url : url.substr(0, hashIndex);
const referenceParts = url.split('/');
const reference = `${referenceParts[0]}/${referenceParts[1]}#${referenceParts[2]}`;
const textArea = document.createElement("textarea");
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '2em';
textArea.style.height = '2em';
textArea.style.padding = 0;
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
textArea.value = reference;
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
}
catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}
function onClickHandler(info, tab) {
if(info.menuItemId === 'GHIssue_repopage'){
extractIssueReferenceFromUrl(info.pageUrl);
}
};
chrome.contextMenus.onClicked.addListener(onClickHandler);
// Set up context menu tree at install time.
chrome.runtime.onInstalled.addListener(function() {
// Create one test item for each context type.
const contexts = ["page", "selection", "link", "editable", "image", "video", "audio"];
const showForPages = ["*://github.com/*/*/issues/*", "*://github.com/*/*/pull/*"];
for (let i = 0; i < contexts.length; i++) {
const context = contexts[i];
const id = chrome.contextMenus.create({
"title": "Copy Issue Reference",
"documentUrlPatterns":showForPages,
"contexts": [context],
"id": "GHIssue_repo" + context
});
}
});