-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpastearmor-contentscript.js
83 lines (73 loc) · 2.05 KB
/
pastearmor-contentscript.js
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
73
74
75
76
77
78
79
80
81
82
83
function Warning(innerText, moreStyle)
{
this.element = null
var that = this
this.delete = function() {
that.element.remove()
}
function init()
{
let ele = document.createElement("div")
ele.style = "position: fixed; float: left; left: 0; top: 0; background: orange; color: black; font-family: courier; font-size: 18px; " + moreStyle
ele.innerText = innerText
document.body.appendChild(ele)
that.element = ele
document.addEventListener("mousemove", (e) => onMouseMove(e))
}
function onMouseMove(e)
{
that.element.style.left = (e.clientX + 10) + 'px'
that.element.style.top = (e.clientY - 5) + 'px'
}
init()
}
function PasteArmor(dangers, showTime=2000)
{
this.warning = null
var that = this
var timeout = null
function init()
{
document.addEventListener("copy", (e) => {
let selected = document.getSelection().toString()
let danger = checkText(selected)
let content = `${selected.length} chars `
if (danger)
content += `Malicious ${danger}`
showWarning(content, "background: " + (danger ? "red" : "orange"))
timeout = setTimeout(hideWarning, showTime)
})
}
function checkText(text) // check whether the text is malicious or not and return the type of danger
{
for (i in dangers)
{
if (dangers[i](text))
{
return i
}
}
return null
}
function hideWarning()
{
that.warning.delete()
that.warning = null
}
function showWarning(...args)
{
if (timeout)
clearTimeout(timeout)
if (that.warning)
hideWarning()
that.warning = new Warning(...args)
}
init()
}
dangers = { // danger functions
template: function(text) {
// return true // if something malicious found in this copied text
return false
}
}
pastearmor = new PasteArmor(dangers)