如何调用“保存图像为...” Chrome扩展功能? [英] How to call "Save Image As..." feature in Chrome Extension?
问题描述
什么是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)//无法启动时下载
{
/ *错误处理* /
}
其他
{
/ *您的进一步任务* /
}
});
请注意:
以下是更多信息: 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屋!