0

我正在更新 Javascript 中的 (Thunderbird) 扩展。在我的一个 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 文件时,它们中具有相同的行。

索姆

  • 我应该坚持var吗?
  • 我应该写:
    if ("undefined" == typeof(ObjectUtils)) {
      const { ObjectUtils } = ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm");
    }
    
    ?
  • 我应该做点别的吗?

根据流行的要求,这是堆栈:

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
4

1 回答 1

0

尝试这个:

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");
于 2021-08-30T13:38:44.097 回答