Fill in form with extension for google Chrome

Asked

Viewed 567 times

0

I am creating an extension that fills a form of a particular web page, but I am with a problem, the code does not run either automatically and even when clicking on the extension, I am very lost.

manifest.json

{
"name": "SGIR - Extensão",
"manifest_version": 2,
"version": "1.0",
"description": "Preencher formulários",
"browser_action": {
    "script": ["funcao.js"]
},
"permissions": ["tabs"]
}

function.js

chrome.window.onload = function(){
var ni = document.getElementById("NI");
ni.action = "Alguma coisa!";
};

1 answer

1


There are two mistakes:

  1. A key browser_action does not have the script, only browser_style, default_icon, default_title, default_popup, theme_icons.

  2. The file is calling the onload method, after the page is already loaded.

To execute a code by clicking on the extension icon, you need to call an html pop.

Manifest.json

{
    "name": "SGIR - Extensão",
    "manifest_version": 2,
    "version": "1.0",
    "description": "Preencher formulários",
    "browser_action": {
        "default_popup": "popup.html"
    },
    "permissions": [
        "tabs",
        "https://www.google.com/"
    ],
    "content_scripts": [{
        "js": ["funcao.js"],
        "matches": ["https://www.google.com/"]
    }]
}

function.js

var ni = document.getElementById("lst-ib");
ni.value = "Alguma coisa!";

popup.html

<!doctype html>
<html>
  <head>
    <script src="funcao2.js"></script>
  </head>
  <body></body>
</html>

function 2.js

chrome.tabs.query({active:true,windowType:"normal", currentWindow: true},function(tab){
    chrome.tabs.executeScript(tab[0].id, {
        "file": "funcao.js"
    });
})

Add permission to the URL you want to modify. It can be regex.

  • I understood his explanation, but even so, when I click on the extension icon nothing happens on the page I’m on, it does not complete the form, in case the text field with id="NI", which may be wrong?

  • In the case of inputs, the correct is ni.value = "Alguma coisa!";.

  • I tried with "value" too, but nothing happens on the page, it just opens the extension popup...

  • @netoschneider I edited my answer. This way it will work either automatically, or by clicking on the extension icon.

  • Thank you very much, cleared my doubt!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.