Firefox extension help
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.
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.
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?
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.
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