使用Firefox插件(SDK)保存网站图片 [英] Save website image using a Firefox Addon (SDK)

查看:701
本文介绍了使用Firefox插件(SDK)保存网站图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在网站上抓取图像的网址,但是我想将图像下载到本地驱动器。我其实想要标准的下载提示。什么是使用Firefox的Addon SDK(使用Javascript)做到这一点的最佳方式? 所以使用一些代码从Firefox的<一个href =https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIWebBrowserPersist?redirectlocale=en-US&redirectslug=nsIWebBrowserPersist =nofollow> nsIWebBrowserPersist文档,我拼凑了这个一起。我不明白涉及的一些范围问题,这不会提示用户在哪里或如何保存。对于我的目的,它的工作原理,但我想要一个更好的解决方案,如果有一个。

 函数DownloadImage(aURLToDownload, aSaveToFile)
{
尝试{

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

//下载目标
console.log(另存为:+ 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);
}
}


I am able to grab the URL of an image on a website, but I want to download the image to the local drive. I actually want the standard download prompt. What is the best way to do this using Firefox Addon SDK (using Javascript)?

解决方案

So using some code lifted from Firefox's nsIWebBrowserPersist docs, I cobbled this together. I don't understand some of the scope issues involved, and this does not prompt the user for where or how to save. For my purposes, it works, but I would like a better solution if there is one out there.

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);
    }
}

这篇关于使用Firefox插件(SDK)保存网站图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆