-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject.js
More file actions
72 lines (65 loc) · 1.96 KB
/
inject.js
File metadata and controls
72 lines (65 loc) · 1.96 KB
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
(() => {
const watermarkChars = [
"\u200B",
"\u200C",
"\u200D",
"\u2060",
"\uFEFF",
"\u202F",
"\u00A0",
];
const cleanse = (text) =>
watermarkChars.reduce((t, ch) => t.split(ch).join(" "), text);
let lastCleanTime = 0;
const THRESHOLD = 50;
const shouldSkip = () => {
const now = Date.now();
if (now - lastCleanTime < THRESHOLD) return true;
lastCleanTime = now;
return false;
};
if (navigator.clipboard?.writeText) {
const orig = navigator.clipboard.writeText.bind(navigator.clipboard);
navigator.clipboard.writeText = async (data) => {
if (shouldSkip()) return orig(data);
const cleaned = cleanse(data);
console.log("[T.W.R.] all text watermarks removed");
return orig(cleaned);
};
}
if (navigator.clipboard?.write) {
const orig = navigator.clipboard.write.bind(navigator.clipboard);
navigator.clipboard.write = async (items) => {
if (shouldSkip()) return orig(items);
const newItems = await Promise.all(
items.map(async (item) => {
const blobs = {};
for (const type of item.types) {
const b = await item.getType(type);
if (type === "text/plain") {
const text = await b.text();
blobs[type] = new Blob([cleanse(text)], { type });
} else blobs[type] = b;
}
console.log("[T.W.R.] all text watermarks removed");
return new ClipboardItem(blobs);
})
);
return orig(newItems);
};
}
document.addEventListener(
"copy",
(e) => {
if (shouldSkip()) return;
const sel = window.getSelection().toString();
const cleaned = cleanse(sel);
e.clipboardData.setData("text/plain", cleaned);
e.clipboardData.setData("text/html", cleaned);
e.preventDefault();
console.log("[T.W.R.] all text watermarks removed");
},
true
);
console.log("[T.W.R.] all interceptors installed");
})();