/* Session Backup Script for the Browser Console

   NOTE: 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

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.
*/
ssj = SessionStore.getBrowserState(); // get Current Session State
if(ssj){
	// Set up Save As dialog
	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("JSON Files", "*.json");
	fp.defaultString = "sessionstore-snapshot.json";
	// Call Save As dialog
	fp.open((aResult) => {
		if (aResult == Components.interfaces.nsIFilePicker.returnOK ||
			aResult == Components.interfaces.nsIFilePicker.returnReplace) {
			try {
				IOUtils.writeUTF8(fp.file.path, ssj);
				alert('Look for ' + fp.file.path);
			} catch (err) {
				alert(err);
			}
		} else {
			alert('Okay, not saving');
		}
	});
}