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

前端 未结 1 1451
北海茫月
北海茫月 2020-12-20 08:50

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, a

相关标签:
1条回答
  • 2020-12-20 09:13

    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")
    
    0 讨论(0)
提交回复
热议问题