从XUL访问附加SDK“主” [英] Access Add-On SDK 'main' from XUL

查看:138
本文介绍了从XUL访问附加SDK“主”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的插件中,我使用XUL来显示对话框窗口,因为我可以自定义它们的外观以适合插件的一般风格(如自定义 titlebar )。 使用迁移指南,我可以轻松地做到这一点。问题是,我想从XUL对话框中调用附加程序的 main 模块中的某些函数。



经过一番搜索,我发现 loader 模块,这似乎能够做到我想要的。但是,我在使用它来访问 main 模块时遇到了麻烦。



首先,方法,如文件中所述;

xul_dialog.js

  let {Loader} = Components.utils.import('resource://gre/modules/commonjs/toolkit/loader.js'); 
let loader = Loader.Loader({
paths:{
'toolkit /':'resource:// gre / modules / commonjs / toolkit /',
'': 'resource:/// modules /',
'./':'resource://< my-addon-name> / root /'
}
});

let main = Loader.main(loader,'./main');

我得到一个'./ main' resource://< my-addon-name> / root / 找不到c>。考虑到我使用了不正确的路径,我尝试了一下,直到我可以删除所有路径相关的错误。



xul_dialog.js

p>

  let {Loader} = Components.utils.import('resource://gre/modules/commonjs/toolkit/loader.js' ); 
let loader = Loader.Loader({
paths:{
'toolkit /':'resource:// gre / modules / commonjs / toolkit /',
'': 'resource:// gre / modules / commonjs /',
'./':'resource://< my-addon-id> -at-jetpack /< my-addon-name> / lib /'
}
});

let main = Loader.main(loader,'./main');

这一次我在 loader.js第279行。

 组件在此上下文中不可用。 
组件提供的功能可能在SDK
模块中可用:https://jetpack.mozillalabs.com/sdk/latest/docs/

但是,如果您仍然需要要导入组件,您可以使用
`chrome`模块的属性快捷键组件属性:

快捷键:
Cc = Components.classes
Ci =组件。接口
Cu = Components.utils
CC = Components.Constructor
例子:
let {Cc,Ci} = require('chrome');

当我使用 Loader.Require(loader,{ id:'./main'})而不是 Loader.main 。我甚至尝试在实例化加载器时传递 Components 作为 globals ,但没有太多好运。 b

我相当确定我做了很多错误的事情。即使在 loader.js 中花费了相当多的时间,我也不明白为什么会出现错误。此外,我还认为,有一个更好的选择,比使用附加ID为 main.js ;硬编码它似乎并不正确。



任何帮助将非常感激。

解决方案

你需要做的是找到一个 Loader 的特定实例,而不是创建一个新的。



在main.js中


$ b

  const {id,name,prefixURI} = require(@ loader / options); 
//将这些传递给XUL对话框

在xul.js xul对话框脚本的名称)

$ p $ Components.utils.import(resource:// gre / modules / addons / XPIProvider。 JSM);
var extensionscope = XPIProvider.bootstrapScopes [id];
var mainjssandbox = extensionscope.loader.sandboxes [prefixURI + name +/lib/main.js];

假设在main处有一个 foo .js,你可以这样称呼它:

  mainjssandbox.foo(); 

当然,不要指望XUL和Add-on SDK实际上混合成一个事情。

In my add-on, I'm using XUL to display dialog windows because I can customize their appearance to suit the add-on's general style (like a custom titlebar).

Using the migration guide, I'm able to do this easily. The thing is, I would like to call certain functions in the add-on's main module from the XUL dialog.

After a bit of searching I found the loader module, which seems to be able to do exactly what I want. But, I'm experiencing trouble in using it to access the main module.

First, I tried the obvious approach as mentioned in the documentation;

xul_dialog.js:

let {Loader} = Components.utils.import('resource://gre/modules/commonjs/toolkit/loader.js');
let loader = Loader.Loader({
    paths: {
        'toolkit/': 'resource://gre/modules/commonjs/toolkit/',
        '': 'resource:///modules/',
        './': 'resource://<my-addon-name>/root/'
    }
});

let main = Loader.main(loader, './main');

I got an error that './main' was not found at resource://<my-addon-name>/root/. Figuring that I was using the incorrect paths, I experimented a bit until I could remove all path associated errors.

xul_dialog.js:

let {Loader} = Components.utils.import('resource://gre/modules/commonjs/toolkit/loader.js');
let loader = Loader.Loader({
    paths: {
        'toolkit/': 'resource://gre/modules/commonjs/toolkit/',
        '': 'resource://gre/modules/commonjs/',
        './': 'resource://<my-addon-id>-at-jetpack/<my-addon-name>/lib/'
    }
});

let main = Loader.main(loader, './main');

This time I got a rather confusing error at loader.js, line 279.

Components is not available in this context.
Functionality provided by Components may be available in an SDK
module: https://jetpack.mozillalabs.com/sdk/latest/docs/

However, if you still need to import Components, you may use the
`chrome` module's properties for shortcuts to Component properties:

Shortcuts:
    Cc = Components.classes
    Ci = Components.interfaces
    Cu = Components.utils
    CC = Components.Constructor
Example:
    let { Cc, Ci } = require('chrome');

I get the same error when I use Loader.Require(loader, {id: './main'}) instead of Loader.main. I even tried passing Components as globals when instantiating the loader, but without much luck.

I'm fairly certain that I'm doing a lot of things wrong. I don't understand why I'm getting the error, even after spending quite a bit of time in loader.js. Plus, I also think that there would be a better alternative than having to use the add-on id for the path to main.js; hard-coding it like that doesn't seem right.

Any help would be really appreciated.

解决方案

What you have to do is to find a specific instance of Loader, not create a new one.

At main.js

const { id, name, prefixURI } = require("@loader/options");
//pass these to the XUL dialog

At xul.js (or whatever is the name of the xul dialog script)

Components.utils.import("resource://gre/modules/addons/XPIProvider.jsm");
var extensionscope = XPIProvider.bootstrapScopes[id];
var mainjssandbox = extensionscope.loader.sandboxes[prefixURI + name + "/lib/main.js"];

Assuming there is a foo function at main.js, you can call it like

 mainjssandbox.foo();

Of course don't expect things to work as if XUL and Add-on SDK actually blended into one thing.

这篇关于从XUL访问附加SDK“主”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆