Skip to content

Commit d423403

Browse files
authored
feat: 사용자의 설정에 따라 특정 도메인에서만 작동하도록 변경
1 parent 27849c8 commit d423403

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

content.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
1-
const script = document.createElement("script");
2-
script.src = chrome.runtime.getURL("inject.js");
3-
script.onload = () => script.remove();
4-
(document.head || document.documentElement).appendChild(script);
1+
(async () => {
2+
const url = location.href;
3+
4+
const toReg = (g) =>
5+
new RegExp(
6+
"^" + g.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*") + "$"
7+
);
8+
9+
const inject = () => {
10+
const s = document.createElement("script");
11+
s.src = chrome.runtime.getURL("inject.js");
12+
(document.head || document.documentElement).append(s);
13+
};
14+
15+
const pref = await chrome.storage.sync.get({
16+
enabled: true,
17+
enableOnThis: false,
18+
domains: [],
19+
});
20+
21+
let should = false;
22+
if (pref.enabled) should = true;
23+
else if (pref.enableOnThis) should = true;
24+
else if (pref.domains.some((d) => toReg(d).test(url))) should = true;
25+
26+
if (should) inject();
27+
})();

inject.js

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,76 +12,61 @@
1212
const cleanse = (text) =>
1313
watermarkChars.reduce((t, ch) => t.split(ch).join(" "), text);
1414

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+
1625
if (navigator.clipboard?.writeText) {
17-
const origWriteText = navigator.clipboard.writeText.bind(
18-
navigator.clipboard
19-
);
26+
const orig = navigator.clipboard.writeText.bind(navigator.clipboard);
2027
navigator.clipboard.writeText = async (data) => {
28+
if (shouldSkip()) return orig(data);
2129
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);
2432
};
2533
}
2634

27-
// sol 2
2835
if (navigator.clipboard?.write) {
29-
const origWrite = navigator.clipboard.write.bind(navigator.clipboard);
36+
const orig = navigator.clipboard.write.bind(navigator.clipboard);
3037
navigator.clipboard.write = async (items) => {
38+
if (shouldSkip()) return orig(items);
3139
const newItems = await Promise.all(
3240
items.map(async (item) => {
3341
const blobs = {};
3442
for (const type of item.types) {
35-
const blob = await item.getType(type);
43+
const b = await item.getType(type);
3644
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;
4248
}
49+
console.log("[T.W.R.] all text watermarks removed");
4350
return new ClipboardItem(blobs);
4451
})
4552
);
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);
6954
};
7055
}
7156

72-
// sol 4
7357
document.addEventListener(
7458
"copy",
7559
(e) => {
60+
if (shouldSkip()) return;
7661
const sel = window.getSelection().toString();
7762
const cleaned = cleanse(sel);
7863
e.clipboardData.setData("text/plain", cleaned);
7964
e.clipboardData.setData("text/html", cleaned);
8065
e.preventDefault();
81-
console.log(`Text watermarks removed.`);
66+
console.log("[T.W.R.] all text watermarks removed");
8267
},
8368
true
8469
);
8570

86-
console.log("[T.W.R.] Injected all content scripts!");
71+
console.log("[T.W.R.] all interceptors installed");
8772
})();

0 commit comments

Comments
 (0)