如何调用“保存图像为...” Chrome扩展功能? [英] How to call "Save Image As..." feature in Chrome Extension?

查看:128
本文介绍了如何调用“保存图像为...” Chrome扩展功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Chrome扩展程序库具有很好的功能来调用下载方法...但是,当我们右键单击图像时,这种回调不能使我们为脚本执行脚本 - 实际上,右键单击图像时不会激活脚本如果它是链接的话)。



什么是Chrome的扩展命令,允许我们调用下载图像?谢谢。

解决方案

我和你有同样的问题,但我有另一种方法:使用 chrome .downloads.download api。



尝试获取图像的url,并将url传递给后台脚本,这是唯一的位置授权chrome.downloads api。



此api的一个示例使用是:

  var name = url.substr(url.lastIndexOf(/)+ 1); 
chrome.downloads.download({url:url,filename:name,saveAs:false},function(res_id){
if(typeof res_id ===undefined)//无法启动时下载
{
/ *错误处理* /
}
其他
{
/ *您的进一步任务* /
}
});

请注意:


  • 下载位置与用户的chrome设置相同。

  • 文件名 可能包含路径和文件夹名称,文件名,指示下载位置的子文件夹。
  • 无需任何弹出窗口即可自动下载 saveAs 关闭;
  • / li>


以下是更多信息: https://developer.chrome.com/extensions/downloads



最后,我认为这只是一种选择。我仍然想知道这个问题的完美答案,就像我在这里问的一样:
铬扩展api是否支持直接从浏览缓存下载?


Chrome extension library has knifty feature to call download method...however such callback doesn't enable us to script for the behavior when we right click an image - in fact it won't activate when right click on an image (would if it is a link).

What is Chrome's extension command that allows us to call for downloading an image? Thanks.

解决方案

I have the same question as you, but I have an alternative to do this: use chrome.downloads.download api.

Try to obtain the url of the image, and pass the url to the background script, which is the only location authorizing chrome.downloads api.

A sample use for this api is:

var name = url.substr(url.lastIndexOf("/")+1);
chrome.downloads.download({url:url,filename:name,saveAs:false},function(res_id){
    if(typeof res_id === "undefined") // when failing to start the download
    {
        /*err handling*/
    }
    else
    {
        /*your further task*/
    }
});

Note that:

  • The download location is the same as user's chrome setting.
  • filename may contain the path and folder name before the file name, indicating sub-folder of the download location.
  • It downloads auto without any popup windows to turn saveAs off;

Here's spec for more: https://developer.chrome.com/extensions/downloads

Finally, I think it's just an alternative. I still want to know a perfect answer to the question, like what I also asked here: Does chrome extension api support downloads directly from browsing cache?

这篇关于如何调用“保存图像为...” Chrome扩展功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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