根据@nmaiers 的帖子,这是你的做法:
如果 mime 类型已经存在,您就是这样做的。如果它不存在我不知道如何添加它,可能是一些注册功能。
出于某种原因,我的torrents 的类型是application/x-download,我不知道为什么。如果你想知道我是如何计算出来的,我会告诉你的。因此,在下面的示例中,我将其用作文件类型。
当我们console.log(wrappedHandlerInfo) 时,我们看到它看起来像这样:
所以现在让我们枚举所有应用程序处理程序(我从这里得到这个:MXR :: gApplicationsPane,如果是.type == 'application/x-download' let'sbreak`,那么我们就可以使用那个对象了。
var handlerService = Cc['@mozilla.org/uriloader/handler-service;1'].getService(Ci.nsIHandlerService);
var listOfWrappedHandlers = handlerService.enumerate();
var i = 0;
while (listOfWrappedHandlers.hasMoreElements()) {
var wrappedHandlerInfo = listOfWrappedHandlers.getNext().QueryInterface(Ci.nsIHandlerInfo);
console.log(i, 'handler for', wrappedHandlerInfo.type, wrappedHandlerInfo);
if (wrappedHandlerInfo.type == 'application/x-download') {
break;
}
i++;
}
console.log('Listed ', i, ' handlers');
console.log('wrappedHandlerInfo=', wrappedHandlerInfo); //should be the application/x-download one as we broke the loop once it found that
现在我们必须设置它的属性然后保存它。
// Change and save mime handler settings.
wrappedHandlerInfo.alwaysAskBeforeHandling = false;
wrappedHandlerInfo.preferredAction = Ci.nsIHandlerInfo.handleInternally;
handlerService.store(wrappedHandlerInfo);
不过,我也不确定如何更改这些属性,也许 @nmaier 可以就此提供建议。
我们在MXR :: nsIHandlerService.idl #L69 看到这家商店这样做:
69 * Save the preferred action, preferred handler, possible handlers, and
70 * always ask properties of the given handler info object to the datastore.
71 * Updates an existing record or creates a new one if necessary.
72 *
73 * Note: if preferred action is undefined or invalid, then we assume
74 * the default value nsIHandlerInfo::useHelperApp.
75 *
76 * @param aHandlerInfo the handler info object
77 */
78 void store(in nsIHandlerInfo aHandlerInfo);
另一种方式
好的,我找到了一个更好的方法,这样你就不需要循环查找处理程序了。
这样做:
var mimeService = Cc['@mozilla.org/mime;1'].getService(Ci.nsIMIMEService);
var CONTENT_TYPE = ''; //'application/x-download'; can leave this blank
var TYPE_EXTENSION = 'torrent';
var handlerInfo = mimeService.getFromTypeAndExtension(CONTENT_TYPE, TYPE_EXTENSION);
console.info('handlerInfo:', handlerInfo); //http://i.imgur.com/dUKox24.png
// Change and save mime handler settings.
handlerInfo.alwaysAskBeforeHandling = false;
handlerInfo.preferredAction = Ci.nsIHandlerInfo.handleInternally;
handlerService.store(handlerInfo);
这个handlerInfo 对象略有不同,因为它有一个primaryExtension 属性来保存种子。
两种方式都有问题
这两种方式的问题是,如果 mime 类型不存在,你必须以某种方式注册它,我不知道如何。可能使用了 mime 服务和一些注册功能。
2014 年 8 月 3 日更新
我想我找到了解决上面提到的问题的方法(两种方式都有问题)。
MXR :: addPossibleApplicationHandler
235 addPossibleApplicationHandler: function(aNewHandler) {
236 var possibleApps = this.possibleApplicationHandlers.enumerate();
237 while (possibleApps.hasMoreElements()) {
238 if (possibleApps.getNext().equals(aNewHandler))
239 return;
240 }
241 this.possibleApplicationHandlers.appendElement(aNewHandler, false);
242 },
243
这是addPossibleApplicationHandler 的代码,我们可能只需要复制它并以某种方式进行编辑。
2014 年 8 月 3 日更新
好的,这就是添加协议处理程序的方法(它只添加了一个 nsIWebAppHandler 但我肯定会添加一个本地含义的 nsIAppHandler 它应该是相似的,只是不需要 uri 参数:
https://gist.github.com/Noitidart/2faaac70c62bc13e7773#add-a-handler-to-a-protocol
nsIMIMEService: MXR :: nsIMMEService.idl 中可用功能的信息