Created
June 11, 2009 02:59
-
-
Save andyed/127671 to your computer and use it in GitHub Desktop.
Firefox SessionStore.js -> Tab URL List w/ Favicons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<title>Untitled Document</title> | |
<script type="text/javascript"> | |
var initialState; | |
const Cc = Components.classes; | |
const Ci = Components.interfaces; | |
const Cr = Components.results; | |
function debug(aMessage) { | |
try { | |
var objects = []; | |
objects.push.apply(objects, arguments); | |
Firebug.Console.logFormatted(objects, | |
TabWatcher.getContextByWindow | |
(content.document.defaultView.wrappedJSObject)); | |
} | |
catch (e) { | |
} | |
var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService); | |
if (aMessage === "") consoleService.logStringMessage("(empty string)"); | |
else if (aMessage != null) consoleService.logStringMessage(aMessage.toString()); | |
else consoleService.logStringMessage("null"); | |
} | |
function readFile(aFile) { | |
try { | |
var stream = Cc["@mozilla.org/network/file-input-stream;1"]. | |
createInstance(Ci.nsIFileInputStream); | |
stream.init(aFile, 0x01, 0, 0); | |
var cvstream = Cc["@mozilla.org/intl/converter-input-stream;1"]. | |
createInstance(Ci.nsIConverterInputStream); | |
cvstream.init(stream, "UTF-8", 1024, Ci.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) { Components.utils.reportError(ex); } | |
return null; | |
} | |
function readStateFile(aFile) { | |
var stateString = Cc["@mozilla.org/supports-string;1"]. | |
createInstance(Ci.nsISupportsString); | |
stateString.data = readFile(aFile) || ""; | |
var observerService = Cc["@mozilla.org/observer-service;1"]. | |
getService(Ci.nsIObserverService); | |
observerService.notifyObservers(stateString, "sessionstore-state-read", ""); | |
return stateString.data; | |
} | |
function init() { | |
// get file references | |
var dirService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties); | |
var sessionFile = dirService.get("ProfD", Ci.nsILocalFile); | |
sessionFile.append("sessionstore.js"); | |
// get string containing session state | |
this._iniString = readStateFile(sessionFile); | |
if (!this._iniString) | |
return; | |
var s = new Components.utils.Sandbox("about:blank"); | |
initialState = Components.utils.evalInSandbox("(" + this._iniString + ")", s); | |
console.log(initialState); | |
render(initialState); | |
} | |
function render(sessionState) { | |
var str=''; | |
var curTab; | |
// Loop over tabs in sessionStore | |
for(var i=0;i<sessionState.windows[0].tabs.length;i++) { | |
var curTab = sessionState.windows[0].tabs[i]; | |
var curUrl = sessionState.windows[0].tabs[i].entries[curTab.index-1]; | |
str += "<a href='"+ curUrl.url + "'>" + "<img src='" + favicon_for(curUrl.url) + "' border=0/> "; | |
str += curUrl.title + "</a><br/>"; | |
} | |
document.getElementById("content") .innerHTML = str; | |
} | |
var favicon_service = Components.classes["@mozilla.org/browser/favicon-service;1"] | |
.getService(Components.interfaces.nsIFaviconService); | |
/** | |
* Creates an nsURI for a URL, needed for interacting with FF services. | |
*/ | |
function uri(url) { | |
try { | |
var ioService = Components.classes["@mozilla.org/network/io-service;1"] | |
.getService(Components.interfaces.nsIIOService); | |
return ioService.newURI(url, null, null); | |
} catch(e) { | |
return null; | |
} | |
} | |
function favicon_for(url) { | |
var favicon_uri = uri(url); | |
return favicon_uri ? | |
favicon_service.getFaviconImageForPage(uri(url)).spec : ""; | |
} | |
</script> | |
</head> | |
<body onload="init();"> | |
<div id="content"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment