-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.tools.js
More file actions
87 lines (67 loc) · 2.35 KB
/
console.tools.js
File metadata and controls
87 lines (67 loc) · 2.35 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
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
(function() {
'use strict';
// 创建构造函数
function ConsoleTools(customConfig) {
// 默认配置
const config = { clean: true, timer: 500, onchange: function() {} };
// 外部自定义配置
this.config = Object.assign({}, config, customConfig);
// 配置参数校验
const { clean, timer, onchange } = this.config;
if (!(typeof clean === 'boolean' &&
typeof timer === 'number' &&
typeof onchange === 'function')) {
throw new Error('配置参数有误,请查阅文档!')
}
}
// 修改原型
ConsoleTools.prototype = {
constructor: ConsoleTools,
opened: function() {
const { clean, timer, onchange } = this.config;
const threshold = 160;
const devtools = {
isOpened: false,
isOpening: false,
orientation: undefined
};
const element = document.createElement('div');
element.setAttribute('id', Date.now());
this.element = element;
// 利用了console打印日志的异步策略
Object.defineProperty(element, 'id', {
get() {
const widthThreshold = window.outerWidth - window.innerWidth > threshold;
const heightThreshold = window.outerHeight - window.innerHeight > threshold;
const orientation = widthThreshold ? 'vertical' : heightThreshold ? 'horizontal' : 'fullscreen';
if (!devtools.isOpening || devtools.orientation !== orientation) {
onchange({ isOpened: true, orientation: orientation });
devtools.orientation = orientation
devtools.isOpening = true;
}
devtools.isOpened = true;
}
});
// 检测开发者工具的状态,当状态改变时触发事件
this.watchTools = setInterval(() => {
devtools.isOpened = false;
console.log(element)
if (clean) console.clear()
if (!devtools.isOpened && devtools.isOpening) {
onchange({ isOpened: false, orientation: undefined });
devtools.isOpening = false;
devtools.orientation = undefined;
}
}, timer);
},
closed: function() {
this.element = null
window.clearInterval(this.watchTools)
}
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = ConsoleTools;
} else {
window.ConsoleTools = ConsoleTools;
}
})();