-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
181 lines (159 loc) · 6.27 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Copyright (c) 2024 George Chakhidze
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use strict';
const TABBARD_CONTEXT_MENU_ITEM_ID = 'c49bb168-99b0-479f-b560-30c6b4cc067a';
const TABBARD_CONTEXT_MENU_ITEM_COPY_ALL = 'Copy all tab URLs';
const TABBARD_CONTEXT_MENU_ITEM_COPY_SELECTED = 'Copy selected tab URLs';
let copyText = [];
chrome.action.onClicked.addListener((tab) => {
start();
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === TABBARD_CONTEXT_MENU_ITEM_ID) {
start(tab);
}
});
/**
* Event handler for the extension tool bar icon.
* @param {Object} tab - Unused. Currently focused tab.
*/
async function start(tab) {
copyText = [];
await chrome.storage.local.get(['copyOnlyCurrentWindow'], async function (items) {
let copyOnlyCurrentWindow = items ? items.copyOnlyCurrentWindow : false;
if (copyOnlyCurrentWindow) {
await chrome.windows.getCurrent({
'populate': true,
'windowTypes': ['normal', 'popup']
}, async function (win) {
await getAllTabs([win]);
})
} else {
await chrome.windows.getAll({
'populate': true,
'windowTypes': ['normal', 'popup']
}, getAllTabs);
}
});
}
/**
* Determines whether there exist additional highlighted tabs except the active ones.
* @param {Object[]} windows - The array of windows.
*/
function isAnyTabHighlighted(windows) {
const numWindows = windows.length;
for (let i = 0; i < numWindows; i++) {
const win = windows[i];
const numTabs = win.tabs.length;
for (let j = 0; j < numTabs; j++) {
const tab = win.tabs[j];
if (!tab.active && tab.highlighted) {
return true;
}
}
}
return false;
}
/**
* Copies URLs of specified windows.
* @param {Object[]} windows - The array of browser windows.
*/
async function getAllTabs(windows) {
await chrome.storage.local.get(['dontCopyTitles'], async function (items) {
let dontCopyTitles = items ? items.dontCopyTitles : false;
let isAnyTabHighlightedExceptActiveOnes = isAnyTabHighlighted(windows);
const numWindows = windows.length;
for (let i = 0; i < numWindows; i++) {
const win = windows[i];
const numTabs = win.tabs.length;
for (let j = 0; j < numTabs; j++) {
const tab = win.tabs[j];
if (isAnyTabHighlightedExceptActiveOnes && !tab.highlighted) {
continue;
}
if (dontCopyTitles) {
copyText.push(tab.url + '\r\n');
} else {
copyText.push(tab.title + '\r\n' + tab.url + '\r\n\r\n');
}
}
}
if (copyText) {
if (copyText.length > 0) {
await addToClipboard(copyText.join(''))
}
}
});
}
// Detect Chromium-based browser. This is an unreliable hack.
// Chromium does not support 'browser' namespace/object and there is no chrome.runtime.getBrowserInfo() function
let isChromium = typeof browser === 'undefined';
let contexts = ['tab', 'page']; // Firefox can put menu item inside tab system menu
if (isChromium) {
// Chrome does not support 'tab' context menu, — fallback to 'page':
contexts = ['page'];
}
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: TABBARD_CONTEXT_MENU_ITEM_ID,
enabled: true,
type: 'normal',
title: TABBARD_CONTEXT_MENU_ITEM_COPY_ALL,
contexts: contexts
});
});
if (!isChromium) {
chrome.tabs.onHighlighted.addListener(function (highlightInfo) {
if (highlightInfo.tabIds) {
if (highlightInfo.tabIds.length > 1) {
chrome.contextMenus.update(TABBARD_CONTEXT_MENU_ITEM_ID, {
title: TABBARD_CONTEXT_MENU_ITEM_COPY_SELECTED
});
chrome.action.setTitle({ title: TABBARD_CONTEXT_MENU_ITEM_COPY_SELECTED });
} else {
chrome.contextMenus.update(TABBARD_CONTEXT_MENU_ITEM_ID, {
title: TABBARD_CONTEXT_MENU_ITEM_COPY_ALL
});
chrome.action.setTitle({ title: TABBARD_CONTEXT_MENU_ITEM_COPY_ALL });
}
}
});
}
// Solution 1 - As of Aug 2024, service workers cannot directly interact with
// the system clipboard using either `navigator.clipboard` or
// `document.execCommand()`. To work around this, we'll create an offscreen
// document and pass it the data we want to write to the clipboard.
async function addToClipboard(value) {
await chrome.offscreen.createDocument({
url: 'offscreen.html',
reasons: [chrome.offscreen.Reason.CLIPBOARD],
justification: 'Write text to the clipboard.'
});
chrome.runtime.sendMessage({
type: 'copy-data-to-clipboard',
target: 'offscreen-doc',
data: value
});
}
// Solution 2 – Once extension service workers can use the Clipboard API,
// replace the offscreen document based implementation with something like this.
// eslint-disable-next-line no-unused-vars -- This is an alternative implementation
async function addToClipboardV2(value) {
navigator.clipboard.writeText(value);
}