-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
69 lines (55 loc) · 1.48 KB
/
index.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
class Metaset {
constructor(self = window) {
this.window = self;
}
query(selector) {
return this.window.document.head.querySelector(selector)
}
createEl(tag) {
return this.window.document.createElement(tag);
}
append(el) {
return this.window.document.head.appendChild(el);
}
setTitle(text) {
this.window.document.title = text
}
getTitle() {
return this.window.document.title;
}
setMeta(type, key, value) {
let el = this.query(`meta[${type}="${key}"]`);
if (!el) {
el = this.createEl('meta');
this.append(el);
}
el.setAttribute(type, key);
if (value)
el.setAttribute('content', value);
}
getMeta(type, key) {
let el;
if (typeof key === 'undefined') {
el = this.query(`meta[${type}]`);
if (el)
return el.getAttribute(type);
} else {
el = this.query(`meta[${type}="${key}"]`);
if (el)
return el.getAttribute('content');
}
}
setMetaName(name, content) {
this.setMeta('name', name, content);
}
getMetaName(name) {
return this.getMeta('name', name);
}
setMetaProperty(property, content) {
this.setMeta('property', property, content);
}
getMetaProperty(property) {
return this.getMeta('property', property);
}
}
module.exports = Metaset;