Skip to content

Instantly share code, notes, and snippets.

@gardejo
Forked from mooz/hoge.js
Last active September 30, 2015 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gardejo/cdd9cb656d58f816bb71 to your computer and use it in GitHub Desktop.
Save gardejo/cdd9cb656d58f816bb71 to your computer and use it in GitHub Desktop.
/**
* List session history of the current (selected) tab.
* CAVEAT: `nsIFaviconService#getFaviconForPage()` is obsoleted since
* Firefox 22.0. Use `mozIAsyncFavicons#getFaviconURLForPage()` instead.
* CAVEAT: Do not use strict mode.
* @see https://gist.github.com/mooz/284632
* @see http://malblue.tumblr.com/post/349001250/tips-japanese-keysnail-github
* @see https://gist.github.com/958/895703
* @todo How do we get timestamp on a page from session history?
* @todo nsISHistory#PurgeHistory()
*/
ext.add('list-tab-history', function() {
// 'use strict'; // DO NOT USE STRICT MODE
/**
* An interface for accessing the favicon service asynchronously.
* @const {mozIAsyncFavicons}
*/
const faviconInterface = PlacesUtils.favicons; // It is the same as below.
// const faviconInterface = Cc['@mozilla.org/browser/favicon-service;1'].
// getService(Ci.nsIFaviconService).
// QueryInterface(Ci.mozIAsyncFavicons);
/**
* URI of default favicon.
* @const {String}
*/
const defaultFavicon = 'chrome://mozapps/skin/places/defaultFavicon.png';
/**
* An interface to the primary properties of the Session History component.
* @type {nsISHistory}
*/
let sessionHistory = gBrowser.webNavigation.sessionHistory;
/**
* Generate a generator-iterator for session history.
* @returns {Object} A generator-iterator for session history.
* @see http://d.hatena.ne.jp/teramako/20130928/p1
*/
function* generateHistory() {
for (let i = 0; i < sessionHistory.count; i++) {
let entry = sessionHistory.getEntryAtIndex(i, false);
yield {
title : entry.title,
uri : entry.URI.spec,
visited : '', // TODO: toAgo(timestamp) cf. history.ks.js
index : i,
};
}
}
/**
* Get URL of favicon for the specified page or default favicon.
* @param {String} uri URI of the page whose favicon's URL we're looking up.
* @param {Function} callback Once we have found the favicon's URL, we
* invoke this callback.
*/
function getFaviconPath(uri, callback) {
faviconInterface.getFaviconURLForPage(
NetUtil.newURI(uri, null, null),
{
onComplete : function(aURI, aDataLen, aData, aMimeType) {
callback( aURI ? aURI.spec : defaultFavicon );
},
}
);
}
/**
* Show prompt selector for session history.
* @param {Array.<Array>} selectee A collection of session history.
*/
function showSelector(selectee) {
prompt.selector({
collection : selectee,
flags : [ICON|IGNORE, 0, 0, IGNORE, IGNORE|HIDDEN],
header : ['Title', 'URL', 'Last visited'],
width : [40, 50, 10],
style : [
style.prompt.description,
style.prompt.url,
style.prompt.description,
],
initialIndex : sessionHistory.index,
callback : function(i) {
if (i >= 0)
gBrowser.webNavigation.gotoIndex(selectee[i][4]);
},
message : M({
en : 'Select history in tab',
ja : 'タブの履歴を選択',
}),
});
}
let history = generateHistory(), done, value;
// cf. http://d.hatena.ne.jp/mooz/20100729/p1
if ( { done, value } = history.next(), done )
return void display.echoStatusBar( M({
en : 'Tab history not exist',
ja : 'タブの履歴がありません',
}), 2000 );
let selectee = [];
getFaviconPath(value.uri, function(favicon) {
selectee.push
([favicon, value.title, value.uri, value.visited, value.index]);
if ( { done, value } = history.next(), done )
showSelector(selectee);
else
// CAVEAT: Strict mode brings the exception below.
// TypeError: 'caller', 'callee' and 'arguments' properties may not
// be accessed on strict mode functions or the arguments objects
// for calls to them
getFaviconPath(value.uri, arguments.callee);
});
}, M({
en : 'List history (session) in the current (selected) tab',
ja : '現在のタブの履歴(セッション)を表示',
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment