拥有一个“大脑”在 Firefox 插件中?
我有一个插件,每隔 5 分钟左右检查 RSS 提要是否有新帖子,如果有,它会显示一个警报()。问题是,我担心如果用户打开多个窗口,当有新帖子时,会弹出数百万个警报,说同样的事情。有没有办法一次只让一个“大脑”运行?
提前致谢!
I have an addon that every 5 minuets or so checks an rss feed for a new post, and if there is one, it displays an alert(). Problem is, I'm afraid that if the user opens multiple windows, that when there's a new post a millions of alerts will popup saying the same thing. Is there anyway to have just one "brain" running at a time?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查找称为“Javascript 共享代码模块”或 JSM 的内容。
主要文档位于:
https://developer.mozilla.org/En/Using_JavaScript_code_modules
每个 .插件中需要共享内存的 Node.js 文件将使用以下行打开:
Components.utils.import("resource://xxxxxxxx/modules/[yourFilenameHere].jsm", com.myFirefoxAddon.shared);
上面的行打开 [yourFilenameHere].jsm 并将其导出(见下文)函数和变量加载到 com.myFirefoxAddon.shared 对象中。加载的该对象的每个实例都将指向内存中的同一个实例。
请注意,如果您希望插件能够通过审核,则需要将所有代码写入 com.myFirefoxAddon.* 类型对象,因为 AMO 的暴徒正在阻止批准不符合 尊重全局命名空间
JSM 最大的警告是您需要手动导出您希望代码的其余部分可用的每个函数...由于 JS 不支持公共/私有类型的东西,这让我觉得这是一种穷人的“公共”支持...在任何情况下在这种情况下,您需要在 JSM 文件中的某个位置创建一个 EXPORTED_SYMBOLS 数组,并列出要导出的每个函数或对象,如下所示:
Look up something called "Javascript shared code modules" or JSMs.
Primary docs are here:
https://developer.mozilla.org/En/Using_JavaScript_code_modules
Each .js file in your addon that needs shared memory will open with the following line:
Components.utils.import("resource://xxxxxxxx/modules/[yourFilenameHere].jsm", com.myFirefoxAddon.shared);
The above line opens [yourFilenameHere].jsm and loads its exported (see below) functions and variables into the com.myFirefoxAddon.shared object. Each instance of that object loaded will point to the same instance in memory.
Note that if you want to have any hope of you addon making it past moderation, you will need to write all your code in a com.myFirefoxAddon.* type object as the goons at AMO are preventing approval of addons that do not Respect the Global Namespace
The biggest caveat for JSM is that you need to manually export each function that you want to be available to the rest of your code... since JS doesn't support public/private type stuff this strikes me as a sort of poor-man's "public" support... in any case, you will need to create an EXPORTED_SYMBOLS array somewhere in your JSM file and name out each function or object that you want to export, like this:
[已编辑]
模块不是解决此问题的正确方法,因为代码仍会导入到每个窗口中,并且您设置的任何侦听器/计时器都将在每个窗口中运行。您应该为此使用模块时要小心——所有计时器/回调都必须在模块代码中设置(而不仅仅是使用模块中定义的观察者对象),并且不应使用对模块中窗口的任何引用。< /em>正确的方法是我更喜欢编写一个 XPCOM 组件(在 JS 中)。是的,这有点复杂,而且我没有方便的链接来解释如何做到这一点。有一件事:使用 XPCOMUtils 实现它更容易,旧文档会给您带来大量样板代码。[edited]
Modules are not the right solution to this problem, since the code will still be imported into every window and the whatever listeners/timers you set up will run in every window.You should be careful with using modules for this -- all the timers/callbacks must be set up in the module code (not just using the observer object defined in the module) and you shouldn't use any references to the window in the module.The right way to do this isI would prefer to write an XPCOM component (in JS). It's somewhat complicated, yes and I don't have a handy link explaining how to do it. One thing: implementing it using XPCOMUtils is easier, older documentation will throw lots of boilerplate code on you.