2015-07-19 15:27:19 +00:00
|
|
|
var template = {};
|
2015-03-27 10:30:07 +00:00
|
|
|
tDocLoader();
|
|
|
|
|
2012-04-16 01:56:12 +00:00
|
|
|
function t(key, params) {
|
|
|
|
var s = chrome.i18n.getMessage(key, params)
|
|
|
|
if (s == "") {
|
|
|
|
throw "Missing string '" + key + "'.";
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
function o(key) {
|
|
|
|
document.write(t(key));
|
|
|
|
}
|
|
|
|
function tE(id, key, attr, esc) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2015-03-27 10:30:07 +00:00
|
|
|
|
|
|
|
function tHTML(html) {
|
|
|
|
var node = document.createElement("div");
|
|
|
|
node.innerHTML = html.replace(/>\s+</g, '><'); // spaces are removed; use for an explicit space
|
|
|
|
tNodeList(node.querySelectorAll("*"));
|
|
|
|
var child = node.removeChild(node.firstElementChild);
|
|
|
|
node.remove();
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
function tNodeList(nodes) {
|
|
|
|
for (var n = 0; n < nodes.length; n++) {
|
|
|
|
var node = nodes[n];
|
|
|
|
if (node.nodeType != 1) { // not an ELEMENT_NODE
|
|
|
|
continue;
|
2015-07-19 15:27:19 +00:00
|
|
|
}
|
|
|
|
if (node.localName == "template") {
|
|
|
|
tNodeList(node.content.querySelectorAll("*"));
|
|
|
|
template[node.dataset.id] = node.content.firstElementChild;
|
|
|
|
continue;
|
2015-03-27 10:30:07 +00:00
|
|
|
}
|
2015-07-26 02:24:18 +00:00
|
|
|
for (var a = node.attributes.length - 1; a >= 0; a--) {
|
|
|
|
var attr = node.attributes[a];
|
|
|
|
var name = attr.nodeName;
|
2015-03-27 10:30:07 +00:00
|
|
|
if (name.indexOf("i18n-") != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
name = name.substr(5); // "i18n-".length
|
2016-03-08 05:22:28 +00:00
|
|
|
var value = t(attr.value);
|
2015-03-27 10:30:07 +00:00
|
|
|
switch (name) {
|
|
|
|
case "text":
|
|
|
|
node.insertBefore(document.createTextNode(value), node.firstChild);
|
|
|
|
break;
|
2017-03-11 09:11:38 +00:00
|
|
|
case "tooltip": {
|
|
|
|
node.setAttribute('title', value);
|
|
|
|
break;
|
|
|
|
}
|
2015-03-27 10:30:07 +00:00
|
|
|
case "html":
|
|
|
|
node.insertAdjacentHTML("afterbegin", value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
node.setAttribute(name, value);
|
|
|
|
}
|
2016-03-08 05:22:28 +00:00
|
|
|
node.removeAttribute(attr.nodeName);
|
2015-03-27 10:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tDocLoader() {
|
|
|
|
// localize HEAD
|
|
|
|
tNodeList(document.querySelectorAll("*"));
|
|
|
|
|
|
|
|
// localize BODY
|
|
|
|
var observer = new MutationObserver(function(mutations) {
|
|
|
|
for (var m = 0; m < mutations.length; m++) {
|
|
|
|
tNodeList(mutations[m].addedNodes);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
observer.observe(document, {subtree: true, childList: true});
|
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
|
|
observer.disconnect();
|
2015-07-26 02:24:18 +00:00
|
|
|
tNodeList(document.querySelectorAll("*"));
|
2015-03-27 10:30:07 +00:00
|
|
|
});
|
|
|
|
}
|