-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_zero_width_clipboard_data.user.js
More file actions
57 lines (50 loc) · 2 KB
/
detect_zero_width_clipboard_data.user.js
File metadata and controls
57 lines (50 loc) · 2 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
// ==UserScript==
// @name Detect Zero-Width Characters When Copying Text
// @namespace http://tampermonkey.net/
// @version 0.2
// @description This script alerts you if you copy a zero-width character and allows you to remove them to prevent being fingerprinted.
// @author Raymond Chee
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Your code here...
var zeroWidthChars = /[\u200B-\u200D\uFEFF]/;
function containsZeroWidth(s) {
return zeroWidthChars.test(s);
}
function replaceZeroWidth(s) {
return s.split(zeroWidthChars).join('•');
}
function removeZeroWidth(s) {
return s.split(zeroWidthChars).join('');
}
function handleCopyEvent(e) {
var selection = window.getSelection();
var data = selection.toString();
if (containsZeroWidth(data)) {
var msg =
`Your ${e.type === "copy" ? "copied" : "cut"} selection contains zero-width characters! Here are the locations of those characters:
${replaceZeroWidth(data)}
Do you want to remove them? Press OK to remove the zero-width characters, or press Cancel to keep them.`;
if (window.confirm(msg)) {
e.preventDefault();
e.clipboardData.setData('text/plain', removeZeroWidth(data));
}
}
}
function handlePasteEvent(e) {
var data = e.clipboardData.getData('text');
if (containsZeroWidth(data)) {
var msg = `Your pasted text contains zero-width characters! Here are the locations of those characters:
${replaceZeroWidth(data)}
If you want to fix it, here is your pasted text without the zero-width characters (you will have to copy and paste it again):
${removeZeroWidth(data)}`;
window.alert(msg);
}
}
document.addEventListener('copy', handleCopyEvent);
document.addEventListener('cut', handleCopyEvent);
document.addEventListener('paste', handlePasteEvent);
})();