The Tab
instances returned by the tabs
module don't allow us to do such advanced stuff, so it is necessary to get access to the XUL tab object associated with the Add-on SDK tab. Ideally this would be provided to us as a property (xultab
anyone?). Since this is not the case we have to improvise.
The id
property of the Add-on SDK tab enables the tracking of the associated XUL tab. Once we have that, the session history of the tab is at hand.
const { getTabs, getTabId, getBrowserForTab } = require('sdk/tabs/utils');
const { activeTab } = require('sdk/tabs');
function getXULTabFromId(id){
var tabs = getTabs();
return tabs.find(function(tab){
return id == getTabId(tab) ? true : false;
});
}
// Assuming the following code runs inside the onclick handler of a widget
var xultab = getXULTabFromId(activeTab.id);
var browser = getBrowserForTab(xultab);
var sessionHistory = browser.webNavigation.sessionHistory;
var count = sessionHistory.count;
for(var i = 0; i < count; i++){
var entry = sessionHistory.getEntryAtIndex(i, false);
if(sessionHistory.index == i) console.log('***');
console.log(entry.title + ' - ' + entry.URI.spec);
if(sessionHistory.index == i) console.log('***')
}