2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const template = {};
|
2015-03-27 10:30:07 +00:00
|
|
|
tDocLoader();
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2012-04-16 01:56:12 +00:00
|
|
|
function t(key, params) {
|
2017-03-26 02:30:59 +00:00
|
|
|
const s = chrome.i18n.getMessage(key, params);
|
|
|
|
if (s == '') {
|
|
|
|
throw `Missing string "${key}"`;
|
|
|
|
}
|
|
|
|
return s;
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
|
2012-04-16 01:56:12 +00:00
|
|
|
function tE(id, key, attr, esc) {
|
2017-03-26 02:30:59 +00:00
|
|
|
if (attr) {
|
|
|
|
document.getElementById(id).setAttribute(attr, t(key));
|
|
|
|
} else if (typeof esc == 'undefined' || esc) {
|
|
|
|
document.getElementById(id).appendChild(document.createTextNode(t(key)));
|
|
|
|
} else {
|
|
|
|
document.getElementById(id).innerHTML = t(key);
|
|
|
|
}
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
2015-03-27 10:30:07 +00:00
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2015-03-27 10:30:07 +00:00
|
|
|
function tHTML(html) {
|
2017-03-26 02:30:59 +00:00
|
|
|
const node = document.createElement('div');
|
|
|
|
node.innerHTML = html.replace(/>\s+</g, '><'); // spaces are removed; use for an explicit space
|
|
|
|
if (html.includes('i18n-')) {
|
2017-04-09 06:43:51 +00:00
|
|
|
tNodeList(node.getElementsByTagName('*'));
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
return node.firstElementChild;
|
2015-03-27 10:30:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2015-03-27 10:30:07 +00:00
|
|
|
function tNodeList(nodes) {
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const node of [...nodes]) {
|
|
|
|
// skip non-ELEMENT_NODE
|
|
|
|
if (node.nodeType != 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (node.localName == 'template') {
|
2017-04-07 12:24:55 +00:00
|
|
|
// compress inter-tag whitespace to reduce number of DOM nodes by 25%
|
|
|
|
template[node.dataset.id] = tHTML(node.innerHTML);
|
2017-03-26 02:30:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (const attr of [...node.attributes]) {
|
|
|
|
let name = attr.nodeName;
|
|
|
|
if (name.indexOf('i18n-') != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
name = name.substr(5); // 'i18n-'.length
|
|
|
|
const value = t(attr.value);
|
|
|
|
switch (name) {
|
|
|
|
case 'text':
|
|
|
|
node.insertBefore(document.createTextNode(value), node.firstChild);
|
|
|
|
break;
|
2017-04-10 06:47:09 +00:00
|
|
|
case 'text-append':
|
|
|
|
node.appendChild(document.createTextNode(value));
|
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
case 'html':
|
|
|
|
node.insertAdjacentHTML('afterbegin', value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
node.setAttribute(name, value);
|
|
|
|
}
|
|
|
|
node.removeAttribute(attr.nodeName);
|
|
|
|
}
|
|
|
|
}
|
2015-03-27 10:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
function tDocLoader() {
|
|
|
|
// localize HEAD
|
2017-04-09 06:43:51 +00:00
|
|
|
tNodeList(document.getElementsByTagName('*'));
|
2017-03-23 17:49:23 +00:00
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
// localize BODY
|
2017-04-09 05:10:25 +00:00
|
|
|
const process = mutations => {
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const mutation of mutations) {
|
|
|
|
tNodeList(mutation.addedNodes);
|
|
|
|
}
|
2017-04-09 05:10:25 +00:00
|
|
|
};
|
|
|
|
const observer = new MutationObserver(process);
|
2017-03-26 02:30:59 +00:00
|
|
|
const onLoad = () => {
|
|
|
|
tDocLoader.stop();
|
2017-04-09 05:10:25 +00:00
|
|
|
process(observer.takeRecords());
|
2017-03-26 02:30:59 +00:00
|
|
|
};
|
|
|
|
tDocLoader.start = () => {
|
|
|
|
observer.observe(document, {subtree: true, childList: true});
|
|
|
|
};
|
|
|
|
tDocLoader.stop = () => {
|
|
|
|
observer.disconnect();
|
|
|
|
document.removeEventListener('DOMContentLoaded', onLoad);
|
|
|
|
};
|
|
|
|
tDocLoader.start();
|
|
|
|
document.addEventListener('DOMContentLoaded', onLoad);
|
2015-03-27 10:30:07 +00:00
|
|
|
}
|