2016-06-19 04:46:50 -04:00
|
|
|
var sourceVisible = localStorage.getItem('source-visible') === 'true';
|
|
|
|
(function ($) {
|
2016-11-26 09:53:33 -05:00
|
|
|
var content = document.querySelector('article');
|
|
|
|
var footer = document.querySelector('.main-footer');
|
2016-06-19 04:46:50 -04:00
|
|
|
|
2017-02-26 05:44:42 -05:00
|
|
|
processContainer(content);
|
2016-11-26 09:53:33 -05:00
|
|
|
processContainer(footer);
|
2016-11-23 22:45:57 -05:00
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
// restore
|
|
|
|
if (content) {
|
2017-02-26 05:44:42 -05:00
|
|
|
// default hidden by css
|
2016-11-24 15:42:38 -05:00
|
|
|
content.style.display = 'block';
|
2016-11-26 09:53:33 -05:00
|
|
|
}
|
|
|
|
footer.style.display = 'block';
|
2016-11-23 22:45:57 -05:00
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
/**
|
|
|
|
* Process container recursively.
|
|
|
|
* @param container
|
|
|
|
*/
|
2017-02-26 05:44:42 -05:00
|
|
|
function processContainer(container) {
|
2017-02-26 08:44:00 -05:00
|
|
|
var nodes = container.querySelectorAll('p,h1,h2,h3,h4,h5,h6,header,a');
|
2017-03-03 02:59:35 -05:00
|
|
|
for(var i in nodes) {
|
|
|
|
(function(node) {
|
|
|
|
if (isTranslation(node.textContent)) {
|
|
|
|
var $translated = $(node);
|
|
|
|
var prevNode = node.previousElementSibling;
|
|
|
|
var $english = $(prevNode);
|
|
|
|
if (isCorrespondingNode(node, prevNode) && !isTranslation(prevNode.textContent)) {
|
|
|
|
$translated.after($english);
|
|
|
|
$translated.addClass('translated');
|
|
|
|
$translated.addClass('translated-cn');
|
|
|
|
$english.addClass('original-english');
|
|
|
|
if (!sourceVisible) {
|
|
|
|
$english.addClass('hidden');
|
|
|
|
}
|
2016-12-08 07:23:09 -05:00
|
|
|
|
2017-03-03 02:59:35 -05:00
|
|
|
$translated.on('click', function (event) {
|
|
|
|
$english.toggleClass('hidden');
|
|
|
|
});
|
|
|
|
}
|
2016-11-26 09:53:33 -05:00
|
|
|
}
|
2017-03-03 02:59:35 -05:00
|
|
|
})(nodes[i]);
|
|
|
|
}
|
2016-11-26 09:53:33 -05:00
|
|
|
}
|
2016-06-19 04:46:50 -04:00
|
|
|
|
2017-02-26 05:44:42 -05:00
|
|
|
function isTranslation(text) {
|
2016-11-26 09:53:33 -05:00
|
|
|
if (text) {
|
|
|
|
text = text.replace('在线例子', '');
|
2017-02-26 05:44:42 -05:00
|
|
|
return /[\u2E80-\u2EFF\u2F00-\u2FDF\u3000-\u303F\u31C0-\u31EF\u3200-\u32FF\u3300-\u33FF\u3400-\u4DBF\u4DC0-\u4DFF\u4E00-\u9FBF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF]/.test(text);
|
2016-06-19 04:46:50 -04:00
|
|
|
}
|
2016-11-26 09:53:33 -05:00
|
|
|
return false;
|
2016-06-19 04:46:50 -04:00
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
}
|
2016-11-24 09:16:23 -05:00
|
|
|
|
2016-11-26 09:53:33 -05:00
|
|
|
function attributesToString(node) {
|
|
|
|
return _.chain(node.attributes)
|
|
|
|
.map(function (value) {
|
|
|
|
if (value.name === 'id') {
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return value.name + '=' + value.value;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.sortBy()
|
|
|
|
.value()
|
|
|
|
.join(';');
|
|
|
|
}
|
|
|
|
|
2017-02-26 05:44:42 -05:00
|
|
|
function isCorrespondingNode(node1, node2) {
|
|
|
|
return node1 && node2 && node1.tagName === node2.tagName &&
|
2016-11-26 09:53:33 -05:00
|
|
|
attributesToString(node1) === attributesToString(node2);
|
|
|
|
}
|
2016-06-19 04:46:50 -04:00
|
|
|
})(angular.element);
|