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-04-17 15:54:39 +00:00
|
|
|
const cache = !params && t.cache[key];
|
|
|
|
const s = cache || chrome.i18n.getMessage(key, params);
|
2017-07-16 18:02:00 +00:00
|
|
|
if (s === '') {
|
2017-03-26 02:30:59 +00:00
|
|
|
throw `Missing string "${key}"`;
|
|
|
|
}
|
2017-04-17 15:54:39 +00:00
|
|
|
if (!params && !cache) {
|
|
|
|
t.cache[key] = s;
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
return s;
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
|
2017-07-19 10:06:02 +00:00
|
|
|
function tHTML(html, tag) {
|
|
|
|
// body is a text node without HTML tags
|
2017-07-19 11:34:31 +00:00
|
|
|
if (typeof html === 'string' && !tag && /<\w+/.test(html) === false) {
|
2017-07-19 10:06:02 +00:00
|
|
|
return document.createTextNode(html);
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-07-19 10:06:02 +00:00
|
|
|
if (typeof html === 'string') {
|
2017-08-18 14:00:06 +00:00
|
|
|
// spaces are removed; use for an explicit space
|
|
|
|
html = html.replace(/>\s+</g, '><').trim();
|
2017-07-19 10:06:02 +00:00
|
|
|
if (tag) {
|
|
|
|
html = `<${tag}>${html}</${tag}>`;
|
|
|
|
}
|
2017-07-22 04:37:13 +00:00
|
|
|
const body = t.DOMParser.parseFromString(html, 'text/html').body;
|
2017-07-19 10:06:02 +00:00
|
|
|
if (html.includes('i18n-')) {
|
2017-07-22 04:37:13 +00:00
|
|
|
tNodeList(body.getElementsByTagName('*'));
|
2017-07-19 10:06:02 +00:00
|
|
|
}
|
2017-08-18 14:00:06 +00:00
|
|
|
// the html string may contain more than one top-level node
|
|
|
|
if (!body.childNodes[1]) {
|
|
|
|
return body.firstChild;
|
2017-07-22 04:37:13 +00:00
|
|
|
}
|
|
|
|
const fragment = document.createDocumentFragment();
|
2017-08-18 14:00:06 +00:00
|
|
|
while (body.firstChild) {
|
|
|
|
fragment.appendChild(body.firstChild);
|
2017-07-22 04:37:13 +00:00
|
|
|
}
|
|
|
|
return fragment;
|
2017-07-19 10:06:02 +00:00
|
|
|
}
|
|
|
|
return html;
|
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-04-17 15:54:39 +00:00
|
|
|
const PREFIX = 'i18n-';
|
2017-12-07 20:21:27 +00:00
|
|
|
|
2017-04-17 15:54:39 +00:00
|
|
|
for (let n = nodes.length; --n >= 0;) {
|
|
|
|
const node = nodes[n];
|
2017-12-07 20:21:27 +00:00
|
|
|
if (node.nodeType !== Node.ELEMENT_NODE) {
|
2017-03-26 02:30:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-07-16 18:02:00 +00:00
|
|
|
if (node.localName === 'template') {
|
2017-12-07 20:21:27 +00:00
|
|
|
createTemplate(node);
|
2017-03-26 02:30:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-04-17 15:54:39 +00:00
|
|
|
for (let a = node.attributes.length; --a >= 0;) {
|
|
|
|
const attr = node.attributes[a];
|
|
|
|
const name = attr.nodeName;
|
|
|
|
if (!name.startsWith(PREFIX)) {
|
2017-03-26 02:30:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-04-17 15:54:39 +00:00
|
|
|
const type = name.substr(PREFIX.length);
|
2017-03-26 02:30:59 +00:00
|
|
|
const value = t(attr.value);
|
2017-12-07 20:21:27 +00:00
|
|
|
let toInsert, before;
|
2017-04-17 15:54:39 +00:00
|
|
|
switch (type) {
|
2017-12-07 20:21:27 +00:00
|
|
|
case 'word-break':
|
|
|
|
// we already know that: hasWordBreak
|
2017-03-26 02:30:59 +00:00
|
|
|
break;
|
2017-12-07 20:21:27 +00:00
|
|
|
case 'text':
|
|
|
|
before = node.firstChild;
|
|
|
|
// fallthrough to text-append
|
2017-04-10 06:47:09 +00:00
|
|
|
case 'text-append':
|
2017-12-07 20:21:27 +00:00
|
|
|
toInsert = createText(value);
|
2017-04-10 06:47:09 +00:00
|
|
|
break;
|
2017-12-07 20:21:27 +00:00
|
|
|
case 'html': {
|
|
|
|
toInsert = createHtml(value);
|
2017-03-26 02:30:59 +00:00
|
|
|
break;
|
2017-12-07 20:21:27 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
default:
|
2017-04-17 15:54:39 +00:00
|
|
|
node.setAttribute(type, value);
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-12-07 20:21:27 +00:00
|
|
|
tDocLoader.pause();
|
|
|
|
if (toInsert) {
|
|
|
|
node.insertBefore(toInsert, before || null);
|
|
|
|
}
|
2017-04-17 15:54:39 +00:00
|
|
|
node.removeAttribute(name);
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-07 20:21:27 +00:00
|
|
|
|
|
|
|
function createTemplate(node) {
|
|
|
|
const elements = node.content.querySelectorAll('*');
|
|
|
|
tNodeList(elements);
|
|
|
|
template[node.dataset.id] = elements[0];
|
|
|
|
// compress inter-tag whitespace to reduce number of DOM nodes by 25%
|
|
|
|
const walker = document.createTreeWalker(elements[0], NodeFilter.SHOW_TEXT);
|
|
|
|
const toRemove = [];
|
|
|
|
while (walker.nextNode()) {
|
|
|
|
const textNode = walker.currentNode;
|
|
|
|
if (!textNode.nodeValue.trim()) {
|
|
|
|
toRemove.push(textNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tDocLoader.pause();
|
|
|
|
toRemove.forEach(el => el.remove());
|
|
|
|
}
|
|
|
|
|
|
|
|
function createText(str) {
|
|
|
|
return document.createTextNode(tWordBreak(str));
|
|
|
|
}
|
|
|
|
|
|
|
|
function createHtml(value) {
|
|
|
|
// <a href=foo>bar</a> are the only recognizable HTML elements
|
|
|
|
const rx = /(?:<a\s([^>]*)>([^<]*)<\/a>)?([^<]*)/gi;
|
|
|
|
const bin = document.createDocumentFragment();
|
|
|
|
for (let m; (m = rx.exec(value)) && m[0];) {
|
|
|
|
const [, linkParams, linkText, nextText] = m;
|
|
|
|
if (linkText) {
|
|
|
|
const href = /\bhref\s*=\s*(\S+)/.exec(linkParams);
|
|
|
|
const a = bin.appendChild(document.createElement('a'));
|
|
|
|
a.href = href && href[1].replace(/^(["'])(.*)\1$/, '$2') || '';
|
|
|
|
a.appendChild(createText(linkText));
|
|
|
|
}
|
|
|
|
if (nextText) {
|
|
|
|
bin.appendChild(createText(nextText));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bin;
|
|
|
|
}
|
2015-03-27 10:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
function tDocLoader() {
|
2017-07-22 04:37:13 +00:00
|
|
|
t.DOMParser = new DOMParser();
|
2017-10-29 17:22:10 +00:00
|
|
|
t.cache = (() => {
|
2017-09-12 12:08:09 +00:00
|
|
|
try {
|
|
|
|
return JSON.parse(localStorage.L10N);
|
|
|
|
} catch (e) {}
|
|
|
|
})() || {};
|
2017-07-11 15:03:35 +00:00
|
|
|
|
|
|
|
// reset L10N cache on UI language change
|
|
|
|
const UIlang = chrome.i18n.getUILanguage();
|
2017-07-16 18:02:00 +00:00
|
|
|
if (t.cache.browserUIlanguage !== UIlang) {
|
2017-07-11 15:03:35 +00:00
|
|
|
t.cache = {browserUIlanguage: UIlang};
|
|
|
|
localStorage.L10N = JSON.stringify(t.cache);
|
|
|
|
}
|
2017-04-17 15:54:39 +00:00
|
|
|
const cacheLength = Object.keys(t.cache).length;
|
2017-07-11 15:03:35 +00:00
|
|
|
|
2017-12-07 20:21:27 +00:00
|
|
|
Object.assign(tDocLoader, {
|
|
|
|
observer: new MutationObserver(process),
|
|
|
|
start() {
|
|
|
|
if (!tDocLoader.observing) {
|
|
|
|
tDocLoader.observing = true;
|
|
|
|
tDocLoader.observer.observe(document, {subtree: true, childList: true});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
stop() {
|
|
|
|
tDocLoader.pause();
|
|
|
|
document.removeEventListener('DOMContentLoaded', onLoad);
|
|
|
|
},
|
|
|
|
pause() {
|
|
|
|
if (tDocLoader.observing) {
|
|
|
|
tDocLoader.observing = false;
|
|
|
|
tDocLoader.observer.disconnect();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-04-09 06:43:51 +00:00
|
|
|
tNodeList(document.getElementsByTagName('*'));
|
2017-12-07 20:21:27 +00:00
|
|
|
tDocLoader.start();
|
|
|
|
document.addEventListener('DOMContentLoaded', onLoad);
|
2017-03-23 17:49:23 +00:00
|
|
|
|
2017-12-07 20:21:27 +00:00
|
|
|
function process(mutations) {
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const mutation of mutations) {
|
|
|
|
tNodeList(mutation.addedNodes);
|
|
|
|
}
|
2017-12-07 20:21:27 +00:00
|
|
|
tDocLoader.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onLoad() {
|
2017-03-26 02:30:59 +00:00
|
|
|
tDocLoader.stop();
|
2017-12-07 20:21:27 +00:00
|
|
|
process(tDocLoader.observer.takeRecords());
|
2017-07-16 18:02:00 +00:00
|
|
|
if (cacheLength !== Object.keys(t.cache).length) {
|
2017-04-17 15:54:39 +00:00
|
|
|
localStorage.L10N = JSON.stringify(t.cache);
|
|
|
|
}
|
2017-12-07 20:21:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function tWordBreak(text) {
|
|
|
|
// adds soft hyphens every 10 characters to ensure the long words break before breaking the layout
|
2017-12-10 06:28:49 +00:00
|
|
|
return text.length <= 10 ? text :
|
|
|
|
text.replace(/[\d\w\u007B-\uFFFF]{10}|[\d\w\u007B-\uFFFF]{5,10}[!-/]|((?!\s)\W){10}/g, '$&\u00AD');
|
2015-03-27 10:30:07 +00:00
|
|
|
}
|