Modify HTML of loaded pages using chrome extensions

后端 未结 2 1015
感动是毒
感动是毒 2020-12-02 05:37

How can I add some HTML code to the loaded page if page\'s title contains specific text?

Chrome extensions are new grounds to me and your help would be greatly appre

2条回答
  •  遥遥无期
    2020-12-02 06:01

    @Sudarshan 's answer explains page specificity, Great, but what about the comments added? here's how to do what he missed in an easier more practical manner to modify existing content or to create content on an page the easiest method would be:

    Modify

    single item modification:

    document.getElementById("Id").innerHtml = this.innerHtml + "";
    

    or to modify attributes:

    document.getElementById("Id").class = "classname";
    
    //or ->
    
    document.getElementById("Id").style.color = "#a1b2c3";
    

    for adding multiple lines of code do the following:

    document.getElementById("Id").innerHtml = this.innerHtml + `
                                                                 
     and we're on multiple lines!` + "variables can be inserted too!" + `        
     
    `
    ;
    
    • but now what about easy element insertation?

    Create

    large code injection (example from coding project done a while back) see insertAdjacentHtml API :

    var bod = document.getElementsByTagName('body')[0];
    
    bod.insertAdjacentHTML('afterbegin', `
        
    `);

    references:

    • insert - Adjacent - Html
    • content specific code

提交回复
热议问题