动态关于:使用JavaScript代码模块的URL(XPCOMUtils.jsm)?
我正在尝试动态注册Firefox扩展的自定义URL,相对于一个变量。 例如:动态关于:使用JavaScript代码模块的URL(XPCOMUtils.jsm)?,javascript,firefox,dynamic,browser,firefox-addon,Javascript,Firefox,Dynamic,Browser,Firefox Addon,我正在尝试动态注册Firefox扩展的自定义URL,相对于一个变量。 例如: - If var = 1, then create... about:123 - If var = 2, then create... about:abc - If var = 3, then create... about:xxx JavaScript代码模块是向我建议的,特别是XPCOMUtils.jsm,但是如何在代码中实现它呢 我浏览了这些页面,但毫无结果: 我已经使用chrome.manifes
- If var = 1, then create... about:123
- If var = 2, then create... about:abc
- If var = 3, then create... about:xxx
JavaScript代码模块是向我建议的,特别是XPCOMUtils.jsm,但是如何在代码中实现它呢
我浏览了这些页面,但毫无结果:
chrome.manifest
文件实现了一个定制的staticabout:URL。但是我不需要关于:URL的静态。我需要动态关于:URL,相对于Firefox扩展内部的变量
谢谢你的帮助 我写道,这将允许您动态地向firefox添加/删除关于:什么的URI
这是以下各项的当前源代码:
关于这个主题的优秀主题,这里有复制粘贴示例:两种解决方案都很好。
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
const { Cr, Cu, Ci, Cc, Cm } = require('chrome');
const { when: unload } = require('sdk/system/unload');
const { validateOptions : validate } = require('sdk/deprecated/api-utils');
const { uuid } = require('sdk/util/uuid');
const { URL, isValidURI } = require('sdk/url');
const tabs = require('sdk/tabs');
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
const validOptions = {
what: {
is: ['string'],
ok: function(what) {
if (what.match(/^[a-z0-9-]+$/i))
return true;
return false;
},
map: function(url) url.toLowerCase()
},
url: {
is: ['string', 'undefined']
},
content: {
is: ['string', 'undefined']
},
useChrome: {
is: ['undefined', 'null', 'boolean'],
map: function(use) !!use
}
};
function add(options) {
let { what, url, content, useChrome } = validate(options, validOptions);
let baseURL = url;
if (content) {
url = encodeURI('data:text/html;charset=utf-8,' + content.replace(/\{\s*page\.baseurl\s*\}/, baseURL));
}
let classID = uuid();
let aboutModule = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIAboutModule]),
newChannel: function (aURI) {
let chan = Services.io.newChannel(url, null, null);
if (useChrome)
chan.owner = Services.scriptSecurityManager.getSystemPrincipal();
return chan;
},
getURIFlags: function () Ci.nsIAboutModule.ALLOW_SCRIPT
};
let factory = {
createInstance: function(aOuter, aIID) {
if (aOuter)
throw Cr.NS_ERROR_NO_AGGREGATION;
return aboutModule.QueryInterface(aIID);
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory])
};
// register about:what
Cm.QueryInterface(Ci.nsIComponentRegistrar).
registerFactory(classID, '', '@mozilla.org/network/protocol/about;1?what='+what, factory);
let remover = unloader.bind(null, what, factory, classID);
unload(remover);
return undefined;
}
exports.add = add;
function unloader(what, factory, classID) {
// unregister about:what
Cm.QueryInterface(Ci.nsIComponentRegistrar).unregisterFactory(classID, factory);
let regEx = new RegExp('^' + what, 'i');
// AMO policy, see http://maglione-k.users.sourceforge.net/bootstrapped.xhtml
// close about:what tabs
for each (let tab in tabs) {
let url = URL(tab.url);
if (url.scheme === 'about' && url.path.match(regEx)) {
tab.close();
}
}
}