0

I recently started learning a little javascript and made a basic chrome extension, but the problem is that it only sometimes works. What I want it to do is replace the text in any input box to be that of the variable specified in the code, and it works, but not all the time. When I load a site, for example google.com, it might work as I type in the search box, but then doesn't if I refresh the page, and then it might works if I refresh again.

here is my code:

manifest.json

{
  "manifest_version": 2,
  "name": "My Cool Extension",
  "version": "0.1",

  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": ["jQuery.js", "content.js"]
    }
  ],

  "browser_action": {
        "default_icon": "icon.png"
   } 
}

content.js

window.onload = function () {
    var inputs = document.getElementsByTagName('input');
    for (i = 0; i < inputs.length; i++) {
        inputs[i].onkeyup = function () {

            var text = "Text to fill in";
            this.value = text.substring(0, this.value.length);

        };
    }
};

I am waiting for the window to fully load before typing. I have also run it on another computer, same problem. Anyone have any ideas?

  • Set a DOM subtree modification breakpoint on the input on the page in question and check if other code is modifying the input. Also, perhaps it's not an input, but a custom input field made with divs and events etc. – kzahel Aug 06 '16 at 19:49
  • When you typing search query Google page not reloading. It change dynamically, that is `content_scripts` not inject again => your code in `window.onload` executed once. – UserName Aug 06 '16 at 19:55
  • so how would i make it so that it would still work if the page changes dynamically? – Anthony Robertson Aug 06 '16 at 21:08
  • http://stackoverflow.com/questions/2844565/is-there-a-javascript-jquery-dom-change-listener – Haibara Ai Aug 08 '16 at 00:54

0 Answers0