Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Firefox extension help


ghost's Avatar
0 0

I'm trying to build a Firefox extension that fetches part of the contents of a certain webpage (something like blog posts) and save them as local files.

My plan is to place a little "save" button on either context menu or status bar, enabling users to click and save the contents to their computers.

I know how to fetch and process data from a webpage (Timed challenges, right?) but to tell the truth, I don't know how to create local files out of acquired data. This is where I need help the most.

Suggesting me useful websites, or even keywords to Google is just fine. Regards,

SQuirreL

P.S. I already Googled the following string: "firefox extension download save" but it only showed existing extensions such as DownloadHelper. I also decompiled a few extensions for resources (as many Japanese martial arts encourage to "steal" techniques from seniors) but I failed to read through them because they were too complicated for me.


elmiguel's Avatar
Member
2,795 1

I have only decompiled a few extensions myself (manly to make them work for the lastest FF ver.) They are written in javascript, correct? Don't have time to look now. Anyway, you want to look up on how to read and write files. I will research it at work then I get back to you.


korg's Avatar
Admin from hell
0 0

Google "saving data locally" you'll get some examples real quick. This is easy to do in javascript think of how firefox saves it's temp. files.


ghost's Avatar
0 0

Thanks for the replies. I'll try the suggested Google keywords and research for temp-file creation.

@Moshbat Well, let's just say I didn't have enough brain to think of the Google keywords myself and thus needed suggestions. That's why I posted this thread.


ghost's Avatar
0 0

Well, I found someone has made a way to store data locally by combining Flash and JavaScript. Googled "saving data locally javascript" and it was there! All I had to type in was what I was trying to do…

Anyway, thanks for the assist.


korg's Avatar
Admin from hell
0 0

SQuirreL wrote: Googled "saving data locally javascript" and it was there!

Yes Virginia it's really that easy.


ghost's Avatar
0 0

OK, I've built something like this with help of some snippets available online. Here's the "core.js"

var y = x[20].innerHTML;

var savefile = &savepath;
try {
	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
	alert("Permission to save file was denied.");
}

function save() {
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
		alert("Permission to save file was denied.");
	}
	var file = Components.classes["@mozilla.org/file/local;1"]
	.createInstance(Components.interfaces.nsILocalFile);
	file.initWithPath( savefile );
	if ( file.exists() == false ) {
		alert( "Creating file... " );
		file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
	}
	var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
	.createInstance( Components.interfaces.nsIFileOutputStream );
	/* Open flags 
	 #define PR_RDONLY       0x01
	 #define PR_WRONLY       0x02
	 #define PR_RDWR         0x04
	 #define PR_CREATE_FILE  0x08
	 #define PR_APPEND      0x10
	 #define PR_TRUNCATE     0x20
	 #define PR_SYNC         0x40
	 #define PR_EXCL         0x80
	*/
	 

	outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
	var result = outputStream.write( y, y.length );
	outputStream.close();

}```

The user triggers the save() function via an entry in the context menu.
But it doesn't seem to work. I tried to study how other extensions save their data locally but I couldn't make it. I Googled only to find somebody saying "Local file manipulation with JavaScript is not allowed."
What I'm supposed to do?

ghost's Avatar
0 0

SQuirreL wrote: I know how to fetch and process data from a webpage (Timed challenges, right?) No. Not right.

As for writing to a file, take a look at Mozilla official documentation about File I/O => https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO

For any further help, contact me on my email.


ghost's Avatar
0 0

Boy! That really helped!! I just can't believe I couldn't reach that site myself!! Pretty much thanks ;)

OK, there's another thing. I'm trying to capture part of the page being shown, so I tried to use this way but no use.

	
	var x = document.getElementsByTagName('table');
	
	var string = x[20].innerHTML;

------OMMITION-------

	outputStream.write(string, string.length);  
	
	outputStream.flush();  
	
	outputStream.close();
	
	fileStream.close(); ```

I don't see it working.
When I hardcore the string to "Hello world" it works.
I guess it's not accepting the element from the page.
Could you point me the problem?


EDIT: Self solved.
XUL doesn't seem to support innerHTML methods

ghost's Avatar
0 0

Sorry for posting yet again.

Firefox extension DOES support innerHTML method. My mistake was I should have used "content.document.getElementsBy….." instead of "document.getElementsBy….."

So at last, I'm able to release this add-on (privately) !! Yay!!!

Many thanks to everyone!!