我正在更新Javascript中的一个(雷鸟)扩展。在我的JS文件中,我有:
var { ObjectUtils } = ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm");现在,我知道var是不受欢迎的,我们喜欢const,而这种导入确实是不变的。但是,如果我使用:
const { ObjectUtils } = ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm");我在重新定义ObjectUtils时会遇到错误,可能是在我的XUL/XHTML中包含多个JS文件时,这些JS文件中有相同的行。
Som
var吗?根据普遍的请求,下面是堆栈:
redeclaration of var ObjectUtils removedupes.js:1
<anonymous> chrome://removedupes/content/removedupes.js:1
<anonymous> chrome://removedupes/content/overlay-injectors/messenger.js:5
_loadIntoWindow jar:file:///home/eyalroz/.thunderbird/Profiles/8shkz5up.default/extensions/{a300a000-5e21-4ee0-a115-9ec8f4eaa92b}.xpi!/api/WindowListener/implementation.js:968
onLoadWindow jar:file:///home/eyalroz/.thunderbird/Profiles/8shkz5up.default/extensions/{a300a000-5e21-4ee0-a115-9ec8f4eaa92b}.xpi!/api/WindowListener/implementation.js:687
checkAndRunExtensionCode resource:///modules/ExtensionSupport.jsm:220
_checkAndRunMatchingExtensions resource:///modules/ExtensionSupport.jsm:192
registerWindowListener resource:///modules/ExtensionSupport.jsm:71
forEach self-hosted:4357
registerWindowListener resource:///modules/ExtensionSupport.jsm:70
startListening jar:file:///home/eyalroz/.thunderbird/Profiles/8shkz5up.default/extensions/{a300a000-5e21-4ee0-a115-9ec8f4eaa92b}.xpi!/api/WindowListener/implementation.js:569
startListening self-hosted:1175
result resource://gre/modules/ExtensionParent.jsm:935
withPendingBrowser resource://gre/modules/ExtensionParent.jsm:491
result resource://gre/modules/ExtensionParent.jsm:935
callAndLog resource://gre/modules/ExtensionParent.jsm:897
recvAPICall resource://gre/modules/ExtensionParent.jsm:934
InterpretGeneratorResume self-hosted:1482
AsyncFunctionNext self-hosted:692发布于 2021-08-30 13:38:44
试试这个:
const { ObjectUtils: Cu } = ChromeUtils;
Cu.import("resource://gre/modules/ObjectUtils.jsm");这个帖子可以帮上忙。
从我可以收集到的信息来看,这增加了一种导入ObjectUtils的方法,而无需为常量赋值(在运行时不能更改)。
我是这样想的:
// say you create a constant array (I know its not an array - just an example)
const cars = ["Saab", "Volvo", "BMW"];
// you can add an element, just like you can add an import
cars.push("Audi");https://stackoverflow.com/questions/68984986
复制相似问题