gpt4 book ai didi

javascript - 在没有 contentWindow 的情况下使用 registerProtocolHandler

转载 作者:行者123 更新时间:2023-12-02 17:10:07 26 4
gpt4 key购买 nike

我正在尝试将 hotmail 安装到我的 mailto 处理程序:

Image

这是通过网页范围内的代码完成的:

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/web-content-handler-registrar;1"].getService(Ci.nsIWebContentHandlerRegistrar);

所以我会这样注册:

nsiwchr.registerProtocolHandler('mailto','http://mail.live.com/secure/start?action=compose&to=%s','Live Mail', null);

我使用null,因为我没有contentWindow,但显然你不能为此传递null。因为:

http://mxr.mozilla.org/mozilla-release/source/browser/components/feeds/src/WebContentConverter.js#372

var uri = this._checkAndGetURI(aURIString, aContentWindow);

然后测试:

aContentWindow.location.hostname != uri.host)

所以我想像这样伪造它:

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);

但它抛出了一些完全奇怪的错误:

/*异常:[对象 XPCWrappedNative_NoHelper]*/浏览器控制台抛出:

"[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

这没有任何意义。我想在不使用真正的 contentWindow 的情况下成功欺骗它。

最佳答案

如果没有窗口,您根本无法使用该高级 API(并且您可能无论如何都不想使用它,因为它实际上不会添加处理程序,而是显示一个 UI 通知,首先要求用户添加它;这不仅不是您想要的,而且也不起作用,因为没有 UI 可以首先显示该通知)。

相反,您需要根据省略所有这些安全检查、UI 通知等的实现来创建自己的版本。

基于registerNotificiation,它看起来像这样。

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);

关于javascript - 在没有 contentWindow 的情况下使用 registerProtocolHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24900655/

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