试图使用chrome.downloads(由于某种原因,它是未定义的) [英] Trying to use chrome.downloads (it's undefined for some reason)

查看:404
本文介绍了试图使用chrome.downloads(由于某种原因,它是未定义的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 chrome.downloads通过Chrome扩展程序从网址下载文件(图片) ,但由于某种原因 chrome.downloads undefined (收到警告:无法读取未定义的属性'download')。我的基础是来自Google的示例

I am trying to download a file (image) from a URL via a Chrome Extension using chrome.downloads, but for some reason chrome.downloads is undefined (getting the warning: Cannot read property 'download' of undefined). I am basing my attempt on example from Google.

我的测试扩展程序没有任何弹出窗口,只有基本清单和非常简单的JavaScript文件。

My test-extension does not have any popup, just a basic manifest and an extremely simply JavaScript file.

manifest.json

{
    "manifest_version": 2,
    "name": "Testing chrome.downloads.download",
    "version": "0.0.1",
    "permissions": [
        "activeTab",
        "downloads",
        "<all_urls>"
    ],
    "content_scripts": [{
        "matches": [
            "http://www.example.com/*"
        ],
        "js": [
            "jquery.js",
            "index.js"
        ]
    }]
}

index.js

$(document).ready(function () {
  link = 'http://example.com/image.jpg';
  chrome.downloads.download({
    url: link,
    filename: './' + link.substr(link.lastIndexOf('/')+1),
    saveAs: false
  });
});

那么,我该怎样才能做到这一点?

So, how can I make this work?

推荐答案

因此经过一些研究后我看到内容脚本不直接支持下载,但您可以将消息传递到支持下载的后台页面。我加入背景页面的原因是能够看到控制台;)你可以尝试直接转到background.js

So after some research i see content scripts dont directly support downloads, but you can pass message to your background page which supports download. The reason i included background page was to be able to see the console ;) you can try to directly goto background.js

manifest.json

manifest.json

{
  "name": "Download static images",
  "description": "Downloads images defined with <img> tag from watched webpage(s) by injecting a script",
  "version": "1.0",
  "browser_action": {
    "default_icon": "debuggerPause.png",
    "default_title": "get page html"
  },
  "background": {
    "page": "background.html"
  },
  "content_scripts": [
    {
      "matches": ["http://stackoverflow.com/*"],
      "js": ["main.js"]
    }
  ],
  "permissions": [
      "tabs", 
      "background","downloads"
  ],
  "manifest_version": 2
}

main.js

function getHtml() {
  var pagehtml = document.documentElement;
  var imgs=pagehtml.getElementsByTagName( 'img' );
  var pass_array=[];
  for (i in imgs){
    pass_array.push(imgs[i]["currentSrc"]);
  }
  console.log(pass_array);
  var param = {collection : pass_array};
  chrome.runtime.sendMessage(param);
};
getHtml();

background.js

background.js

chrome.runtime.onMessage.addListener(
  function(arg, sender, sendResponse) {
    var args=arg.collection;
    for (i in args){
    var img_url=args[i];
    try{
     saveas=img_url.replace(/[^a-zA-Z0-9]/g,'-');
    }
    catch (problem){
    }

     chrome.downloads.download({
     url: img_url,
     filename: saveas,
    saveAs: false
    });
   }
});

  function sendResponse(){
  }

backgroundpage.html

backgroundpage.html

<script src="background.js"></script>

这篇关于试图使用chrome.downloads(由于某种原因,它是未定义的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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