Tampermonkey replace text

Posted under » JavaScript on 20 November 2023

Many times we replace text using the combination if .getElementById and .innerHTML. eg. 1, 2 and 3.

Replace logo with text taik using innerHTML

(function() {
    'use strict';

var innerHTML = 'taik';
setTimeout(replaceLogo, 500);
function replaceLogo(){
    document.getElementById('logo-icon').innerHTML = innerHTML;
 }
})();

This was done during the greasemonkey days

(function() {
  var replacements, regex, key, textnodes, node, s; 

  replacements = { 
    
    "facebook": "channelnewsasia",
    "Teh": "The",
    "TEH": "THE",
    };

regex = {}; 
for (key in replacements) { 
    regex[key] = new RegExp(key, 'g'); 
} 

textnodes = document.evaluate( "//body//text()", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 

for (var i = 0; i < textnodes.snapshotLength; i++) { 
    node = textnodes.snapshotItem(i); 
    s = node.data; 
    for (key in replacements) { 
        s = s.replace(regex[key], replacements[key]); 
    } 
    node.data = s; 
} 

})();

Continue with a tampermonkey tutorial.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data