gpt4 book ai didi

javascript - nsI 协议(protocol)示例不清楚

转载 作者:行者123 更新时间:2023-11-28 13:30:59 25 4
gpt4 key购买 nike

我在 MDN 上比较了这个例子: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIProtocolHandler#Implementation

关于如何创建自定义协议(protocol)的此附加组件: https://addons.mozilla.org/en-US/firefox/files/browse/141969/file/components/AboutFosdem.js#top

有人可以详细说明它到底想做什么吗? SMTP 的东西让我很失望。

我无法理解 MDN 上的示例在做什么,无论它在没有 chrome.manifst 的情况下做什么。我知道该插件正在创建“fosdem://blah”,其中 blah 是根据 WhereToGo 中的定义我想要的任何内容,但它使用 chrome.manifest。

我认为 mdn 示例正在做与插件相同的事情,我会在复制粘贴该 mdn 代码后执行类似的操作来设置我的自定义协议(protocol):

function myCustomBlah() {}

myCustomBlah.prototype =
makeProtocolHandler("mycustomblah",
-1,
"b14c2b67-8680-4c11-8d63-9403c7d4f757"); //i can generate any id

var components = [myCustomBlah];
const NSGetFactory = XPCOMUtils.generateNSGetFactory(components);

最佳答案

好吧,让我们给出一个更合理的自定义协议(protocol)处理程序示例。

我决定实现一个 ddg: 协议(protocol)处理程序,注册后可用于在地址栏中输入 ddg:some search terms(除其他外),它将加载“一些搜索词”的 DuckDuckGo 搜索页面。

组件

需要实现nsIProtocolHandler界面。

这个示例组件的作用是“重定向”到 DuckDuckGo(好吧,不是真正的重定向,但它返回了 duckduckgo.com 的 channel )。请参阅内嵌评论。

var {classes: Cc,
interfaces: Ci,
manager: Cm,
results: Cr,
Constructor: CC
} = Components;
Cm.QueryInterface(Ci.nsIComponentRegistrar);

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

const SCHEME = "ddg";
const DDG_URI = Services.io.newURI("https://duckduckgo.com/?q=%s", null, null);

const nsIURI = CC("@mozilla.org/network/simple-uri;1", "nsIURI");

function DuckDuckGoProtocolHandler() {}
DuckDuckGoProtocolHandler.prototype = Object.freeze({
classDescription: "DuckDuckGo Protocol Handler",
contractID: "@mozilla.org/network/protocol;1?name=" + SCHEME,
classID: Components.ID('{858ea860-129a-11e4-9191-0800200c9a66}'),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler]),

// nsIProtocolHandler
scheme: SCHEME,
defaultPort: -1, // No default port.

// nsIProtocolHandler
allowPort: function(port, scheme) {
// This protocol handler does not support ports.
return false;
},

// nsIProtocolHandler
// Our protocol handler does not support authentication,
// but it is OK to be loaded from any web-page, not just privileged pages""
protocolFlags: Ci.nsIProtocolHandler.URI_NOAUTH |
Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,

// nsIProtocolHandler
newURI: function(aSpec, aOriginCharset, aBaseURI) {
// Nothing special here, actually. We were asked to create a new URI.

// If there is a base-URI, this means that the browser tries to resolve
// a dependent resource (an image, script) or the user clicked on a relative link.
// In this case we cannot really return another "ddg" URI, but need to return
// the proper https URI.
if (aBaseURI && aBaseURI.scheme == SCHEME) {
return Services.io.newURI(aSpec, aOriginCharset, DDG_URI);
}

// We don't care about the charset, so just ignore that
// (we support what nsIURI supports).
let rv = new nsIURI();
rv.spec = aSpec;
return rv;
},

// nsIProtocolHandler
newChannel: function(aURI) {
// We were asked to open a new channel.
// We could implement an entirely custom channel that supports
// (most of) nsIChannel. But that is tremendous work and outside
// of the scope of this basic example (which is about protocol handlers and
// not channels).
// Or we can just return any other channel we can create.
// Since we're going to implement the "ddg:" protocol, lets just open a
// regular https channel to duckduckgo.com, use the URI as the search term
// and return that channel.
let spec = DDG_URI.spec.replace("%s", aURI.path);
let channel = Services.io.newChannel(spec, aURI.originCharset, null);

// Setting .originalURI will not only let other code know where this
// originally came from, but the UI will actually show that .originalURI.
channel.originalURI = aURI;

return channel;
}
});

在 chrome.manifest 中注册组件

如果我们的组件是 JavaScript 组件,我们需要实现 NSGetFactory通过 chrome.manifest 注册。幸运的是,XPCOMUtils.jsm 有一个帮助程序。

var NSGetFactory =
XPCOMUtils.generateNSGetFactory([DuckDuckGoProtocolHandler]);

在引导加载项(和便签本)中注册

在引导/无需重新启动的附加组件(包括 SDK 附加组件)和 Scratchpad 中,需要手动注册组件,因为 chrome.manifest 注册不可用。

可以注册NSGetFactory(classID)的结果,但这里有一些代码手动创建工厂并注册它。

function Factory(component) {
this.createInstance = function(outer, iid) {
if (outer) {
throw Cr.NS_ERROR_NO_AGGREGATION;
}
return new component();
};
this.register = function() {
Cm.registerFactory(component.prototype.classID,
component.prototype.classDescription,
component.prototype.contractID,
this);
};
this.unregister = function() {
Cm.unregisterFactory(component.prototype.classID, this);
}
Object.freeze(this);
this.register();
}
var factory = new Factory(DuckDuckGoProtocolHandler);

请注意,在无需重新启动的附加组件中,您还需要取消注册它再次关闭!

factory.unregister();

在便签本中测试

将组件代码和手动注册代码复制到暂存器中,将Enviroment设置为Browser,然后运行。然后在选项卡中打开 ddg:some search terms ;)

It works!

关于javascript - nsI 协议(protocol)示例不清楚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24917460/

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