forked from ipcjs/bilibili-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipcjs.lib.js
117 lines (115 loc) · 3.73 KB
/
ipcjs.lib.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
; (function () {
const log = console.log.bind(console)
/**
* 创建元素的快捷方法:
* 1. type, props, children
* 2. type, props, innerHTML
* 3. 'text', text
* @param type string, 标签名; 特殊的, 若为text, 则表示创建文字, 对应的t为文字的内容
* @param props object, 属性; 特殊的属性名有: className, 类名; style, 样式, 值为(样式名, 值)形式的object; event, 值为(事件名, 监听函数)形式的object;
* @param children array, 子元素; 也可以直接是html文本;
*/
const util_ui_element_creator = (type, props, children) => {
let elem = null;
if (type === "text") {
return document.createTextNode(props);
} else {
elem = document.createElement(type);
}
for (let n in props) {
if (n === "style") {
for (let x in props.style) {
elem.style[x] = props.style[x];
}
} else if (n === "className") {
elem.className = props[n];
} else if (n === "event") {
for (let x in props.event) {
elem.addEventListener(x, props.event[x]);
}
} else {
elem.setAttribute(n, props[n]);
}
}
if (children) {
if (typeof children === 'string') {
elem.innerHTML = children;
} else {
for (let i = 0; i < children.length; i++) {
if (children[i] != null)
elem.appendChild(children[i]);
}
}
}
return elem;
}
const util_html = function (html) {
let template = util_ui_element_creator('template')
template.innerHTML = html.trim()
return Array.from(template.content.childNodes)
}
class ElePlus {
constructor(ele) {
this.ele = ele;
}
hasClass(name) {
return this.ele.className.includes(name);
}
removeClass(name) {
let list = this.ele.className.split(/ +/);
let index = list.indexOf(name);
if (index != -1) {
list.splice(index, 1);
this.ele.className = list.join(' ');
}
return this;
}
addClass(name) {
this.ele.className = `${this.ele.className} ${name}`;
return this;
}
toggleClass(name) {
this.hasClass(name) ? this.removeClass(name) : this.addClass(name);
return this;
}
on(eventName, listener) {
this.ele.addEventListener(eventName, (...args) => {
listener.apply(this.ele, args);
});
return this;
}
text(value) {
if (value === undefined) {
return this.ele.innerText;
} else {
this.ele.innerText = value;
return this;
}
}
html(value) {
if (value === undefined) {
return this.ele.innerHTML;
} else {
this.ele.innerHTML = value;
return this;
}
}
}
if (window.ipcjs) {
log('已经存在window.ipcjs', window.ipcjs)
} else {
window.ipcjs = {
_: util_ui_element_creator,
html: util_html,
log: log,
$: (ele) => new ElePlus(ele),
installInto: function (target) {
if (typeof target === 'function') {
target.call(null, this)
} else {
Object.assign(target, this)
}
}
}
}
})()