Extension manifest must request permission to access this host

前端 未结 1 629
青春惊慌失措
青春惊慌失措 2020-12-08 04:42

I am trying to append a div to page of current active tab. However I am getting this error:

Error during tabs.executeScript: Cannot access contents of url ...         


        
相关标签:
1条回答
  • 2020-12-08 05:14

    Code which worked

    manifest.json

    {
        "name": "Manifest Permissions",
        "description": "http://stackoverflow.com/questions/14361061/extension-manifest-must-request-permission-to-access-this-host",
        "version": "1",
        "manifest_version": 2,
        "browser_action": {
            "default_popup": "popup.html"
        },
        "permissions": [
            "tabs",
            "notifications",
            "http://*/",
            "https://*/"
        ]
    }
    

    popup.html

    <html>
    
        <head>
            <script src="back.js"></script>
        </head>
    
        <body>
            <button id="tmp-clipboard">Click Me</button>
        </body>
    
    </html>
    

    back.js

    document.addEventListener("DOMContentLoaded", function () {
        document.getElementById('tmp-clipboard').onclick = function () {
            chrome.tabs.executeScript(null, {
                file: "script.js"
            });
        }
    });
    

    script.js

    var dv = document.createElement('div');
    dv.id = 'myid';
    dv.innerHTML = 'test';
    document.body.appendChild(dv);
    

    Try Eliminating deprecated chrome.tabs.getSelected from your code and use chrome.tabs.query instead.

    Sample Usage

    chrome.tabs.query({
        "currentWindow": true,
        "status": true,
        "active": true //Add any parameters you want
    }, function (tabs) {//It returns an array
        for (tab in tabs) {
            //Do your stuff here
        }
    });
    

    Edit 1

    If you intention is to capture active browsing tab in current window where he clicked browser action use this code

    chrome.tabs.query({
        "currentWindow": true,//Filters tabs in current window
        "status": "complete", //The Page is completely loaded
        "active": true // The tab or web page is browsed at this state,
        "windowType": "normal" // Filters normal web pages, eliminates g-talk notifications etc
    }, function (tabs) {//It returns an array
        for (tab in tabs) {
            $('#url').val(tabs[tab].url); 
            $('#title').val(tabs[tab].title);
            $loader.hide(); 
        }
    });
    
    0 讨论(0)
提交回复
热议问题