WebExtensions: How to send a message to content script? (Android)

后端 未结 1 1022
说谎
说谎 2020-12-22 10:01

I just noticed that the tabs API is only available for the desktop not for Android. In the past I have used this code to send messages to my content scripts:



        
相关标签:
1条回答
  • 2020-12-22 10:20

    As of Firefox 54, use .tabs.sendMessage()

    As of Firefox 54, the tabs API is supported on Firefox for Android.

    Alternative for versions of Firefox prior to Firefox 54.

    The storage API is stated as supported in Firefox for Android. Thus, while I have not tested it, a method you could use to send data to content script would be to save a value using chrome.storage.local.set(). By listening to the chrome.storage.onChanged event in your content script(s), you can then be notified of that data being stored/changed. This will provide an event driven way to send a message (i.e. stored data) to the content script.

    In order to differentiate between receiving the data in different tabs, you will need to establish a protocol for what the data you save means. This could be as simple as just a particular saved key/value meaning that all content scripts should send a message to the background script to get more information, or more complex where you send/store something like:

    {
        contentScriptMessage: {
            tab: 14,
            frame: 1234,
            message: 'Some data'
        }
    }
    

    In each content script's chrome.storage.onChanged listener, it can then ignore any changes that are not to the tab/frame in which it is running.

    This methodology will require fleshing out as you try to implement it. Hopefully, at least part of the chrome.tabs API will be implemented for Android in the near future.

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