|
12 | 12 | const cleanse = (text) => |
13 | 13 | watermarkChars.reduce((t, ch) => t.split(ch).join(" "), text); |
14 | 14 |
|
15 | | - // sol 1 |
| 15 | + let lastCleanTime = 0; |
| 16 | + const THRESHOLD = 50; |
| 17 | + |
| 18 | + const shouldSkip = () => { |
| 19 | + const now = Date.now(); |
| 20 | + if (now - lastCleanTime < THRESHOLD) return true; |
| 21 | + lastCleanTime = now; |
| 22 | + return false; |
| 23 | + }; |
| 24 | + |
16 | 25 | if (navigator.clipboard?.writeText) { |
17 | | - const origWriteText = navigator.clipboard.writeText.bind( |
18 | | - navigator.clipboard |
19 | | - ); |
| 26 | + const orig = navigator.clipboard.writeText.bind(navigator.clipboard); |
20 | 27 | navigator.clipboard.writeText = async (data) => { |
| 28 | + if (shouldSkip()) return orig(data); |
21 | 29 | const cleaned = cleanse(data); |
22 | | - console.log(`Text watermarks removed.`); |
23 | | - return origWriteText(cleaned); |
| 30 | + console.log("[T.W.R.] all text watermarks removed"); |
| 31 | + return orig(cleaned); |
24 | 32 | }; |
25 | 33 | } |
26 | 34 |
|
27 | | - // sol 2 |
28 | 35 | if (navigator.clipboard?.write) { |
29 | | - const origWrite = navigator.clipboard.write.bind(navigator.clipboard); |
| 36 | + const orig = navigator.clipboard.write.bind(navigator.clipboard); |
30 | 37 | navigator.clipboard.write = async (items) => { |
| 38 | + if (shouldSkip()) return orig(items); |
31 | 39 | const newItems = await Promise.all( |
32 | 40 | items.map(async (item) => { |
33 | 41 | const blobs = {}; |
34 | 42 | for (const type of item.types) { |
35 | | - const blob = await item.getType(type); |
| 43 | + const b = await item.getType(type); |
36 | 44 | if (type === "text/plain") { |
37 | | - const text = await blob.text(); |
38 | | - const cleaned = cleanse(text); |
39 | | - console.log(`Text watermarks removed.`); |
40 | | - blobs[type] = new Blob([cleaned], { type }); |
41 | | - } else blobs[type] = blob; |
| 45 | + const text = await b.text(); |
| 46 | + blobs[type] = new Blob([cleanse(text)], { type }); |
| 47 | + } else blobs[type] = b; |
42 | 48 | } |
| 49 | + console.log("[T.W.R.] all text watermarks removed"); |
43 | 50 | return new ClipboardItem(blobs); |
44 | 51 | }) |
45 | 52 | ); |
46 | | - return origWrite(newItems); |
47 | | - }; |
48 | | - } |
49 | | - |
50 | | - // sol 3 |
51 | | - { |
52 | | - const origExec = Document.prototype.execCommand; |
53 | | - Document.prototype.execCommand = (cmd, ...args) => { |
54 | | - if (cmd.toLowerCase() === "copy") { |
55 | | - const sel = window.getSelection().toString(); |
56 | | - const cleaned = cleanse(sel); |
57 | | - const ta = document.createElement("textarea"); |
58 | | - ta.value = cleaned; |
59 | | - ta.style.position = "fixed"; |
60 | | - ta.style.opacity = "0"; |
61 | | - document.body.appendChild(ta); |
62 | | - ta.select(); |
63 | | - const result = origExec.call(this, "copy", ...args); |
64 | | - document.body.removeChild(ta); |
65 | | - console.log(`Text watermarks removed.`); |
66 | | - return result; |
67 | | - } |
68 | | - return origExec.call(this, cmd, ...args); |
| 53 | + return orig(newItems); |
69 | 54 | }; |
70 | 55 | } |
71 | 56 |
|
72 | | - // sol 4 |
73 | 57 | document.addEventListener( |
74 | 58 | "copy", |
75 | 59 | (e) => { |
| 60 | + if (shouldSkip()) return; |
76 | 61 | const sel = window.getSelection().toString(); |
77 | 62 | const cleaned = cleanse(sel); |
78 | 63 | e.clipboardData.setData("text/plain", cleaned); |
79 | 64 | e.clipboardData.setData("text/html", cleaned); |
80 | 65 | e.preventDefault(); |
81 | | - console.log(`Text watermarks removed.`); |
| 66 | + console.log("[T.W.R.] all text watermarks removed"); |
82 | 67 | }, |
83 | 68 | true |
84 | 69 | ); |
85 | 70 |
|
86 | | - console.log("[T.W.R.] Injected all content scripts!"); |
| 71 | + console.log("[T.W.R.] all interceptors installed"); |
87 | 72 | })(); |
0 commit comments