Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting fixes + .editorconfig #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Source/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[**.js]
indent_style = space
indent_size = 6
2 changes: 1 addition & 1 deletion Source/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
_alreadyQueued = false;
checkForRandomSwap();
}, pollTimeout);

_alreadyQueued = true;
}
}
Expand Down
275 changes: 137 additions & 138 deletions Source/content_script.js
Original file line number Diff line number Diff line change
@@ -1,155 +1,154 @@
(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;
}

}

// Only change the node if there was any actual text change
if (matchFound) {
textNode.nodeValue = v;
}

}

function walk(node) {

// I stole this function from here: - ZW
// And I stole it from ZW - AG
// http://is.gd/mwZp7E

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 node
handleText(node);
break;
}
}



// Flag to prevent multiple triggering of DOMSubtreeModified
// set it to true initially so that the DOMSubtreeModified event
// does not trigger work until the two chrome.extension requests
// have been handled
var running = true;


// Function that calls walk() but makes sure that it only is called once
// the first call has finished. Any changes that we make to the DOM in walk()
// will trigget DOMSubtreeModified, so we handle this by using the running flag
function work() {
// Set running to true to prevent more calls until the first one is done
running = true;

// Go through the DOM
walk(document.body);

// Set running to false to allow additional calls
running = false;
}



chrome.extension.sendRequest({id: 'isPaused?'}, function(response) {
var isPaused = response.value;

// If the extension is paused, no need to try to call getExcluded
if(isPaused) {
return;
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);
});
}

chrome.extension.sendRequest({id: 'getExcluded'}, function (r2) {

var ex = r2.value;
for (x in ex) {
if (window.location.href.indexOf(ex[x]) != -1) {
return;

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) {
if (replacements.hasOwnProperty(original)) {
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;
}
}
}

getDictionary(function() {
work();
// regex replacements
for(original in expressions) {
regex = new RegExp(original, "g");
if (v.match(regex)) {
v = v.replace(regex, expressions[original]);
matchFound = true;
}
}

// Only change the node if there was any actual text change
if (matchFound) {
textNode.nodeValue = v;
}
}

function walk(node) {

// I stole this function from here: - ZW
// And I stole it from ZW - AG
// http://is.gd/mwZp7E

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 node
handleText(node);
break;
}
}



// Flag to prevent multiple triggering of DOMSubtreeModified
// set it to true initially so that the DOMSubtreeModified event
// does not trigger work until the two chrome.extension requests
// have been handled
var running = true;


// Function that calls walk() but makes sure that it only is called once
// the first call has finished. Any changes that we make to the DOM in walk()
// will trigget DOMSubtreeModified, so we handle this by using the running flag
function work() {
// Set running to true to prevent more calls until the first one is done
running = true;

// Go through the DOM
walk(document.body);

// Set running to false to allow additional calls
running = false;
}



chrome.extension.sendRequest({id: 'isPaused?'}, function(response) {
var isPaused = response.value;

// If the extension is paused, no need to try to call getExcluded
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();
});
});
});

});
});




/**
The below solution to handle dynamically added content
is borrowed from http://stackoverflow.com/a/7326468
*/
/**
The below solution to handle dynamically added content
is borrowed from http://stackoverflow.com/a/7326468
*/

// Add a timer to prevent instant triggering on each DOM change
var timeout = null;
// Add a timer to prevent instant triggering on each DOM change
var timeout = null;

// Add an eventlistener for changes to the DOM, e.g. new content has been loaded via AJAX or similar
// Any changes that we do to the DOM will trigger this event, so we need to prevent infinite looping
// by checking the running flag first.
document.addEventListener('DOMSubtreeModified', function(){
if (running) {
return;
}
// Add an eventlistener for changes to the DOM, e.g. new content has been loaded via AJAX or similar
// Any changes that we do to the DOM will trigger this event, so we need to prevent infinite looping
// by checking the running flag first.
document.addEventListener('DOMSubtreeModified', function(){
if (running) {
return;
}

if (timeout) {
clearTimeout(timeout);
}
if (timeout) {
clearTimeout(timeout);
}

timeout = setTimeout(work, 500);
}, false);
timeout = setTimeout(work, 500);
}, false);

})();
Loading