gpt4 book ai didi

firefox-addon - 如何使 Firefox 扩展中的代码在计时器上执行

转载 作者:行者123 更新时间:2023-12-04 03:45:52 24 4
gpt4 key购买 nike

今天是我使用 Firefox 扩展的第一天。

基本上,我正在制作一个将在内部网络上使用的扩展程序,以检查 Web 服务器是否有新通知。

我使用 mozilla 页面上的向导制作了一个骨架扩展,然后主要用一些 ajax 代码编辑了 overlay.js。

我正在使用“加载”事件监听器向我的 ajax 调用调用 setTimeout,然后使用 setTimeouts 循环。

问题似乎是在每个新的浏览器窗口上执行“加载”事件监听器。我只想要一个全局计时器来解决这个问题。

有任何想法吗?

更新:

我发现了这个:https://developer.mozilla.org/en/JavaScript_code_modules/Using
这似乎是我想要的。问题是我不知道如何导入 jsm 文件。目录结构是什么?

更新:

尝试此操作时:

chrome.manifest

content   spt                 chrome/content/
skin spt classic/1.0 chrome/skin/
locale spt en-US chrome/locale/en-US/


overlay chrome://browser/content/browser.xul chrome://spt/content/ff-overlay.xul
style chrome://global/content/customizeToolbar.xul chrome://spt/skin/overlay.css


resource mycontent chrome/content/

chrome/content/overlay.js 的前 5 行
try{
Components.utils.import("resource://spt/mycontent/ajax.jsm");
}catch(err){
alert(err);
}

我收到此错误:

[异常...“组件返回失败代码:0x80040111(NS_ERROR_NOT_AVAILABLE)[nsIXPCComponents_Utils.import]”nsresult:“0x80040111(NS_ERROR_NOT_AVAILABLE)”位置:“JS 框架::chrome://spt/content/overlay.js::: :第2行“数据:无]

或者,如果我从 chrome.manifest 中删除资源别名并在 overlay.js 的开头使用它
try{
Components.utils.import("chrome://spt/content/ajax.jsm");
}catch(err){
alert(err);
}

我收到此错误:

[异常...“组件返回失败代码:0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXPCComponents_Utils.import]”nsresult:“0x80070057 (NS_ERROR_ILLEGAL_VALUE)”位置:“JS 框架::chrome://spt/content/overlay.js::: :第 3 行“数据:无]

最佳答案

是的,如果您的代码应该在窗口之间共享(并且在加载新窗口时不应执行)并且不需要访问 chrome,请使用 JavaScript code modules .

您可以使用以下命令导入模块:

Components.utils.import("resource://youraddon/your_module.jsm");

前提是您设置了 resource在您的 chrome.manifest .例如。如果你添加
resource youraddon modules/

那么你的文件必须存储在 /path/to/your/addon/modules/your_module.jsm .

补充说明:
  • 代码模块不必具有文件扩展名 .jsm .你可以离开它.js .有时与某些编辑器(和语法突出显示等)一起工作得更好。
  • Afaik 你不能使用 setTimeout在一个模块中,因为它无法访问 window目的。我建议使用 nsITimer .
  • 关于firefox-addon - 如何使 Firefox 扩展中的代码在计时器上执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4629893/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com