-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
49 lines (44 loc) · 1.38 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
const JiraCopier = () => {
let urlPrefix =
document.location.href.match(/https?:\/\/[\.\w-]+\//)[0] + "browse/";
let ticketId = document.getElementById("key-val").textContent;
let ticketDescription =
document.getElementById("summary-val").childNodes[0].textContent;
let ticketUrl = urlPrefix + ticketId;
// Create Hyperlink
let jiraLink = document.createElement("a");
document.body.appendChild(jiraLink);
jiraLink.innerHTML = "[ " + ticketId + " ] - " + ticketDescription;
jiraLink.href = ticketUrl;
// Copy to Clipboard
const range = document.createRange();
range.selectNode(jiraLink);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
const successful = document.execCommand("copy");
// Remove the added Hyperlink
document.body.removeChild(jiraLink);
};
// On Extension Button Click
chrome.action.onClicked.addListener((tab) => {
if (!tab.url) return;
if (tab.url && tab.url.includes("chrome://")) return;
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: JiraCopier,
});
});
// On Keyboard Shortcut Pressed
chrome.commands.onCommand.addListener((command, tab) => {
if (!tab.url) return;
if (tab.url && tab.url.includes("chrome://")) return;
switch (command) {
case "copytoclipboard":
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: JiraCopier,
});
break;
}
});