[MacEd] Re: macro to save web page as "web page, complete" ?

Biju G C bijumaillist at yahoo.com
Sat Nov 1 18:23:37 EST 2003


Sorry for this big mail....
While reply please remove irrelevant parts ....

 --- Valter Mazzola wrote: 
> how i can write a macro to load a webpage and then
> save in a dir  as "web page, complete" ?

I think the best way to try this is to study how this functionality 
is already done in Firebird/Mozilla.

Another thing you need to know is how to load a web page to browser.

see JS at first
http://mozdev.org/pipermail/macroeditor/2003-October/000002.html

now to load a webpage do

  getBrowserWin().location = "http://mozilla.org";

or try

 var browserwin = getBrowserWin();
 browserwin.location = "http://mozilla.org";




To study the default functionality you may 
have to open the jar files in the chrome dir.
you can unzip the entire jar then look thru file.
or use emacs editor to browse thru jar.
or use http://7-zip.org to open up and browse thru jar.
in 7-zip you can also set it to the editor of your choice


now go thru

browser.jar!\content\browser\browser.xul

you will see
<command id="Browser:SavePage" oncommand="saveDocument(window._content.document);"/>

as i got the function name
I will go back to browser (Mozilla/Firebird)

from menu Tools/Macro/execute...

then do 

 alert(saveDocument);

it will display the function body
or do

  prompt(saveDocument,saveDocument);

you will be able to copy output from the prompt text box
warning !! as the function is multi line you may see blank
but ctrl-C will copy everything
if it doesnt try ctrl-A, ctrl-C

past it to an editor/notepad
you get

=====

function saveDocument(aDocument, aSkipPrompt) {
    if (aDocument) {
        saveInternal(aDocument.location.href, aDocument, false, aSkipPrompt);
    } else {
        saveInternal(_content.location.href, null, false, aSkipPrompt);
    }
}


=====

do 
 
  prompt(saveInternal,saveInternal);

you get

=====

function saveInternal(aURL, aDocument, aFileName, aFilePickerTitleKey, aShouldBypassCache,
aSkipPrompt) {
    if (aSkipPrompt == undefined) {
        aSkipPrompt = false;
    }
    var data = {url:aURL, fileName:aFileName, filePickerTitle:aFilePickerTitleKey,
document:aDocument, bypassCache:aShouldBypassCache, window:window};
    var sniffer = new nsHeaderSniffer(aURL, foundHeaderInfo, data, aSkipPrompt);
}


=====

oops... there is mistake in function call, (arguments dont match!!!)
we found a bug  !!!!


now do 
 
  prompt(nsHeaderSniffer,nsHeaderSniffer);

you get

=====




function nsHeaderSniffer(aURL, aCallback, aData, aSkipPrompt) {
    this.mCallback = aCallback;
    this.mData = aData;
    this.mSkipPrompt = aSkipPrompt;
    this.uri = makeURL(aURL);
    this.linkChecker =
Components.classes['@mozilla.org/network/urichecker;1'].createInstance().QueryInterface(Components.interfaces.nsIURIChecker);
    var flags;
    if (aData.bypassCache) {
        flags = Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
    } else {
        flags = Components.interfaces.nsIRequest.LOAD_FROM_CACHE;
    }
    this.linkChecker.asyncCheckURI(aURL, this, null, flags);
}




=====

now you found foundHeaderInfo in following line of saveInternal() is a call back function

    var sniffer = new nsHeaderSniffer(aURL, foundHeaderInfo, data, aSkipPrompt);

so try

  prompt(foundHeaderInfo,foundHeaderInfo);

=====

function foundHeaderInfo(aSniffer, aData, aSkipPrompt) {
    var contentType = aSniffer.contentType;
    var contentEncodingType = aSniffer.contentEncodingType;
    var shouldDecode = false;
    try {
        const helperAppService =
Components.classes['@mozilla.org/uriloader/external-helper-app-service;1'].getService(Components.interfaces.nsIExternalHelperAppService);
        var url = aSniffer.uri.QueryInterface(Components.interfaces.nsIURL);
        var urlExt = url.fileExtension;
        if (helperAppService.applyDecodingForType(contentType) &&
            (!urlExt || helperAppService.applyDecodingForExtension(urlExt))) {
            shouldDecode = true;
        }
    } catch (e) {
    }
    var isDocument = aData.document != null && isDocumentType(contentType);
    if (!isDocument && !shouldDecode && contentEncodingType) {
        contentType = contentEncodingType;
    }
    var defaultFileName = getDefaultFileName(aData.fileName, aSniffer.suggestedFileName,
aSniffer.uri, aData.document);
    var defaultExtension = getDefaultExtension(defaultFileName, aSniffer.uri, contentType);
    var defaultString = getNormalizedLeafName(defaultFileName, defaultExtension);
    const prefSvcContractID = "@mozilla.org/preferences-service;1";
    const prefSvcIID = Components.interfaces.nsIPrefService;
    var prefs =
Components.classes[prefSvcContractID].getService(prefSvcIID).getBranch("browser.download.");
    const nsILocalFile = Components.interfaces.nsILocalFile;
    const lfContractID = "@mozilla.org/file/local;1";
    var dir = null;
    try {
        dir = prefs.getComplexValue("defaultFolder", nsILocalFile);
    } catch (e) {
    }
    var filterIndex = 0;
    var file;
    if (!aSkipPrompt || !dir) {
        try {
            dir = prefs.getComplexValue("lastDir", nsILocalFile);
        } catch (e) {
            var fileLocator =
Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties);

            function getDesktopKey() {
                return "DeskV";
                return "Home";
            }

            dir = fileLocator.get(getDesktopKey(), Components.interfaces.nsILocalFile);
        }
        var fp = makeFilePicker();
        var titleKey = aData.filePickerTitle || "SaveLinkTitle";
        var bundle = getStringBundle();
        fp.init(window, bundle.GetStringFromName(titleKey),
Components.interfaces.nsIFilePicker.modeSave);
        appendFiltersForContentType(fp, contentType, isDocument ? MODE_COMPLETE : MODE_FILEONLY);
        if (dir) {
            fp.displayDirectory = dir;
        }
        if (isDocument) {
            try {
                fp.filterIndex = prefs.getIntPref("save_converter_index");
            } catch (e) {
            }
        }
        fp.defaultExtension = defaultExtension;
        fp.defaultString = defaultString;
        if (fp.show() == Components.interfaces.nsIFilePicker.returnCancel ||
            !fp.file) {
            return;
        }
        var useDownloadDir = false;
        try {
            useDownloadDir = prefs.getBoolPref("useDownloadDir");
        } catch (ex) {
        }
        var directory = fp.file.parent.QueryInterface(nsILocalFile);
        prefs.setComplexValue("lastDir", nsILocalFile, directory);
        fp.file.leafName = validateFileName(fp.file.leafName);
        filterIndex = fp.filterIndex;
        file = fp.file;
    } else {
        dir.append(defaultString);
        file = dir;
        while (file.exists()) {
            var parts = /.+-(\d+)(\..*)?$/.exec(file.leafName);
            if (parts) {
                file.leafName = file.leafName.replace(/((\d+)\.)/, (function (str, p1, part, s)
{return (parseInt(part) + 1) + ".";}));
            } else {
                file.leafName = file.leafName.replace(/\./, "-1$&");
            }
        }
    }
    if (isDocument) {
        prefs.setIntPref("save_converter_index", filterIndex);
    }
    var source = (isDocument && filterIndex != 1) ? aData.document : aSniffer.uri;
    var persistArgs = {source:source, contentType:(isDocument && filterIndex == 2) ? "text/plain"
: contentType, target:file, postData:aData.document ? getPostData() : null,
bypassCache:aData.bypassCache};
    var persist = makeWebBrowserPersist();
    const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
    const flags = nsIWBP.PERSIST_FLAGS_NO_CONVERSION |
nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
    if (aData.bypassCache) {
        persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_BYPASS_CACHE;
    } else {
        persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE;
    }
    if (shouldDecode) {
        persist.persistFlags &= ~nsIWBP.PERSIST_FLAGS_NO_CONVERSION;
    }
    var dl =
Components.classes['@mozilla.org/download;1'].createInstance(Components.interfaces.nsIDownload);
    if (isDocument && filterIndex != 1) {
        var filesFolder = null;
        if (persistArgs.contentType != "text/plain") {
            filesFolder = Components.classes[lfContractID].createInstance(nsILocalFile);
            filesFolder.initWithPath(persistArgs.target.path);
            var nameWithoutExtension = filesFolder.leafName;
            nameWithoutExtension = nameWithoutExtension.substring(0,
nameWithoutExtension.lastIndexOf("."));
            var filesFolderLeafName = getStringBundle().formatStringFromName("filesFolder",
[nameWithoutExtension], 1);
            filesFolder.leafName = filesFolderLeafName;
        }
        var encodingFlags = 0;
        if (persistArgs.contentType == "text/plain") {
            encodingFlags |= nsIWBP.ENCODE_FLAGS_FORMATTED;
            encodingFlags |= nsIWBP.ENCODE_FLAGS_ABSOLUTE_LINKS;
            encodingFlags |= nsIWBP.ENCODE_FLAGS_NOFRAMES_CONTENT;
        }
        const kWrapColumn = 80;
        dl.init(aSniffer.uri, persistArgs.target, null, null, null, persist);
        persist.saveDocument(persistArgs.source, persistArgs.target, filesFolder,
persistArgs.contentType, encodingFlags, kWrapColumn);
    } else {
        dl.init(source, persistArgs.target, null, null, null, persist);
        persist.saveURI(source, null, null, persistArgs.postData, null, persistArgs.target);
    }
}



============

studing this will help you in solving you problem


________________________________________________________________________
Want to chat instantly with your online friends?  Get the FREE Yahoo!
Messenger http://mail.messenger.yahoo.co.uk


More information about the MacroEditor mailing list