javascript - How would you make this actually show the image instead of text? -


i trying make chrome extension replaces word "trump" picture of trump pages display html code:

<img src=\"https://yt3.ggpht.com/-oia5414katc/aaaaaaaaaai/aaaaaaaaaaa/qpc5yjqbox8/s48-c-k-no/photo.jpg" height=\"30\" width=\"30\" /> 

instead of picture.

var elements = document.getelementsbytagname('*');  (var = 0; < elements.length; i++) {     var element = elements[i];      (var j = 0; j < element.childnodes.length; j++) {         var node = element.childnodes[j];          if (node.nodetype === 3) {             var text = node.nodevalue;             //document.body.innerhtml = document.body.innerhtml.replace(/trump/gi, '<img src=\"https://yt3.ggpht.com/-oia5414katc/aaaaaaaaaai/aaaaaaaaaaa/qpc5yjqbox8/s48-c-k-no/photo.jpg" height=\"30\" width=\"30\" />');             var replacedtext = text.replace(/trump/gi, '<img src=\"https://yt3.ggpht.com/-oia5414katc/aaaaaaaaaai/aaaaaaaaaaa/qpc5yjqbox8/s48-c-k-no/photo.jpg" height=\"30\" width=\"30\" />');              if (replacedtext !== text) {                 element.replacechild(document.createtextnode(replacedtext), node);             }         }     } } 

i got work code (the comment in above code), lags browser (or crash page):

document.body.innerhtml = document.body.innerhtml.replace(/trump/gi, '<img src=\"https://yt3.ggpht.com/-oia5414katc/aaaaaaaaaai/aaaaaaaaaaa/qpc5yjqbox8/s48-c-k-no/photo.jpg" height=\"30\" width=\"30\" />'); 

untested, instead of replacing element a text node, can replace image element?

// replace document.createtextnode(replacedtext)  // var img = new image() img.width = 30; img.height = 30; img.src = "https://yt3.ggpht.com/-oia5414katc/aaaaaaaaaai/aaaaaaaaaaa/qpc5yjqbox8/s48-c-k-no/photo.jpg"; 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -