user2918160 user2918160 - 1 year ago 72
Javascript Question

Change matching words in a webpage's text to buttons

I am trying to make a Chrome extension that parses through a website looking for keywords, then replacing those keywords with buttons. However, when I change the text the image path becomes corrupted.

// This is a content script (isolated environment)
// It will have partial access to the chrome API
// Consider adding a "run_at": "document_end" in the manifest...
// don't want to run before full load
// Might also be able to do this via the chrome API
console.log("Scraper Running");
var keywords = ["sword", "gold", "yellow", "blue", "green", "china", "civil", "state"];

// This will match the keywords with the page textx
// Will also create the necessary buttons
(function() {
function runScraper() {
for(var i = 0; i < keywords.length; i++){
.replace(new RegExp(keywords[i], "ig"),"<button> " + keywords[i] + " </button>"));
console.log("Ran it " + i);
function createResourceButton() {
// Programatically create a button here

// Really want to return the button
return null;
function createActionButton() {
// TODO create the functions that the buttons will call
// These will pass data to the chrome extension (see message passing)
// Or we can consider a hack like this:
// "Building a Chrome Extension - Inject code in a page using a Content script"

Image of current results:

Wikipedia images will not load

Answer Source

Your approach to this problem is wrong. To do this, you need to walk though the document only changing text nodes, not the HTML of all nodes.

Modifying the code from this other answer of mine, the following complete extension changes all matching words on the page to buttons.

The extension in action:

button-izing matching words in Wikipedia page used in image by OP


    "description": "Upon action button click, make all matching words buttons.",
    "manifest_version": 2,
    "name": "Button all matching words",
    "version": "0.1",

    "permissions": [

    "background": {
        "scripts": [

    "browser_action": {
        "default_icon": {
            "32": "myIcon.png"
        "default_title": "Make Buttons of specified words"


chrome.browserAction.onClicked.addListener(function(tab) {
    //Inject the script to change the text of all matching words into buttons.
    chrome.tabs.executeScript(,{file: 'contentScript.js'});


    var keywords = ["sword", "gold", "yellow", "blue", "green", "china", "civil", "state"];
    //Build the RegExp once. Doing it for every replace is inefficient.
    //  Build one RegExp that matches all of the words instead of multiple RegExp.
    var regExpText = '\\b(' + keywords.join('|') + ')\\b';
    var myRegExp = new RegExp(regExpText ,'mgi');

    function handleTextNode(textNode) {
        if(textNode.nodeName !== '#text') {
            //Don't do anything except on text nodes.
        let origText = textNode.textContent;
        //Clear the regExp search, not doing so may cause issues if matching against
        //  identical strings after the first one.
        myRegExp.lastIndex = 0;
        let newHtml=origText.replace(myRegExp, '<button>$1</button>');
        //Only change the DOM if we actually made a replacement in the text.
        //Compare the strings, as it should be faster than a second RegExp operation and
        //  lets us use the RegExp in only one place for maintainability.
        if( newHtml !== origText) {
            let newSpan = document.createElement('span');
            newSpan.innerHTML = newHtml;

    //This assumes that you want all matching words in the document changed, without
    //  limiting it to only certain sub portions of the document (i.e. not 'not(a)').
    let allP = [document.body];
    let textNodes = [];
    for (let p of allP) {
        //Create aNodeIterator to get the text nodes descendants
        let nodeIter = document.createNodeIterator(p,NodeFilter.SHOW_TEXT);
        let currentNode;
        //Add text nodes found to list of text nodes to process below.
        while(currentNode = nodeIter.nextNode()) {
    //Process each text node



The code in handleTextNode to make changes to text nodes was modified from code in another answer of mine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download