简体   繁体   English

Firefox附加组件:同步通信?

[英]Firefox Add-on: Synchronous communication?

I'm at the moment looking for a class or something that would make it possible to synchronously communicate with the Javascript of a tab. 我正在寻找一个可以与标签的Javascript同步通信的类或其他东西。

It's a pain to dynamically manipulate a website with the default API. 使用默认API动态操作网站是一件痛苦的事。 It would be so great if we could do something like this: 如果我们能做到这样的事情会很棒:

var tabs = require('sdk/tabs');
var exampleElement = tabs.activeTab.document.getElementById('exampleId');
console.log(exampleElement.innerHTML);
exampleElement.style.width = '200px';

So is there a class/lib that allows me to do this or can you tell me how I could make something like that possible? 那么是否有一个类/ lib允许我这样做,或者你能告诉我如何才能做出类似的东西吗? I'm really new to this... 我真的很新......

As mentioned here , it's not possible with the addons SDK. 正如这里提到的,使用addons SDK是不可能的。 You can use non-SDK code for synchronous communication, but if Firefox switches to a multi-process architecture, it will stop working. 您可以使用非SDK代码进行同步通信,但如果Firefox切换到多进程体系结构,它将停止工作。 It's generally recommended to stick with the asynchronous APIs for new code. 通常建议坚持使用新代码的异步API。

One option would be to wrap async calls using the built-in promise library: 一种选择是使用内置的promise库包装异步调用:

https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/core/promise.html https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/core/promise.html

This style of development is a little more obtuse than synchronous calls but it does avoid 'callback hell' when using multiple / nested callbacks. 这种开发风格比同步调用更加迟钝,但它在使用多个/嵌套回调时确实避免了“回调地狱”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM