|
| 1 | +(() => { |
| 2 | + const watermarkChars = [ |
| 3 | + "\u200B", |
| 4 | + "\u200C", |
| 5 | + "\u200D", |
| 6 | + "\u2060", |
| 7 | + "\uFEFF", |
| 8 | + "\u202F", |
| 9 | + "\u00A0", |
| 10 | + ]; |
| 11 | + |
| 12 | + const cleanse = (text) => |
| 13 | + watermarkChars.reduce((t, ch) => t.split(ch).join(" "), text); |
| 14 | + |
| 15 | + // sol 1 |
| 16 | + if (navigator.clipboard?.writeText) { |
| 17 | + const origWriteText = navigator.clipboard.writeText.bind( |
| 18 | + navigator.clipboard |
| 19 | + ); |
| 20 | + navigator.clipboard.writeText = async (data) => { |
| 21 | + const cleaned = cleanse(data); |
| 22 | + console.log(`Text watermarks removed.`); |
| 23 | + return origWriteText(cleaned); |
| 24 | + }; |
| 25 | + } |
| 26 | + |
| 27 | + // sol 2 |
| 28 | + if (navigator.clipboard?.write) { |
| 29 | + const origWrite = navigator.clipboard.write.bind(navigator.clipboard); |
| 30 | + navigator.clipboard.write = async (items) => { |
| 31 | + const newItems = await Promise.all( |
| 32 | + items.map(async (item) => { |
| 33 | + const blobs = {}; |
| 34 | + for (const type of item.types) { |
| 35 | + const blob = await item.getType(type); |
| 36 | + 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; |
| 42 | + } |
| 43 | + return new ClipboardItem(blobs); |
| 44 | + }) |
| 45 | + ); |
| 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); |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + // sol 4 |
| 73 | + document.addEventListener( |
| 74 | + "copy", |
| 75 | + (e) => { |
| 76 | + const sel = window.getSelection().toString(); |
| 77 | + const cleaned = cleanse(sel); |
| 78 | + e.clipboardData.setData("text/plain", cleaned); |
| 79 | + e.clipboardData.setData("text/html", cleaned); |
| 80 | + e.preventDefault(); |
| 81 | + console.log(`Text watermarks removed.`); |
| 82 | + }, |
| 83 | + true |
| 84 | + ); |
| 85 | + |
| 86 | + console.log("[T.W.R.] Injected all content scripts!"); |
| 87 | +})(); |
0 commit comments