WebExtensions:如何将消息发送到内容脚本? (机器人) [英] WebExtensions: How to send a message to content script? (Android)

查看:159
本文介绍了WebExtensions:如何将消息发送到内容脚本? (机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚注意到,标签API是仅适用于桌面,不适用于Android。在过去,我用这段代码发送消息给我的内容脚本:

  sendMsgToTabs(msg){
return browser.tabs.query({})。then(tabs => {
let msgPromises = []
for(let tab of tab){
let msgPromise = browser.tabs.sendMessage (tab.id,msg)
msgPromises.push(msgPromise)
}
return Promise.all(msgPromises)
})
}
code>

但是如何在tab API不可用时执行此操作?

我的意思是唯一的我能想到的事情是不断地从内容脚本发送空的消息到后台脚本,并且每当后台脚本有新的信息时,它就可以发送对这些消息之一的直接响应。但这听起来非常低效。 从Firefox 54开始,使用 .tabs.sendMessage()

从Firefox 54开始, 选项卡 API 在Firefox for Android上受支持
$ b

在Firefox 54之前。



storage API 在Firefox for Android中支持。因此,虽然我没有测试过,但是可以用来将数据发送到内容脚本的方法是使用 chrome.storage.local.set() 。通过收听 chrome.storage.onChanged 事件,则可以通知您存储/更改的数据。这将提供事件驱动的方式来发送消息(即存储的数据)到内容脚本。



为了区分在不同标签中接收数据,您将需要为您保存的数据建立协议。这可能就像一个特定的保存键/值一样简单,这意味着所有内容脚本都应该向后台脚本发送消息以获取更多信息,或者在发送/存储类似以下内容时更为复杂:

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




$ b $ p
$ b在每个内容脚本的 chrome.storage.onChanged 侦听器,它可以忽略任何不是它所运行的选项卡/框架的更改。



此方法需要充实当你试图实现它。希望至少部分 chrome.tabs API将在不久的将来为Android实现。


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:

sendMsgToTabs(msg) {
    return browser.tabs.query({}).then(tabs => {
        let msgPromises = []
        for (let tab of tabs) {
            let msgPromise = browser.tabs.sendMessage(tab.id, msg)
            msgPromises.push(msgPromise)
        }
        return Promise.all(msgPromises)
    })
}

But how am I supposed to do that when the tabs API is not available?
I mean the only thing I can think of is to constantly send empty messages from the content scripts to the background script and whenever the background script has new information then it can send a direct response to one of these messages. But that sounds horribly inefficient. There must be a better way, right?

解决方案

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.

这篇关于WebExtensions:如何将消息发送到内容脚本? (机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆