-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchdog.js
215 lines (196 loc) · 6.63 KB
/
watchdog.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const domStyle = {
position: "fixed !important",
top: "0 !important",
right: "0 !important",
bottom: "0 !important",
left: " 0 !important",
width: "100vw !important",
height: "100vh !important",
"z-index": "10000 !important",
"pointer-events": "none !important",
"background-color": " transparent !important",
opacity: "1 !important",
"-webkit-transform": "none !important",
"-moz-transform": "none !important",
"-ms-transform": "none !important",
transform: "none !important",
visibility: "visible !important",
display: "block !important",
fill: "rgba(0,0,0,0.1) !important",
clip: "auto !important",
filter: "none !important",
zoom: "1 !important",
"font-size": "14px !important",
"user-select": "none !important"
}
const dealStyle = function(obj) {
let str = ""
for (const key in obj) {
const value = obj[key]
str += `${key}: ${value};`
}
return str
}
//
// canvas方法
//
// pointer-events应用于普通dom时不兼容ie10 ,放弃canvas方法,将来放弃ie10的时候可以启用该方案
// const clearCanvas = () => {
// let cas = document.querySelectorAll("body > canvas")
// for (let i = 0; i < cas.length; i++) {
// cas[i].remove()
// }
// }
// const draw = (ct, waterMark) => {
// const timestamp = formatDate(new Date().getTime())
// const w = 350
// const h = 200
// ct.rotate((-20 * Math.PI) / 180)
// ct.translate(-250, 0)
// ct.font = "italic 20px Arial"
// ct.fillStyle = "rgba(0,0,0,0.15)"
// for (let i = 0; i < 2000; i += w) {
// for (let j = 0; j < 2000; j += h) {
// ct.fillText(waterMark, i, j)
// ct.fillText(timestamp, i, j + 40)
// }
// }
// }
// const creatCanvas = (waterMark) => {
// let canvas = document.createElement("canvas")
// canvas.width = window.innerWidth
// canvas.height = window.innerHeight
// canvas.setAttribute("style", domStyle)
// let ct = canvas.getContext("2d")
// draw(ct, waterMark)
// clearCanvas()
// document.body.appendChild(canvas)
// }
//
// svg方法
//
export const clearSvg = () => {
const _svg = document.querySelector("#watchdogSvg")
if (_svg) {
_svg.removeEventListener("DOMNodeRemoved", dealDebug)
document.body.removeChild(_svg)
}
}
let message = ""
const creatSvg = () => {
clearSvg()
const _svg = document.createElementNS("http://www.w3.org/2000/svg", "svg")
const elT = document.createElementNS("http://www.w3.org/2000/svg", "text")
_svg.setAttribute("style", dealStyle(domStyle))
_svg.setAttribute("id", "watchdogSvg")
const w = 350
const h = 200
const winW = window.innerWidth
const winH = window.innerHeight
for (let i = 0; i < Math.floor(winW * 1.14); i += w) {
for (let j = 20; j < Math.floor(winW * 1.14); j += h) {
const elSpan = document.createElementNS("http://www.w3.org/2000/svg", "tspan")
const elSpan2 = document.createElementNS("http://www.w3.org/2000/svg", "tspan")
elSpan.setAttribute("x", i)
elSpan.setAttribute("y", j)
elSpan.setAttribute("style", dealStyle(domStyle))
elSpan.textContent = message
elSpan2.setAttribute("x", i)
elSpan2.setAttribute("y", j + 40)
elSpan2.setAttribute(
"style",
dealStyle({
...domStyle,
fill: "rgba(0,0,0,0.01) !important"
})
)
elSpan2.textContent = "北京xxxx科技有限公司"
elT.appendChild(elSpan)
elT.appendChild(elSpan2)
}
}
const textStyle = {
...domStyle,
"-webkit-transform": `rotate(-30deg) translate(-${Math.floor((winH / 5) * 3)}px,0) !important`,
"-moz-transform": `rotate(-30deg) translate(-${Math.floor((winH / 5) * 3)}px,0) !important`,
"-ms-transform": `rotate(-30deg) translate(-${Math.floor((winH / 5) * 3)}px,0) !important`,
transform: `rotate(-30deg) translate(-${Math.floor((winH / 5) * 3)}px,0) !important`
}
elT.setAttribute("x", "0")
elT.setAttribute("y", "0")
elT.setAttribute("transform", `rotate(-30) translate(-${Math.floor((winH / 5) * 3)} 0)`)
elT.setAttribute("style", dealStyle(textStyle))
_svg.appendChild(elT)
document.body.appendChild(_svg)
}
//
// 防调试
//
const dealDebug = () => {
// window.location.href = "/errorPage.html"
draw(message)
}
export const draw = (waterMark) => {
message = waterMark
creatSvg()
// {
// //
// // 禁止打开控制台方式(次要手段,容易破解)
// //
// console.clear()
// // 方法一、 监听宽度方式
// if (window.outerWidth - window.innerWidth > 160 || window.outerHeight - window.innerHeight > 160) {
// // 如果打开控制台导致内外大小不同时,则认为是打开控制台
// dealDebug()
// }
// // 方法二、 chrome、ie
// let element = new Image()
// Object.defineProperty(element, "id", {
// get: function() {
// dealDebug()
// return false
// }
// })
// console.info(element)
// // 方法三、 firefox(只在首次打开控制台有效)
// let str1 = `(function() {
// // 创建一个对象
// let foo1 = /./
// // 将其打印到控制台上,实际上是一个指针
// console.info(foo1)
// // 要在第一次打印完之后再重写toString方法
// foo1.toString = function() {
// dealDebug()
// return false
// }
// })()`
// eval(str1)
// // 方法四、 debug陷阱(防止加载页面前打开控制台)
// let str2 = `(function() {
// var startTime = new Date()
// debugger
// if (new Date() - startTime > 10) {
// dealDebug()
// }
// })()`
// eval(str2)
// }
//
// 禁止修改dom(主要手段)
//
const watchdogSvg = document.querySelector("#watchdogSvg")
// 方法一、mutation event方法
watchdogSvg.addEventListener("DOMNodeRemoved", dealDebug)
// 方法二、mutationObserver方法
const config = {
childList: true,
attributes: true,
characterData: true,
subtree: true
}
const callback = function() {
dealDebug()
}
const observer = new MutationObserver(callback)
observer.observe(watchdogSvg, config)
}