Can `chrome.*` extension API's be used inside content scripts?

前端 未结 1 1217
死守一世寂寞
死守一世寂寞 2020-12-12 02:50

I am trying to use chrome.windows.onCreated but I got Uncaught TypeError: Cannot read property \'onCreated\' of undefined. I noticed in the samples

相关标签:
1条回答
  • 2020-12-12 03:06

    From the documentation for Content scripts:

    However, content scripts have some limitations. They cannot:

    • Use chrome.* APIs (except for parts of chrome.extension)
    • Use variables or functions defined by their extension's pages
    • Use variables or functions defined by web pages or by other content scripts

    These limitations can indirectly be avoided, mainly by sending messages within the extension. The documentation offers several examples which involves message passing. Prior Chrome 20, the message API methods was called onRequest and sendRequest. Since version 20, they're called onMessage and sendMessage.

    Here's an answer which mentions the steps how to pass a message from a content script to a popup: https://stackoverflow.com/a/11617742

    0 讨论(0)
提交回复
热议问题