- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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;
}
});
如果我们的组件是 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
;)
关于javascript - nsI 协议(protocol)示例不清楚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24917460/
internal protocol Reducer { associatedtype S : BaseState associatedtype A : BaseActi
我在考虑我的应用程序中的验证检查,我认为在任何模型上调用 ValidatorFactory,实现 Validee,这意味着说哪个类负责 ValidatorCreation 听起来不错。但是下面的代码不
我已经定义了 2 个协议(protocol)。我需要第一个 (NameProtocol) 来执行 Equatable 协议(protocol)。而另一个类 (BuilderProtocol) 有一个返
在上传方面,WebDAV 协议(protocol)在哪些方面优于 HTTP 协议(protocol)。 Socket Upload 协议(protocol)和 WebDav Upload 协议(pro
是否可以在任何版本的 Swift 中扩展具有混合类/协议(protocol)类型约束的协议(protocol)?例如,仅当 Self 是 UIViewController 的子类并且符合 Protoc
我有一个协议(protocol) (ProtocolA),其中包含符合第二个协议(protocol) (ProtocolB) 的单个属性。 public protocol ProtocolA {
NSObject 协议(protocol)带有常用的协议(protocol)模板,但它似乎并不是协议(protocol)实际实现所必需的。将其排除在外似乎完全没有任何改变。那么,协议(protocol
我想根据这两种协议(protocol)的一般特征(例如开销(数据包)、安全性、信息建模和可靠性)来比较 OPC UA 和 MQTT。我在哪里可以找到每个协议(protocol)的开销和其他特性的一些示
使用 Swift 4,我正在尝试编写一个自定义协议(protocol),它提供对 @objc 协议(protocol)的一致性。 一些代码 更具体地说,我有一个自定义协议(protocol) Sear
我想定义一个在 Viper 架构中使用的协议(protocol),以使用具有弱属性的协议(protocol)在 Viper 组件之间建立连接,但我收到以下错误消息: 'weak' may only b
我在同一个网络中有 3 个 docker 容器: 存储 (golang) - 它提供了用于上传视频文件的 API。 主播 (nginx) - 它流式传输上传的文件 反向代理 (姑且称之为代理) 我有
我打算在我的项目中使用 php socket。它需要用户登录才能根据 session 填充内容。所以我的问题是,TCP/IP 协议(protocol)也像 HTTP 协议(protocol)一样为每个
目前,我的网站有两个版本。一种带有 https://-证书,一种没有。我想将我网站的 http 版本上的所有用户 301 重定向到我网站的 https://版本。 这似乎不可能,因为创建重定向将导致重
目前,我的网站有两个版本。一种带有 https://-证书,一种没有。我想将我网站的 http 版本上的所有用户 301 重定向到我网站的 https://版本。 这似乎不可能,因为创建重定向将导致重
我有一个 Swift View Controller ,它定义了一个在 Objective-C View Controller 中应该遵循的协议(protocol): ChildViewControl
我在客户那里有数百个硬件设备,需要通过telnet接口(interface)发送HTTP数据。 目标是等待数据的 Apache 2 Web 服务器和 PHP 脚本。 这已经可以正常工作了,但是我们发现
我发现如果我创建一个这样的协议(protocol): protocol MyProtocol { } 我不能这样做: weak var myVar: MyProtocol? 我找到了解决这个问题的方法
Xcode 基于模板生成了这个头文件: // this file is XYZAppDelegate.h #import @interface XYZAppDelegate : UIRespond
我在 github 中有一个公开的存储库,我正在开发一个开源应用程序,用于制作产品目录和小型 cms 内容。 我还有一个私有(private)仓库(不托管在github),它是在托管在github的开
您好,我想让别人看到私有(private) repo 代码,但不想公开我的 repo ,也不希望他们有能力更改内容。这可能吗?我查看了网站的“管理”部分,但没有找到合适的内容。谢谢大家。 最佳答案 据
我是一名优秀的程序员,十分优秀!