赞
踩
- <?xml version="1.0"?>
- <overlay id="sessionstoreOverlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
- <script type="application/javascript"
- src="chrome://sessionstore/content/overlay.js" />
- <menupopup id="menu_ToolsPopup">
- <menu label="Session Store" insertbefore="sanitizeSeparator">
- <menupopup>
- <menuitem label="Save Session" />
- <menuseparator />
- <!-- Dynamically generated menu items go here -->
- <menuseparator />
- <menuitem label="Clear Sessions" />
- </menupopup>
- </menu>
- </menupopup>
- </overlay>
- var gSessionStore = {
- // Directory to save sessions (nsILocalFile)
- _dir: null,
- // Initialization
- init: function() { },
- // uninitialization
- uninit: function() { },
- // Save session (event handler)
- save: function(event) { },
- // Restore session (event handler)
- restore: function(event) { },
- // Delete session (event handler)
- clear: function(event) { },
- // Dynamically generate menu items (event handler)
- createMenu: function(event) { },
- // Read file
- _readFile: function(aFile) { },
- // Write file
- _writeFile: function(aFile, aData) { },
- };
- window.addEventListener("load", function(){
- gSessionStore.init(); }, false);
- window.addEventListener("unload", function(){
- gSessionStore.uninit(); }, false);
- <menupopup onpopupshowing="gSessionStore.createMenu(event);"
- oncommand="gSessionStore.restore(event);">
- <menuitem label="Save Session" oncommand="gSessionStore.save(event);" />
- <menuseparator />
- <!-- Dynamically generated menu items go here -->
- <menuseparator />
- <menuitem label="Clear Sessions" oncommand="gSessionStore.clear(event);" />
- </menupopup>
- init: function() {
- var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"]
- .getService(Components.interfaces.nsIProperties);
- this._dir = dirSvc.get("ProfD", Components.interfaces.nsILocalFile);
- this._dir.append("sessionstore");
- if (!this._dir.exists())
- this._dir.create(this._dir.DIRECTORY_TYPE, 0700);
- },
- uninit: function() {
- this._dir = null;
- },
- function(event) {
- event.stopPropagation();
- var ss = Components.classes["@mozilla.org/browser/sessionstore;1"]
- .getService(Components.interfaces.nsISessionStore);
- var state = ss.getBrowserState();
- var fileName = "session_" + Date.now() + ".js";
- var file = this._dir.clone();
- file.append(fileName);
- this._writeFile(file, state);
- },
- restore: function(event) {
- var fileName = event.target.getAttribute("fileName");
- var file = this._dir.clone();
- file.append(fileName);
- var state = this._readFile(file);
- var ss = Components.classes["@mozilla.org/browser/sessionstore;1"]
- .getService(Components.interfaces.nsISessionStore);
- ss.setWindowState(window, state, false);
- },
- clear: function(event)
- {
- event.preventBubble();
- var fileEnum = this._dir.directoryEntries;
- while (fileEnum.hasMoreElements()) {
- var file = fileEnum.getNext().QueryInterface(Components.interfaces.nsIFile);
- file.remove(false);
- // debug
- dump("SessionStore> clear: " + file.leafName + "\n");
- }
- },
- _readFile: function(aFile)
- {
- try {
- var stream = Components.classes["@mozilla.org/network/file-input-stream;1"].
- createInstance(Components.interfaces.nsIFileInputStream);
- stream.init(aFile, 0x01, 0, 0);
- var cvstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
- createInstance(Components.interfaces.nsIConverterInputStream);
- cvstream.init(stream, "UTF-8", 1024, Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
- var content = "";
- var data = {};
- while (cvstream.readString(4096, data)) {
- content += data.value;
- }
- cvstream.close();
- return content.replace(/\r\n?/g, "\n");
- }
- catch (ex) { }
- return null;
- },
- _writeFile: function(aFile, aData)
- {
- // init stream
- var stream = Components.classes["@mozilla.org/network/safe-file-output-stream;1"].
- createInstance(Components.interfaces.nsIFileOutputStream);
- stream.init(aFile, 0x02 | 0x08 | 0x20, 0600, 0);
- // convert to UTF-8
- var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].
- createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
- converter.charset = "UTF-8";
- var convertedData = converter.ConvertFromUnicode(aData);
- convertedData += converter.Finish();
- // write and close stream
- stream.write(convertedData, convertedData.length);
- if (stream instanceof Components.interfaces.nsISafeOutputStream) {
- stream.finish();
- } else {
- stream.close();
- }
- },
- createMenu: function(event)
- {
- var menupopup = event.target;
- for (var i = menupopup.childNodes.length - 1; i >= 0; i--) {
- var node = menupopup.childNodes[i];
- if (node.hasAttribute("fileName"))
- menupopup.removeChild(node);
- }
- var fileEnum = this._dir.directoryEntries;
- while (fileEnum.hasMoreElements()) {
- var file = fileEnum.getNext().QueryInterface(Components.interfaces.nsIFile);
- var re = new RegExp("^session_(\\d+)\.js$");
- if (!re.test(file.leafName))
- continue;
- var dateTime = new Date(parseInt(RegExp.$1, 10)).toLocaleString();
- var menuitem = document.createElement("menuitem");
- menuitem.setAttribute("label", dateTime);
- menuitem.setAttribute("fileName", file.leafName);
- menupopup.insertBefore(menuitem, menupopup.firstChild.nextSibling.nextSibling);
- }
- },
名称 | 目的 |
sessionstore-prefs.js | 首选项设置默认值文件。只能放在defaults\preferences目录下 |
prefs.xul | 首选项面板。 |
首选项名称 | 类型 | 值 |
extensions.session store.warnOnClear | Boolean | 删除会话时激活。如果为true,会出现一股对话框以进行确认;如果为false,则立即删除。 |
extensions.session store.replaceTabs | Integer | 存储会话时激活。 0: 离开当前标签页 1: 覆盖当前标签页 2: 总是询问 |
- pref("extensions.sessionstore.warnOnClear", true);
- pref("extensions.sessionstore.replaceTabs", 2);
- <?xml version="1.0"?>
- <?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
- <prefwindow id="sessionstorePrefs" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
- title="Session Store Preferences">
- <prefpane>
- <preferences>
- <preference id="extensions.sessionstore.warnOnClear"
- name="extensions.sessionstore.warnOnClear"
- type="bool" />
- <preference id="extensions.sessionstore.replaceTabs"
- name="extensions.sessionstore.replaceTabs"
- type="int" />
- </preferences>
- <checkbox label="Confirm before clearing sessions"
- preference="extensions.sessionstore.warnOnClear" />
- <groupbox>
- <caption label="When restoring session:" />
- <radiogroup preference="extensions.sessionstore.replaceTabs">
- <radio value="0" label="Keep current tabs" />
- <radio value="1" label="Replace current tabs"/>
- <radio value="2" label="Ask me every time" />
- </radiogroup>
- </groupbox>
- </prefpane>
- </prefwindow>
<em:optionsURL>chrome://sessionstore/content/prefs.xul</em:optionsURL>
在清单28中,我们添加一个首选项以使确认对话框在我们删除会话的时候显示。当我们要用javascript获得首选项的值时,我们利用XPCOM的nsIPrefService接口,使用getBoolPref和getIntPref方法来获取准确的类型。清单28中,我们获取nsIPrefBranch对象来读取和写入所有以extensions.sessionstore.为前缀的首选项,这样我们就能够用getBoolPref方法来获取值了。
清单28(文中无)
alert(foo);
- for (var i in obj) {
- dump(i + " : " + obj[i] + "\n");
- }
- function log(aText) {
- var console = Components.classes["@mozilla.org/consoleservice;1"]
- .getService(Components.interfaces.nsIConsoleService);
- console.logStringMessage(aText);
- }
Mozilla开发中心(MDN)有大量的关于我们在这一节讨论过的nsISessionStore接口的文档。一般的,XPCOM接口的细节(IDL)可以从包含具有可以进行全文本搜索的源代码的Mozilla Cross-Reference获取。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。