下载网址镀铬扩展 [英] Download url chrome extension

查看:102
本文介绍了下载网址镀铬扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过编写Chrome扩展的代码来下载url。这里是myscript.js文件:

  chrome.downloads.download(
{url:'http:// www.iana.org/_img/iana-logo-pageheader.png',
saveAs:true
},
function(res){alert(res);});

这里是我的manifest.json



<$
name:我的扩展名,
版本:1.0,
manifest_version:2,
background_page:background.html,
browser_action:{
name:操作DOM,
图标:[icon.png],
default_icon:icon.png
},
permissions:[downloads,
tabs,http:// * / *,https :// * / *

content_scripts:[
{
matches:[http:// * / *,https:/ / * / *],
js:[jquery.js,d3.v2.js,myscript.js],
run_at:document_end


$ b code
$ b $ p $但是控制台显示错误无法调用未定义的方法下载。
请帮助我。

解决方案

chrome.downloads 明确指出,API仍在开发中。仅适用于Chrome用户开发人员提前发布频道 。 (重点是我的,目前在Chrome 23上)。



要使用API​​,您需要获得 dev canary strong>构建Chrome(请参阅此页面以获取下载链接)。



解决该问题的另一种方法是不使用 chrome.downloads API。我一直在使用下面的方法来创建下载,它的功能就像一个魅力(它可以在任何地方工作:内容脚本/背景页面/弹出/任何):

  var a = document.createElement('a'); 
a.href ='http://www.iana.org/_img/iana-logo-pageheader.png';
a.download ='iana-logo-pageheader.png'; //文件名
a.click(); //触发器下载

a.click()导致Chrome浏览器关注该链接。

下载属性会导致Chrome下载目标,并在保存中将该属性的值建议为文件名作为对话框。



此功能不仅限于Chrome扩展程序,您还可以在普通网页中使用它。看看这个演示: http://jsfiddle.net/dEeHF/


I am trying to download a url by writing code for chrome extension. Here is the myscript.js file:

chrome.downloads.download(
    {url: 'http://www.iana.org/_img/iana-logo-pageheader.png',
     saveAs: true
    },
    function(res){alert(res);});

and here is my manifest.json

{
  "name": "My extension",
  "version": "1.0",
  "manifest_version":2,
  "background_page": "background.html",
  "browser_action": {
    "name": "Manipulate DOM",
    "icons": ["icon.png"],
    "default_icon": "icon.png"
  },
  "permissions": ["downloads",
    "tabs", "http://*/*","https://*/*"
  ],
  "content_scripts": [
    {
      "matches": [ "http://*/*", "https://*/*"],
      "js": ["jquery.js","d3.v2.js","myscript.js"],
      "run_at": "document_end"
    }
  ]
}

but the console is showing the error "Cannot call method 'download' of undefined". Please help me.

解决方案

The documentation for chrome.downloads clearly states that the "API is still under development. It is only available for Chrome users on the dev early release channel." (emphasis mine, currently at Chrome 23).

To use the API, you need to get a dev or canary build of Chrome (see this page for download links).

Another way to solve the problem is by not using the chrome.downloads API. I've been using the following method to create downloads, and it works like a charm (it works anywhere: Content script / background page / popup / whatever):

var a = document.createElement('a');
a.href = 'http://www.iana.org/_img/iana-logo-pageheader.png';
a.download = 'iana-logo-pageheader.png'; // Filename
a.click();                               // Trigger download

a.click() causes Chrome to follow the link.
The download attribute causes Chrome to download the target, and suggest the attribute's value as a file name in the Save As dialog.

This feature is not limited to Chrome extensions, you can also use it in an ordinary web page. Have a look at this demo: http://jsfiddle.net/dEeHF/.

这篇关于下载网址镀铬扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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