function() {
var _self = this;
var _dictionary;
function getDictionary(callback) {
chrome.extension.sendRequest({id: "getDictionary"}, function(response) {
_dictionary = response; // Store the dictionary for later use аа
callback.apply(_self, arguments);
});
}
function handleText(textNode) {
var replacements = _dictionary.replacements;
var expressions = _dictionary.expressions;
var v = textNode.nodeValue;
var matchFound = false;
var regex, original;
//text replacements
for(original in replacements) {
original_escaped = original;
regex_for_question_mark = /\?/g
regex_for_period = /\./g
original_escaped = original_escaped.replace(regex_for_question_mark, "\\?");
original_escaped = original_escaped.replace(regex_for_period, "\\.");
regex = new RegExp('\\b' + original_escaped + '\\b', "gi");
if (v.match(regex)) {
v = v.replace(regex, replacements[original]);
matchFound = true;
}
}
// regex replacements
for(original in expressions) {
regex = new RegExp(original, "g");
if (v.match(regex)) {
v = v.replace(regex, expressions[original]);
matchFound = true;
}
}
// Меняем только в случае изменения текста
if (matchFound) {
textNode.nodeValue = v;
}
}
function walk(node) {
// не удалять. связан с осн узлом
var child, next;
switch(node.nodeType) {
case 1: // Element
case 9: // Document
case 11: // Document fragment
child = node.firstChild;
while(child) {
next = child.nextSibling;
walk(child);
child = next;
}
break;
case 3: // Text
handleText(node);
break;
}
}
var running = true;
function work() {
running = true;
walk(document.body);
running = false;
}
chrome.extension.sendRequest({id: 'isPaused?'}, function(response) {
var isPaused = response.value;
if(isPaused) {
return;
}
chrome.extension.sendRequest({id: 'getExcluded'}, function (r2) {
var ex = r2.value;
for (x in ex) {
if (window.location.href.indexOf(ex[x]) != -1) {
return;
}
}
getDictionary(function() {
work();
});
});
});
var timeout = null;
document.addEventListener('DOMSubtreeModified', function(){
if (running) {
return;
}
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(work, 500);
}, false);
})();