我目前正在制作一个firefox扩展程序,它允许您选择要在启动后重新打开的选项卡,而不是打开所有内容(它打开:sessionrestore页面)。我的JS代码如下所示:
编辑:使用工作版更新的代码
chrome.manifest用于
component {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} components/myextension.js
contract @example.com/MyExtension;1 {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
category profile-after-change MyExtension @example.com/MyExtension;1
组件/ myextension.js
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
const CI = Components.interfaces, CC = Components.classes, CR = Components.results;
// class declaration
function MyExtension () {}
MyExtension.prototype = {
classDescription: "My Firefox Extension",
classID: Components.ID("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"),
contractID: "@example.com/MyExtension;1",
QueryInterface: XPCOMUtils.generateQI([CI.nsIObserver]),
// add to category manager
_xpcom_categories: [{
category: "profile-after-change"
}],
observe: function(aSubject, aTopic, aData)
{
var obs = CC["@mozilla.org/observer-service;1"].getService(CI.nsIObserverService);
switch (aTopic)
{
case "profile-after-change":
obs.addObserver(this, "sessionstore-state-read", false);
break;
case "sessionstore-state-read":
aSubject.QueryInterface(CI.nsISupportsString);
let newData = { windows: [{ tabs: [{ entries: [{
url: "about:sessionrestore",
formdata: { "#sessionData": aSubject.data }
}] }] }] };
aSubject.data = JSON.stringify(newData);
break;
default:
throw Components.Exception("Unknown topic: " + aTopic);
}
}
};
if (XPCOMUtils.generateNSGetFactory)
var NSGetFactory = XPCOMUtils.generateNSGetFactory([EmbarrassedObserver]);
else
var NSGetModule = XPCOMUtils.generateNSGetModule([EmbarrassedObserver]);
它适用于sessionstore-state-write(注释行),但我想用sessionstore-state-read事件来做,因为我不想编辑它写入磁盘的内容。问题是,当我改变读取事件时,我的观察者停止工作。我的猜测是我注册了观察者太晚了(在会话被读取之后),所以我试图尽快注册它(当首次加载扩展时,注意窗口加载事件),但它仍然无法工作。知道如何让它运作吗?