Trying To Modify javaScript In Firefox Add-On

Hello Fellow Experts

I downloaded and installed a small Firefox extension named "Save My Tabs", but it doesn't quite do what I would like it to do.

At timed intervals it saves out a separate tab-separated text file to a specified directory (both parameters can be changed in the add-on's options) containing the Firefox Window number, Tab number, browser window Title, and URL.  The default file name is:

That's all I want of an extension for the time being.  I know there are other extensions that allow you to manage and save sessions, but I am only interested in something simple and text files suit my needs.  The problem is the layout within the text file isn't what I would choose.  The details of each tab is all on one line and there are no blank lines between them.  I would also prefer a different file naming convention.

Here is the "overlay.js" file from the "\chrome\content\" folder of the extracted XPI file (savemytabs@dmitriy.khudorozhkov.xpi):
var savemytabs = {

	// Shortcuts:
	Cc: Components.classes,
	Ci: Components.interfaces,

	// Preference branch of extension:
	branch: null,

	// Initialization:
	init: function()
		// Initialize preferences:
		var prefservice = this.Cc[";1"].getService(this.Ci.nsIPrefService);
		this.branch = prefservice.getBranch("extensions.savemytabs.");

		// Prepare the first run:;

	// Saving the state of tabs:
	save: function()
		// Check if this is a top-most window:
		var mediator = this.Cc[";1"].getService(this.Ci.nsIWindowMediator);  

		if(window != mediator.getMostRecentWindow("navigator:browser"))
			// It's not - deny saving:;

		var lines = [];

		// Cycle through the windows:
		var w = 1, t = 1;
		var browserEnumerator = mediator.getEnumerator("navigator:browser");  

			var browserWin = browserEnumerator.getNext();
			var tabbrowser = browserWin.gBrowser;

			// Cycle through the tabs:
			var nbrowsers = tabbrowser.browsers.length;

			for(var i = 0; i < nbrowsers; i++)
				var browser = tabbrowser.browsers[i];

				lines.push(("window #" + w + "/tab #" + (t++)) + "\t" + browser.currentURI.spec.replace("\t", " ") + "\t" + browser.contentDocument.title.replace("\t", " "));

			t = 1;

		// Extract current date/time:
		function prepare(num)
			var str = String(num);

			if(str.length < 2)
				str = "0" + str;

			return str;

		var today = new Date();
		var yyyy = today.getFullYear();
		var mm = today.getMonth() + 1;
		var dd = today.getDate();
		var hh = today.getHours();
		var min = today.getMinutes();

		// Get the directory to save to:
		var file = null;
		var dir = this.branch.getCharPref("directory");

			case "Profile":
				file = this.Cc[";1"].getService(this.Ci.nsIProperties).get("ProfD", this.Ci.nsIFile);

			case "Home":
				file = this.Cc[";1"].getService(this.Ci.nsIProperties).get("Home", this.Ci.nsIFile);

				file = this.Cc[";1"].createInstance(this.Ci.nsILocalFile);

		if(file && file.exists())
			file.append("opentabs-" + this.getUserName() + "-" + String(yyyy) + prepare(mm) + prepare(dd) + "-" + prepare(hh) + prepare(min) + ".txt");

			// Create file output stream:
			var foStream = this.Cc[";1"].createInstance(this.Ci.nsIFileOutputStream);

			// Write, create, truncate:
			foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);

			// Be sure to write Unicode:
			var converter = this.Cc[";1"].createInstance(this.Ci.nsIConverterOutputStream);
			converter.init(foStream, "UTF-8", 0, 0);

		// Prepare for the next iteration:;

	next: function()
		var that = this;

		this.branch.getIntPref("period") * 60 * 1000);

	getUserName: function()
		var env = this.Cc[";1"].getService(this.Ci.nsIEnvironment);
		var user = "";

			user = env.get('computername');

				user = env.get('username');

				user = env.get('user');

		return user;

window.addEventListener("load", function() { savemytabs.init(); }, false);

Open in new window

Just so that we are clear, I do not intend to modify this extension for anything other than my own purposes on this computer.  All I want to do is get a better text file layout to suit my own uses.

I have a feeling this may be quite easy, but I don't really know JavaScript other than a general idea of what each element does just by looking through it.  Here's what I tried to change so that it outputs to three separate lines for each tab, and with a blank line between each, like this:

Window #1 <2 spaces here> Tab #1
Window/Page Title
Full URL

Window #1 <2 spaces here> Tab #2
Window/Page Title
Full URL

				lines.push(("window #" + w + "/tab #" + (t++)) + "\t" + browser.currentURI.spec.replace("\t", " ") + "\t" + browser.contentDocument.title.replace("\t", " "));

Open in new window

				lines.push(("Window #" +w + "\s" + "\s" + "Tab #" + (t++)) + "\n" + browser.contentDocument.title.replace("\t", " ") + "\n" browser.currentURI.spec.replace("\t", " ") + "\n");

Open in new window

Further down is where the output file name is created.  This is the original code which outputs files named "opentabs-ComputerName-yyyymmdd-hhmin.txt":
			file.append("opentabs-" + this.getUserName() + "-" + String(yyyy) + prepare(mm) + prepare(dd) + "-" + prepare(hh) + prepare(min) + ".txt");

Open in new window

And here is the code as I attempted to modify it an effort to have the files created as: "Saved-Tabs_yyyy-mm-dd_hh-min.txt":
			file.append("Saved-Tabs_" + String(yyyy) + "-" + prepare(mm) + "-" + prepare(dd) + "_" + prepare(hh)  + "-" + prepare(min)  + ".txt");

Open in new window

I remarked out the line further down because that looks like it forces all the lines back up into one:


After I zipped up the folder again with the modified "overlay.js" and renamed as an *.xpi file, it installed OK in Firefox but for some reason didn't output any files.  I am not sure if this is because of an error in the JavaScript or if there is some issue with the compression algorithm when repacked with WinZip.

I really want to ensure that the modified code is correct before I go off trying to pack the XPI file using different compression methods.

I appreciate your time.

LVL 39
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawel WitkowskiSenior Javascript DeveloperCommented:
In a modified code you have:

				lines.push(("Window #" +w + "\s" + "\s" + "Tab #" + (t++)) + "\n" + browser.contentDocument.title.replace("\t", " ") + "\n" browser.currentURI.spec.replace("\t", " ") + "\n");

Open in new window

You miss a one "+" sign, hopefully now it will work with:

				lines.push(("Window #" +w + "\s" + "\s" + "Tab #" + (t++)) + "\n" + browser.contentDocument.title.replace("\t", " ") + "\n" + browser.currentURI.spec.replace("\t", " ") + "\n");

Open in new window

BillDLAuthor Commented:
Thank you wilq32

I don't know how I didn't see that.  I'll give it a try and post back later.
Dave BaldwinFixer of ProblemsCommented:
Maybe this?
lines.push(("Window #" +w + "  " + "Tab #" + (t++)) + "\r\n" + browser.contentDocument.title.replace("\t", " ") + "\r\n" + browser.currentURI.spec.replace("\t", " ") + "\r\n\r\n");

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BillDLAuthor Commented:

Thank you for spotting my error in the code.  I rectified it, but unfortunately there must have been something in my code as it was which was preventing it from running at the set intervals.  I wasn't sure how to debug it either.  I did try packaging it in various different ways.  Some installed and some didn't, but none worked after installing.

Eventually (with Dave Baldwin's suggested changes) I just renamed the XPI file as a ZIP, opened it in WinZip, navigated to "overlay.js", Right-Clicked it, and opened it in my text editor (which handles Unicode, Linux, DOS, Mac, etc text formats).  After saving and closing the text editor it prompted me to update the file in the archive, which it did.  That package installed properly and worked.

Thank you Dave, your additional  \r\n  instead of just  \n   and the "  " instead of the + "\s" twice  did it.  I don't know why it wasn't working the way I had it, but I'm thankful for the suggested change.

Here's what the original horrible file naming convention:
and layout of results would have been with the original extension installed:
window #1/tab #1	Trying To Modify javaScript In Firefox Add-On: XPI file, overlay.js
window #1/tab #2	Profile | DaveBaldwin
window #1/tab #3	DIBs Places

Open in new window

Bear in mind that it looks OK in the code snippet above, but because each entry is separated by tabs and on one line, in a text editor it all spills over like spaghetti, even in a maximized window.

Here's the new file naming convention, created by my own changes to the code as:
which contains this:
Window #1  Tab #1
Trying To Modify javaScript In Firefox Add-On: XPI file, overlay.js

Window #1  Tab #2
Profile | DaveBaldwin

Window #1  Tab #3
DIBs Places

Open in new window

That's the way I manually make my notes as I do stuff online, so that's perfect for me.  I wish this had been working for me while doing price comparisons over the past couple of weeks for DIY stuff on quite a lot of sites.


Thank you both.
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.