簡體   English   中英

使用Firefox插件(SDK)保存網站圖像

[英]Save website image using a Firefox Addon (SDK)

我能夠在網站上獲取圖像的URL,但我想將圖像下載到本地驅動器。 我其實想要標准的下載提示。 使用Firefox Addon SDK(使用Javascript)執行此操作的最佳方法是什么?

因此,使用從Firefox的nsIWebBrowserPersist文檔中提取的一些代碼,我將它拼湊在一起。 我不理解所涉及的一些范圍問題,並且這不會提示用戶在何處或如何保存。 就我的目的而言,它有效,但如果有一個更好的解決方案,我想要更好的解決方案。

function DownloadImage(aURLToDownload, aSaveToFile)
{
    try {

        // download from: aURLToDownload
        var downloadURI = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService).newURI(aURLToDownload, null, null);
        console.log("Saving from: " + aURLToDownload);

        // download destination
        console.log("Saving as: " + aSaveToFile);
        var outputFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); 
        outputFile.initWithPath(aSaveToFile)

        var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Ci.nsIWebBrowserPersist);

        persist.progressListener = {
            // onComplete: function(){
                // alert("Download complete: " + aSaveToFile);
            // }
            onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {
                var percentComplete = (aCurTotalProgress/aMaxTotalProgress)*100;
                console.log(percentComplete +"%");
            }
            // onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
            // }
        };

        persist.saveURI(downloadURI, null, null, null, "", outputFile);
    } catch (e) {
        console.log(e);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM