0

I have created a chrome extension that will append an index to the beginning of search results on google but so far I have only been successful in getting the right output if I inspect element then refresh the page. If I regularly refresh the page it is as if the code was not run.

manifest.json:

{
    "manifest_version": 2,

    "name": "number nodes for accessibility",
    "version": "0.1.0",
    "description": "numbering of anchor tags for accessibility",
    "permissions": ["activeTab", "<all_urls>", "file:///*"],

    "browser_action": {
        "default_icon": "128.png"
    },
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "content_scripts": [{
        "matches":["<all_urls>"],
        "css": ["number.css"],
        "js": ["jquery-3.4.1.js","number.js"]
    }]
  }

background.js

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript({
        file: 'number.js'
    });
});

number.js

$(document).ready(()=>{
    var a = $("div.v0nnCb")
    var links=[];
    for (let index = 0; index < a.length; index++) {
        const element = a[index].innerHTML;
        a[index].innerHTML = "("+index+"): "+element;
        links.push(a[index].baseURI)
    }
    console.log(links)
})
  • 1
    Google constructs its page dynamically using its own page script **after** your content script has run. You can use MutationObserver or setTimeout to recheck the element's existence, [more info](/a/39508954). – wOxxOm Nov 23 '19 at 11:55
  • After editing a content script, click the reload icon on `chrome://extensions` page in your extension's card. – wOxxOm Nov 23 '19 at 16:12

0 Answers0