简体   繁体   中英

Chrome Extension Fill in Selected Field

I'm trying to build a very simple chrome extension that fills the selected form field on a page. I've created a git repo with the code:

https://github.com/mastermindg/chrome-extension-fillform

My problem is that nothing happens when I click the button.

Here's my background js:

chrome.tabs.getSelected(null, function(tab) {
      d = document;

      var formfield = d.activeElement;
      d.getElementById(formfield).value = Math.floor(89+Math.random()*11);

});

I suspect it's around the js element selection process but I'm not sure.

It's not that perfect solution but the best I've seen so far...

I ended up using browser_action instead of background and using chrome.tabs.executeScript to affect the active tab. I updated my github repo as well with the current code.

chrome.tabs.executeScript(null,
    {code:"var valued = Math.floor(89+Math.random()*11);var formfield = document.activeElement;formfield.value = valued"}
);

You should replace d.getElementById(formfield).value with just formfield.value .

var formfield = d.activeElement; gets the element for you, so d.getElementById(formfield) is not needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM