-
Notifications
You must be signed in to change notification settings - Fork 2
/
content.js
30 lines (29 loc) · 887 Bytes
/
content.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
'use strict';
{
let el;
let root = document;
const getRoot = chrome.dom?.openOrClosedShadowRoot;
while (
(el = root.activeElement) &&
(root = el.shadowRoot || (getRoot ? getRoot(el) : el.openOrClosedShadowRoot))
) {}
const text = el.innerText;
const {clipboard} = navigator;
if (clipboard) {
clipboard.writeText(text);
} else {
const sel = getSelection();
const {anchorNode, anchorOffset, focusNode, focusOffset} = sel;
const t = document.createElement('textarea');
t.style.cssText = 'all:revert; position:fixed; top: 0; left: 0; visibility:hidden;'
.replace(/;/g, '!important');
t.value = text;
document.body.appendChild(t);
t.focus({preventScroll: true});
t.select();
document.execCommand('copy');
el.focus();
t.remove();
sel.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset);
}
}