Last active
June 22, 2025 23:35
-
-
Save jscher2000/2a389c98aa2f3c70d0ba7bc787a84940 to your computer and use it in GitHub Desktop.
Save Firefox Open Tabs to File (Browser Console script)
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
/* Current Session Open Tab List Exporter Script for the Browser Console | |
BEFORE RUNNING THIS SCRIPT, CHECK THIS SETTING: | |
Type or paste about:config into the address bar and press Enter | |
Click the button promising to be careful | |
In the search box type devt and pause while Firefox filters the list | |
If devtools.chrome.enabled is false, double-click it to toggle to true | |
TO RUN THIS SCRIPT: | |
Paste this entire script into the command line at the bottom of the Browser Console (Windows: Ctrl+Shift+j) | |
Then press Enter to run the script. A save dialog should promptly open. | |
*/ | |
try { | |
var ssj = SessionStore.getBrowserState(); // get Current Session State | |
var oSess = JSON.parse(ssj); | |
} catch(err) { | |
alert('Unable to retrieve session: ' + err); | |
} | |
if(oSess && Object.hasOwn(oSess, 'windows') && oSess.windows.length > 0){ | |
// Generate a string with the format of a bookmark export file | |
var out = '<!DOCTYPE NETSCAPE-Bookmark-file-1>\n<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">\n<TITLE>Bookmarks</TITLE>\n<H1>Bookmarks Menu</H1>\n<DL><p>\n'; | |
const escapeHtmlEntities = function(aText){return (aText || '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''')}; | |
for (var j=0; j<oSess.windows.length; j++){ | |
// Create a folder for each window | |
out += '<DT><H3>Window ' + j + '</H3>\n<DL><p>\n'; | |
// List tabs | |
for (var i=0; i<oSess.windows[j].tabs.length; i++){ | |
var currTab = oSess.windows[j].tabs[i]; | |
// Create a bookmark (link) for each tab | |
if (currTab.entries[currTab.index - 1].url.indexOf('about:') < 0) { | |
out += ' <DT><A HREF="' + currTab.entries[currTab.index - 1].url + '">' + escapeHtmlEntities(currTab.entries[currTab.index - 1].title) + '</A>\n'; | |
} else { | |
out += ' <DT>' + escapeHtmlEntities(currTab.entries[currTab.index - 1].title) + ' {' + currTab.entries[currTab.index - 1].url + '}\n'; | |
} | |
} | |
out += '</DL><p>\n'; | |
} | |
out += '</DL><p>\n</DL>'; | |
// Set up Save As dialog with proposed file name | |
var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker); | |
try { // Fx125+ | |
fp.init(window.browsingContext, 'Open File', Components.interfaces.nsIFilePicker.modeSave); | |
} catch(e) { // Fx124 and earlier | |
fp.init(window, 'Open File', Components.interfaces.nsIFilePicker.modeSave); | |
} | |
fp.appendFilter('HTML Files', '*.html'); | |
var d = new Date(); | |
d.setMinutes(d.getMinutes() - d.getTimezoneOffset()); | |
var dt = d.toISOString().split('.')[0].replace(/-/g, '').replace(/:/g, '').replace(/T/, '_'); | |
fp.defaultString = 'bookmarks-from-open-tabs_' + dt + '.html'; | |
// Call Save As dialog and (unless user cancels) write the file | |
fp.open((aResult) => { | |
if (aResult == Components.interfaces.nsIFilePicker.returnOK || | |
aResult == Components.interfaces.nsIFilePicker.returnReplace) { | |
try { | |
IOUtils.writeUTF8(fp.file.path, out); | |
alert('Look for ' + fp.file.path); | |
} catch (err) { | |
alert(err); | |
} | |
} else { | |
alert('Okay, not saving'); | |
} | |
}); | |
} else { | |
alert('No open windows found in the current session.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment