|
@@ -0,0 +1,56 @@
|
|
1
|
+/*
|
|
2
|
+ license: The MIT License, Copyright (c) 2016-2017 YUKI "Piro" Hiroshi
|
|
3
|
+ original:
|
|
4
|
+ http://github.com/piroor/webextensions-lib-l10n
|
|
5
|
+*/
|
|
6
|
+
|
|
7
|
+var l10n = {
|
|
8
|
+ updateString(aString) {
|
|
9
|
+ return aString.replace(/__MSG_(.+?)__/g, function(aMatched) {
|
|
10
|
+ var key = aMatched.slice(6, -2);
|
|
11
|
+ return chrome.i18n.getMessage(key);
|
|
12
|
+ });
|
|
13
|
+ },
|
|
14
|
+
|
|
15
|
+ $log(aMessage, ...aArgs) {
|
|
16
|
+ aMessage = 'l10s ' + aMessage;
|
|
17
|
+ if (typeof log === 'function')
|
|
18
|
+ log(aMessage, ...aArgs);
|
|
19
|
+ else
|
|
20
|
+ console.log(aMessage, ...aArgs);
|
|
21
|
+ },
|
|
22
|
+
|
|
23
|
+ updateDocument() {
|
|
24
|
+ var texts = document.evaluate(
|
|
25
|
+ 'descendant::text()[contains(self::text(), "__MSG_")]',
|
|
26
|
+ document,
|
|
27
|
+ null,
|
|
28
|
+ XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
|
|
29
|
+ null
|
|
30
|
+ );
|
|
31
|
+ for (let i = 0, maxi = texts.snapshotLength; i < maxi; i++)
|
|
32
|
+ {
|
|
33
|
+ let text = texts.snapshotItem(i);
|
|
34
|
+ text.nodeValue = this.updateString(text.nodeValue);
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ var attributes = document.evaluate(
|
|
38
|
+ 'descendant::*/attribute::*[contains(., "__MSG_")]',
|
|
39
|
+ document,
|
|
40
|
+ null,
|
|
41
|
+ XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
|
|
42
|
+ null
|
|
43
|
+ );
|
|
44
|
+ for (let i = 0, maxi = attributes.snapshotLength; i < maxi; i++)
|
|
45
|
+ {
|
|
46
|
+ let attribute = attributes.snapshotItem(i);
|
|
47
|
+ this.$log('apply', attribute);
|
|
48
|
+ attribute.value = this.updateString(attribute.value);
|
|
49
|
+ }
|
|
50
|
+ }
|
|
51
|
+};
|
|
52
|
+
|
|
53
|
+document.addEventListener('DOMContentLoaded', function onReady() {
|
|
54
|
+ document.removeEventListener('DOMContentLoaded', onReady);
|
|
55
|
+ l10n.updateDocument();
|
|
56
|
+});
|