Skip to content

Instantly share code, notes, and snippets.

@steida
Created May 16, 2014 19:04
Show Gist options
  • Save steida/c845293390693f798b7f to your computer and use it in GitHub Desktop.
Save steida/c845293390693f798b7f to your computer and use it in GitHub Desktop.
(function() {
var run = function() {
// loadjscssfile("http://iodine-app.herokuapp.com/med-terms/externs/tooltip/bootstrap.min.css", "css");
// loadjscssfile("http://iodine-app.herokuapp.com/med-terms/bookmarklet.css", "css");
// createToolbar();
// loadDictionary(onDictionaryLoad);
var textNodes = [];
console.time('Translating');
getAllTextNodes(document.body, textNodes);
console.timeEnd('Translating');
console.log(textNodes.length);
};
var loadjscssfile = function(filename, filetype){
var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('href', filename);
document.getElementsByTagName('head')[0].appendChild(link);
};
var createToolbar = function() {
var div = document.createElement('div');
document.body.insertBefore(div,document.body.childNodes[0]);
div.id = 'io-translateBar';
document.getElementById('io-translateBar').innerHTML = '<div class="io-barContent"><ul><li><span id="translatedcount" class="countMessage">Translating...</span></li><a href="javascript:document.location.reload();"><li class="close">X</li></a></ul></div>';
};
var loadDictionary = function(onSuccess) {
jQuery.ajax({
type: 'GET',
url: 'http://iodine-app.herokuapp.com/med-terms/data.json',
dataType: 'json',
success: onSuccess
});
};
var onDictionaryLoad = function(terms) {
console.time('Translating');
translate(terms);
console.timeEnd('Translating');
jQuery('.io-tooltip').tooltip();
};
var translate = function() {
var textNodes = textNodesUnder(document.body);
console.log(textNodes.length);
};
var getAllTextNodes = function(node, textNodes) {
for (node = node.firstChild; node; node = node.nextSibling){
if (node.nodeType == 3)
textNodes.push(node);
else
getAllTextNodes(node, textNodes);
}
};
var textNodesUnder = function(el){
var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
while(n=walk.nextNode()) a.push(n);
return a;
};
run()
})();
// var counter = 0;
//
// var updateCounter = function() {
// document.getElementById('translatedcount').innerHTML = counter + ' medical terms translated by <img style="width: 96px; display: inline !important; vertical-align: middle; margin-top: -8px;" src="http://iodine-app.herokuapp.com/med-terms/iodine-logo.png" /></span>';
// };
//
//
// //loadjscssfile("http://iodine-app.herokuapp.com/med-terms/assets/animate.min.css", "css");
// var translateMedTerms = function() {
// var element = document.getElementsByTagName('body')[0];
// };
//
// var nodeCount = 0;
//
// var replaceTextNodes = function(element, terms, callback) {
// nodeCount++;
// if (element.childNodes.length > 0) {
// var i = 0;
// while (i < element.childNodes.length) {
// replaceTextNodes(element.childNodes[i], terms, callback);
// i++;
// }
// }
// nodeCount--;
//
// if (element.nodeType === Node.TEXT_NODE && /\S/.test(element.nodeValue) && element.nodeValue.length > 3) {
// // console.log(element.nodeValue.length);
// var translated = false;
// var value = element.nodeValue;
// var splittedValue = value.split(/\W/g);
// var foundValues = [];
//
// for (var key in terms) {
// var termIndex = key;
// var matchFunction = function(match) {
// var perfectMatch = true;
// var splittedKey = key.split(/\W/g);
// splittedKey.forEach(function (keyPart) {
// if (0 > splittedValue.indexOf(keyPart) || foundValues.indexOf(keyPart) > -1) {
// perfectMatch = false;
// }
// });
//
// if (!perfectMatch) return match;
//
// splittedKey.forEach(function (keyPart) {
// foundValues.push(keyPart);
// });
// translated = true;
// return '<strong class="io-highlight io-tooltip" data-toggle="tooltip" data-placement="top" title="' + terms[termIndex] + '">' + key + '</strong>';
// };
//
// value = value.replace(new RegExp(key, 'g'), matchFunction);
// if (!translated) {
// key = key + 's';
// value = value.replace(new RegExp(key, 'g'), matchFunction);
// }
// }
//
// if (translated) {
// counter++;
// var span = document.createElement('span');
// span.innerHTML = value;
// return element.parentNode.replaceChild(span, element);
// }
// }
// if (nodeCount === 0 && callback) {
// callback();
// document.getElementById("io-loader").className = "animated bounceOut";
// document.getElementById("io-translateBar").className = "animated slideInDown io-show";
// }
// };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment