-
Notifications
You must be signed in to change notification settings - Fork 2
/
bg.js
70 lines (61 loc) · 1.69 KB
/
bg.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
'use strict';
/** @type {chrome.runtime.Port} */
let port;
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: 'run',
type: 'normal',
contexts: ['link'],
title: 'Copy link te&xt',
documentUrlPatterns: ['*://*/*', 'file://*/*'],
});
});
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
const tabId = tab.id;
const {frameUrl, frameId} = info;
const isCrossOrigin = frameId &&
new URL(frameUrl).origin !== new URL(tab.url).origin;
if (isCrossOrigin && !await chrome.permissions.request({origins: [frameUrl]}))
return;
const [{message: err, result}] = await chrome.scripting.executeScript({
target: {tabId, frameIds: [frameId]},
injectImmediately: true,
func: findLink,
}).catch(e => [e]);
chrome.action.setTitle({
tabId,
title: result ? '' : `${err}`,
});
if (isCrossOrigin)
chrome.permissions.remove({origins: [frameUrl]});
if (!err) {
if (!port) await initOffscreen();
port.postMessage(result);
}
});
function findLink() {
let el;
let root = document;
while (
(el = root.activeElement) &&
(root = el.shadowRoot || chrome.dom.openOrClosedShadowRoot(el))
) {/*nop*/}
return el.innerText;
}
async function initOffscreen() {
try {
await chrome.offscreen.createDocument({
url: 'offscreen.html',
reasons: ['CLIPBOARD'],
justification: 'Write to clipboard',
});
} catch (err) {
if (!err.message.startsWith('Only a single offscreen')) throw err;
}
port = chrome.runtime.connect();
port.onDisconnect.addListener(onOffscreenClosed);
}
/** Fired if the offscreen document was killed or closed */
function onOffscreenClosed() {
port = null;
}