JavaScript to save MAFF in Firefox

I am experimenting with iMacros (Firefox add-on) to automate as task that Firefox will do. I simply want to save the current page with the MAFF extension. The JavaScript that the iMacros forum has lead me to, is this:

// I stuck these variable in just to try something.
var doc = "http://www.traderjoes.com";
var file = "C:\\Export\\Test.maff";
var format = "MAFF";
// I stuck these variable in just to try something.

var MafObjects = {};
Components.utils.import("resource://maf/modules/mafObjects.jsm",
                        MafObjects);
var jobListener = {
  onJobComplete: function(aJob, aResult) {
    if (!Components.isSuccessCode(aResult)) {
      // An error occurred
    } else {
      // The save operation completed successfully
    }
  },
  onJobProgressChange: function(aJob, aWebProgress, aRequest,
                                aCurSelfProgress,
                                aMaxSelfProgress,
                                aCurTotalProgress,
                                aMaxTotalProgress) { },
  onStatusChange: function(aWebProgress, aRequest, aStatus,
                           aMessage) { }
};
var saveJob = new MafObjects.SaveJob(jobListener);
saveJob.addJobFromDocument(doc, file, format);
saveJob.start();

I was only getting an error on line 26 because this was sample code. With the little JavaScript I know I tried to add some variables on the lines before the code starts. The thing is that when I try to search for syntax example for the method .addJobFromDocument I don’t find much, just like two results. Is this a method of JavaScript? Usually with things from the DOM you will get a great deal of information on them.

Does anybody know a way of automating the save of MAFF of the current open tab in Firefox and then closing the browser? iMacros was something I came to and glad to see it features but really I just want to automate from a command line the saving of a URL as a MAFF archive The doc (that I got from iMacros forum) also had these code snippets but I don’t have much idea how to use them. Thanks

var fileUri = Components.
              classes["@mozilla.org/network/io-service;1"].
              getService(Components.interfaces.nsIIOService).
              newFileURI(file);
var persistObject = new MafObjects.MafArchivePersist(null, format);
persistObject.saveDocument(doc, fileUri);

Also:

var doc = gBrowser.contentDocument;
var file = Components.classes["@mozilla.org/file/local;1"].
           createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("C:\\My Documents\\Test.maff");
var format = "TypeMAFF";

Okay, I’m not really sure how to answer this because I don’t know anything about iMacros or MAFF. I’m only posting at all to let you know what I recognise, in case it’s useful.

addJobFromDocument is not a native JS method, it’s a custom method that the iMacros extension defines.

The snippets you posted are XPCOM code. XPCOM is the object model that Firefox is written with (think of it as the DOM of Firefox’s scripting environment). It relates to reading and writing files, but you can only use that code within the context of a Firefox extension.

I’m guessing then that iMacros allows you to run snippets of Firefox code in order to perform specific tasks. Sounds like a security risk to me, but anyway :smile:

Where did you find all that code? If I could see the same forum post you read, I might be able to help you make sense of it.

Thanks, sure and I should of included it in the first place. Here is the post it is from Original Link and the actual forum I was on was this iMacros What you are saying make absolute sense because what I am working with is what you imagine a way to run snippets to perform specific tasks. I am not worried about the security because of the context and environment, but thanks for also passing that information I will use knowledge to be safe. I am glad you explain XPCOM because via all the searching I am doing I saw one little glimpse about that and I did not know where it fits in. Whether I can accomplish this or not I am glad to learn about these things as I build my knowledge of JavaScript and things like browsers. Thank you so much!!!

Your first example is pretty much working code, except that your definition of “doc” isn’t valid. That doc variable has to be a reference to a document object, not a string URL.

I’m trying to understand how iMacros works so I can test it directly. Where did you define your JS code? Like where did you physically put it so it could run as a macro?

This page gives an example of how to use it. I tried to put it in my own function. because I saw there code was for ie. Thanks

If you are writing your own extension, you could try

.......
	write_file	:	function(file_name, content_string) {
"use strict";
					var {Cc, Ci} = require("chrome");
					var file = Cc["@mozilla.org/file/directory_service;1"]
									.getService(Ci.nsIProperties)
									.get("ProfD", Ci.nsIFile);
					file.append(file_name);
//					file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); 
//  octal literals and octal escape sequences are deprecated
					file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0666", 8));
					var foStream = Cc["@mozilla.org/network/file-output-stream;1"]
									.createInstance(Ci.nsIFileOutputStream);
								// use 0x02 | 0x10 to open file for appending.
//					foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0); 
//  octal literals and octal escape sequences are deprecated
					foStream.init(file, 0x02 | 0x08 | 0x20, parseInt("0666", 8), 0);
					var converter = Cc["@mozilla.org/intl/converter-output-stream;1"]
								.createInstance(Ci.nsIConverterOutputStream);
					converter.init(foStream, "UTF-8", 0, 0);
					converter.writeString(content_string);
					converter.close();
				},
.......

Thanks, hi I am not writing my own extension, I am trying to place correct JavaScript according to specification for a Firefox add-on. This is supposed to extended it allowing the save of MAFF format. Thanks for you code I will try first to see if I can understand (my JavaScript knowledge is not this level yet) and see where I can apply it. Thanks a lot!

I’ve been trying to get my head around this, but I can’t see anywhere that you can call XPCOM code directly.

The iMacros scripting environment doesn’t have that level of access, as far as I can figure out, it can only run JavaScript within the scope of a webpage, like a bookmarklet.

I might be wrong about that, I just can’t see where the code would be allowed to run – I can’t see anything in the macros setup that would allow you to inject that code in the right place.

So sorry, I’m not saying it can’t be done, but I can’t figure out how.

Well thanks a lot any way. I have started a new approach made some initial progress and have different questions now. I will post them in another question. Just what you said about the Document Object Model pointed me in the correct direction. Thank you so much!

The clearest sense I can get from the posts you linked to, is that the posters weren’t looking for code to run in a macro, they were looking for a way to edit the extension code itself, effectively to fix iMacros so that its save page option works better.

Ultimately, if you want this level of access to Firefox, you probably need to look at extension development. I don’t know if that’s of interest, but it’s not as intimidating as it seems at first :slight_smile: There’s a good ebook called “Build Your Own Firefox Extension” you might try (> http://practicalaction.org/docs/about_us/byofirefoxpdf1.pdf and I know it’s good, cos I wrote it!).

[quote]The snippets you posted are XPCOM code. XPCOM is the object model that
Firefox is written with (think of it as the DOM of Firefox’s scripting
environment).[/quote]
Thanks for the interest you put into this, this is such a great forum. I have solved it, by doing exactly what you say. I looked at material that got me up to speed with what Mozilla add-ons are, and how they are made. Then I jumped in and added the MAFF functionality. Like I said being rusty with JavaScript I had to first start writing some again, and this code was way over my head, so it pushed me to learn some more, that part was also great. But what I really would like to know is how relative are the (interesting) concepts that Mozilla relies on to do there add-ons and things. Concepts like XUL (learned a little for this project) very interesting, but made me ask is anybody paying much attention to them except Mozilla? This works really good now, but ultimately I would like to develop a program that is lightweight and does not even need to open a browser grab an archive of a webpage. I know Wget and things like that do it but none as qualified as MAFF. It was you mentioning XPCOM that led me to the right place! Since XPCOM is Mozilla’s technology I began researching what it can accomplish. I have been reading about it, and it seems as though, XPCOM via C++ would be the place to build the program want. I am currently learning C++ right now, so that is also great! Thank you so much!

XUL is only useful in Firefox extensions, it’s not used anywhere else. Even FirefoxOS (their mobile platform) doesn’t use XUL. So there’s no future in it outside of Firefox extension development.

You can run C++ inside Firefox extensions, but you don’t need to – everything you want to do here can be done in JavaScript. You only need C++ if you want to extend XPCOM itself.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.