2016-05-13 01:00:21 -04:00
|
|
|
|
(function () {
|
2016-05-13 03:06:35 -04:00
|
|
|
|
var nodes = document.querySelectorAll('p, li, h1, h2, h3, h4, h5, h6, header, a, button, small');
|
|
|
|
|
_.each(nodes, function (node) {
|
2016-05-13 01:00:21 -04:00
|
|
|
|
if (isTranslationResult(node)) {
|
|
|
|
|
var prevNode = node.previousElementSibling;
|
2016-05-13 03:37:56 -04:00
|
|
|
|
if (prevNode && isPureEnglish(prevNode.innerText) && !prevNode.classList.contains('nav-list-item')) {
|
2016-05-13 03:16:55 -04:00
|
|
|
|
if (location.hostname === 'localhost') {
|
|
|
|
|
prevNode.classList.add('original-english-debug');
|
|
|
|
|
} else {
|
|
|
|
|
prevNode.classList.add('original-english');
|
|
|
|
|
}
|
2016-05-14 08:42:09 -04:00
|
|
|
|
node.title = prevNode.innerText;
|
2016-05-14 08:59:13 -04:00
|
|
|
|
node.setAttribute('id', prevNode.id);
|
|
|
|
|
prevNode.removeAttribute('id');
|
2016-05-14 08:42:09 -04:00
|
|
|
|
node.classList.add('translated');
|
|
|
|
|
node.classList.add('translated-cn');
|
2016-05-13 01:00:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2016-05-13 03:37:56 -04:00
|
|
|
|
function isPureEnglish(text) {
|
2016-05-14 04:20:01 -04:00
|
|
|
|
return /^[\1-\255—’“”ç]*$/.test(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function attributesToString(node) {
|
|
|
|
|
return _.chain(node.attributes)
|
|
|
|
|
.map(function (value) {
|
|
|
|
|
if (value.name === 'id') {
|
|
|
|
|
return '';
|
|
|
|
|
} else {
|
|
|
|
|
return value.name + '=' + value.value;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.sortBy()
|
|
|
|
|
.value()
|
|
|
|
|
.join(';');
|
2016-05-13 01:00:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isClonedNode(node1, node2) {
|
2016-05-14 04:20:01 -04:00
|
|
|
|
return node1.tagName === node2.tagName &&
|
2016-05-14 07:25:47 -04:00
|
|
|
|
attributesToString(node1) === attributesToString(node2);
|
2016-05-13 01:00:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function indexOf(node) {
|
|
|
|
|
var i = 0;
|
|
|
|
|
var aNode = node.parentNode.firstChild;
|
|
|
|
|
while (aNode !== node) {
|
|
|
|
|
++i;
|
|
|
|
|
if (aNode.tagName !== node.tagName) {
|
|
|
|
|
i = 0;
|
|
|
|
|
}
|
|
|
|
|
aNode = aNode.nextElementSibling;
|
|
|
|
|
}
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isTranslationResult(node) {
|
|
|
|
|
var prevNode = node.previousElementSibling;
|
2016-05-13 03:37:56 -04:00
|
|
|
|
return indexOf(node) % 2 === 1 && prevNode && isClonedNode(node, prevNode) && isPureEnglish(prevNode.innerText);
|
2016-05-13 01:00:21 -04:00
|
|
|
|
}
|
|
|
|
|
})();
|