-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhelper.js
More file actions
58 lines (56 loc) · 1.42 KB
/
Copy pathhelper.js
File metadata and controls
58 lines (56 loc) · 1.42 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
var stor = {
db: browser.storage.local,
load() {return this.db.get()},
async get(id) {
const c = await this.db.get(id)
return c[id]
},
set(k, v) {
return this.db.set({[k]: v})
},
saveTa(o) {
var {title, val, type, id, url, sessionKey} = o;
return this.set(`${sessionKey} ${url} ${id}`, {
time: sessionKey,
type: type,
val: val,
url: url,
last_modified: String((new Date()).getTime())
});
},
delete(id) {return this.db.remove(id)}
}
const xpath = {
e2string(p, node = document.documentElement) {
const ns = null
const r = document.evaluate(p, node, ns, XPathResult.STRING_TYPE, null)
return r.stringValue
}
}
const i18nAuto = {
NS: null,
q: `*[data-i18n-xpath]`,
withNode(e) {
if (e.dataset.i18nXpath) {
const id = xpath.e2string(e.dataset.i18nXpath, e)
let k = id
if (this.NS) k = this.NS + '__' + k
e.textContent = browser.i18n.getMessage(k)
}
else {
console.error(`i18n-unknown: ${e}`)
}
},
run(ns) {
this.NS = ns
for (const e of $all(this.q)) {
this.withNode(e)
}
}
}
function $(q, root = document) {
return root.querySelector(q)
}
function $all(q, root = document) {
return Array.from(root.querySelectorAll(q))
}