Chrome extension: message from background.js to content.js

后端 未结 1 1289
野的像风
野的像风 2020-12-08 18:00

I am trying to send a message from background.js to content.js using the following code:

Background

chrome         


        
相关标签:
1条回答
  • 2020-12-08 18:27

    There are 2 sendMessage functions in Chrome API.

    • chrome.runtime.sendMessage sends a message to all open extension pages (i.e. background, popup, etc.)
    • chrome.tabs.sendMessage sends a message to all content scripts from the extension in a given tab (possibly filtered by frame ID)

    So, to send a message TO a content script, you need to use chrome.tabs. To send a message FROM a content script (or within the extension pages), you need to use chrome.runtime.

    The event is chrome.runtime.onMessage in both cases.

    See Messaging docs for more details.

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