使用没有contentWindow的registerProtocolHandler [英] Use registerProtocolHandler without contentWindow
问题描述
我试图安装hotmail到我的mailto处理程序:
这是通过网页scpoe中的这段代码完成的:
navigator。 registerProtocolHandler('mailto','http://mail.live.com/secure/start?action = compose& to =%s','Live Mail');
在XPCOM中,我们必须使用这个: MXR :: WebContentConverter.js#369)。
所以从搜索github代码我想你可以像这样导入:
var nsiwchr = Cc [ @ mozilla.org / embeddor.implemented /网络的内容处理程序,注册商; 1]的getService(Ci.nsIWebContentHandlerRegistrar)。
所以我会这样注册:
nsiwchr.registerProtocolHandler('mailto','http://mail.live.com/secure/start?action = compose& to =%s','Live Mail',null );
我使用 null
,因为我没有有一个 contentWindow
,但显然你不能通过这个null。因为:
var uri = this._checkAndGetURI(aURIString,aContentWindow);
然后测试:
<$ p $因此,我认为这是一个很好的例子。想像这样伪造:
var fakeContentWindow = {
document:{
baseURIObject:{
asciiHost:mail.live.com,
asciiSpec:http://mail.live.com/secure,
hasRef:true,
host:mail .live.com,
hostPort:mail.live.com,
originCharset:UTF-8,
密码:,
path:/ secure ,
port:-1,
prePath:http://mail.live.com,
ref:,// 369
scheme:http ,
spec:http://mail.live.com/secure,
specIgnoringRef:http://mail.live.com,
userPass:,
用户名:
}
},
位置:{$ b $ hash:,//#369
host:mail.live.com ,
主机名:mail.live.com,
href:http://mail.live.com/secure,
来源:http://mxr.mozilla.org,
路径名:/ secure,
端口:,
协议:http:,
search:
}
};
nsiwchr.registerProtocolHandler('mailto','http://mail.live.com/secure/start?action = compose& to =%s','Live Mail',fakeContentWindow);
但是会抛出一些奇怪的错误:
< b
$ b例外:[object XPCWrappedNative_NoHelper]
* /
浏览器控制台抛出:
<$ c $ 999
SP_writeAsErrorComment /<()scratchpad.js:999
Handler.prototype.process()Promise-backend.js:863
这个Promise-backend.js:742
>
请帮助我成功地欺骗这个婊子,而不使用真正的
contentWindow
。解决方案在没有窗口的情况下,您根本无法使用高级API(而且您可能不想使用它,因为它实际上不会添加处理程序,一个用户界面通知,首先要求用户添加它;这不仅是你想要的,但也不会工作,因为没有用户界面来显示该通知的第一位) / p>
相反,您将希望基于实现省略所有安全检查,UI通知等来创建您自己的版本。
以
registerNotificiation
为基础,它看起来像这样。
var protocolScheme =mailtoorsomething;
var uri = Services.io.newURI(someuri?with_%s_replacement,null,null);
var name =Some Name;
var desc =一些描述;
var protocolHandler = Services.io.getProtocolHandler(protocolScheme);
if(!(protocolHandler instanceof Ci.nsIExternalProtocolHandler)){
throw new Error(无法注册内置协议处理程序);
}
var eps = Cc [@ mozilla.org/uriloader/external-protocol-service;1]。
getService(Ci.nsIExternalProtocolService);
var handlerInfo = eps.getProtocolHandlerInfo(protocolScheme);
var handlers = handlerInfo.possibleApplicationHandlers;
for(let i = 0; i< handlers.length; i ++){
let h = handlers.queryElementAt(i,Ci.nsIWebHandlerApp);
if(h.uriTemplate == uri.spec){
throw new Error(Already registered);
}
}
var handler = Cc [@ mozilla.org/uriloader/web-handler-app;1]。
createInstance(Ci.nsIWebHandlerApp);
handler.name = name;
handler.detailedDescription = desc;
handler.uriTemplate = uri.spec;
handlerInfo.possibleApplicationHandlers.appendElement(handler,false);
handlerInfo.alwaysAskBeforeHandling = false;
handlerInfo.preferredApplicationHandler = handler;
handlerInfo.preferredAction = Ci.nsIHandlerInfo.useHelperApp;
var hs = Cc [@ mozilla.org/uriloader/handler-service;1]。
getService(Ci.nsIHandlerService);
hs.store(handlerInfo);
I'm trying to install hotmail to my mailto handlers:
this is accomplished with this code from webpage scpoe:
navigator.registerProtocolHandler('mailto','http://mail.live.com/secure/start?action=compose&to=%s','Live Mail');
From XPCOM we would have to use this: MXR ::WebContentConverter.js#369) .
So from searching github code I figure you import it like this:
var nsiwchr = Cc["@mozilla.org/embeddor.implemented/web-content-handler-registrar;1"].getService(Ci.nsIWebContentHandlerRegistrar);
So I would register it like this:
nsiwchr.registerProtocolHandler('mailto','http://mail.live.com/secure/start?action=compose&to=%s','Live Mail', null);
I use
null
because I don't have acontentWindow
but apparently you can't pass null for this. Because:var uri = this._checkAndGetURI(aURIString, aContentWindow);
And then it tests:
aContentWindow.location.hostname != uri.host)
So I figured to fake it out like this:
var fakeContentWindow = { document: { baseURIObject: { asciiHost:"mail.live.com", asciiSpec:"http://mail.live.com/secure", hasRef:true, host:"mail.live.com", hostPort:"mail.live.com", originCharset:"UTF-8", password:"", path:"/secure", port:-1, prePath:"http://mail.live.com", ref:"", //369 scheme:"http", spec:"http://mail.live.com/secure", specIgnoringRef:"http://mail.live.com", userPass:"", username:"" } }, location: { hash:"", //#369 host:"mail.live.com", hostname:"mail.live.com", href:"http://mail.live.com/secure", origin:"http://mxr.mozilla.org", pathname:"/secure", port:"", protocol:"http:", search:"" } }; nsiwchr.registerProtocolHandler('mailto','http://mail.live.com/secure/start?action=compose&to=%s','Live Mail', fakeContentWindow);
but it throws some totally weird error:
/* Exception: [object XPCWrappedNative_NoHelper] */ Browser Console throws:
"[object XPCWrappedNative_NoHelper]" scratchpad.js:999 SP_writeAsErrorComment/<() scratchpad.js:999 Handler.prototype.process() Promise-backend.js:863 this.PromiseWalker.walkerLoop() Promise-backend.js:742
Makes no sense.
Please help me to successfully spoof this bitch without using a real
contentWindow
.解决方案You simply cannot use that high-level API without a window (and you probably don't want to use it anyway, because it will not actually add the handler, but show a UI notification that first asks the user to add it; which is not only not what you want, but also won't work because there is no UI to display that notification in the first place).
Instead, you'll want to create your own version based on the implementation that omits all those security checks, UI notifications, etc.
Based on
registerNotificiation
, it would look something like this.var protocolScheme = "mailtoorsomething"; var uri = Services.io.newURI("someuri?with_%s_replacement", null, null); var name = "Some Name"; var desc = "Some description"; var protocolHandler = Services.io.getProtocolHandler(protocolScheme); if (!(protocolHandler instanceof Ci.nsIExternalProtocolHandler)) { throw new Error("Cannot register handler for built-in protocol"); } var eps = Cc["@mozilla.org/uriloader/external-protocol-service;1"]. getService(Ci.nsIExternalProtocolService); var handlerInfo = eps.getProtocolHandlerInfo(protocolScheme); var handlers = handlerInfo.possibleApplicationHandlers; for (let i = 0; i < handlers.length; i++) { let h = handlers.queryElementAt(i, Ci.nsIWebHandlerApp); if (h.uriTemplate == uri.spec) { throw new Error("Already registered"); } } var handler = Cc["@mozilla.org/uriloader/web-handler-app;1"]. createInstance(Ci.nsIWebHandlerApp); handler.name = name; handler.detailedDescription = desc; handler.uriTemplate = uri.spec; handlerInfo.possibleApplicationHandlers.appendElement(handler, false); handlerInfo.alwaysAskBeforeHandling = false; handlerInfo.preferredApplicationHandler = handler; handlerInfo.preferredAction = Ci.nsIHandlerInfo.useHelperApp; var hs = Cc["@mozilla.org/uriloader/handler-service;1"]. getService(Ci.nsIHandlerService); hs.store(handlerInfo);
这篇关于使用没有contentWindow的registerProtocolHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!