-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
79 lines (65 loc) · 2.33 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Initialize a counter for clicks
let clickCount = 0;
let singleClickTimer = null;
// This event is fired when the extension icon is clicked
chrome.action.onClicked.addListener((tab) => {
// Increment the click counter
clickCount++;
// If this is the first click, start a timer
// If there's no second click within 300ms, execute the single click function
if (clickCount === 1) {
singleClickTimer = setTimeout(() => {
// Reset click counter
clickCount = 0;
// Perform the single click action
singleClickAction(tab);
}, 300);
} else if (clickCount === 2) { // If there was a second click
// Cancel the timer for single click action
clearTimeout(singleClickTimer);
// Reset click counter
clickCount = 0;
// Perform the double click action
doubleClickAction(tab);
}
});
// This function is executed when the extension icon is clicked once
function singleClickAction(tab) {
// Decode the current tab's URL
let url = decodeURIComponent(tab.url);
// Execute the browser's copy command
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: copyToClipboard,
args: [url],
});
// Change the extension icon to indicate success
chrome.action.setIcon({ path: "icons/icon_48_green.png", tabId: tab.id });
// After 1 second, change the icon back to the original
setTimeout(() => {
chrome.action.setIcon({ path: "icons/icon_48.png", tabId: tab.id });
}, 1000);
}
// This function is executed when the extension icon is double-clicked
function doubleClickAction(tab) {
// Get all tabs in the current window
chrome.tabs.query({currentWindow: true}, function(tabs) {
// Create a newline-separated string of all decoded URLs
let urls = tabs.map(tab => decodeURIComponent(tab.url)).join('\n');
// Execute the browser's copy command
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: copyToClipboard,
args: [urls],
});
// Change the extension icon to indicate success
chrome.action.setIcon({ path: "icons/icon_48_green_all.png", tabId: tab.id });
// After 1 second, change the icon back to the original
setTimeout(() => {
chrome.action.setIcon({ path: "icons/icon_48.png", tabId: tab.id });
}, 1000);
});
}
function copyToClipboard(text) {
navigator.clipboard.writeText(text);
}