Erratic behavior of executeScript: “Cannot access a chrome:// URL” on http://www.imdb.com/list

十年热恋 提交于 2019-12-18 07:18:09

问题


I'm executing content scripts on some http://www.imdb.com/list... specific urls. It works fine most of the time, but sometimes it's not. Then I reload the page with F5, and again, it works well most of the time. In an event page I listen for onDOMContentLoaded

chrome.webNavigation.onDOMContentLoaded.addListener(listener, {'url': urlFilter});

The listener hangs well.

function listener(tab){
    t=tab;
    chrome.tabs.executeScript(tab.id, {file: 'elementoWeb 2.1.js'}, function(resultado){
        if(chrome.runtime.lastError)
            console.log('Ejecutando elementoWeb.js, chrome.runtime.lasterror: ', chrome.runtime.lastError, '\nTab: ', t);
    }); 
    chrome.tabs.executeScript(tab.id, {file: 'imdbListaCs.js'}    , function(resultado){
        if(chrome.runtime.lastError)
            console.log('Ejecutando imdbListaCs.js, chrome.runtime.lasterror: ', chrome.runtime.lastError, '\nTab: ', t);
    });
}

When it fails, there are two console's logs like this, one for each execution attempt:

Ejecutando elementoWeb.js, chrome.runtime.lasterror:  Object {message: "Cannot access a chrome:// URL"} 
Tab:  Object {frameId: 0, processId: 320, tabId: 405, timeStamp: 1421159075402.253, url: "http://www.imdb.com/list/......"}
frameId: 0
processId: 320
tabId: 405
timeStamp: 1421159075402.253
url: "http://www.imdb.com/list/......"
__proto__: Object

(I shortened imdb's urls)

Tab 405 is the one with the imdb.com page on it. Not a chrome://url.

Sometimes the error messages differs:

Cannot access contents of url "chrome-devtools://devtools/bundled/devtools.html?&dockSide=undocked&toolbarColor=rgba(223,223,223,1)&textColor=rgba(0,0,0,1)". Extension manifest must request permission to access this host.

I don't have any devtools' experiments activated.

Anyone with the same odd problem?


回答1:


The chrome.webNavigation.onDOMContentLoaded event does not get a Tab object, but an object that contains information about the navigation. Take a look at the documentation of the event listener's callback function (or look at the object that you printed to the console!), it shows that the ID of the tab is provided in the tabId property.

In your code, you read a non-existent property of the parameter (hence undefined) and pass it to chrome.tabs.executeScript. Because the tab ID is missing, executeScript defaults to the active tab of the current window. When the navigation is triggered while you're at a different window or tab, the content script is inserted in the wrong tab. If you don't have access to that tab (e.g. because it's chrome://newtab), Chrome refuses to execute the script. If you're viewing a devtools window, then you'll get an error about that window (though not any more starting Chrome 41).

To fix your problem, change your function to

function listener(details) { // changed "tab" to "details"
    chrome.tabs.executeScript(details.tabId, // (was "tab.id")


来源:https://stackoverflow.com/questions/27924957/erratic-behavior-of-executescript-cannot-access-a-chrome-url-on-http-www

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!