Fill form with extension for google chrome

0

I'm creating an extension that fills a form of a particular web page, but I'm having a problem, the code does not run automatically or even when I click on the extension, I'm 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!";
};
    
asked by anonymous 19.12.2017 / 00:09

1 answer

1

There are two errors:

  • A key browser_action does not have the script, just browser_style , default_icon , default_title , default_popup, theme_icons in>.

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

  • To run a code by clicking the extension icon, you need to call a pop html.

    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.br/"
        ],
        "content_scripts": [{
            "js": ["funcao.js"],
            "matches": ["https://www.google.com.br/"]
        }]
    }
    

    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>
    

    funcao2.js

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

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

        
    19.12.2017 / 00:50