【问题标题】:Trying to use chrome.downloads (it's undefined for some reason) [duplicate]尝试使用 chrome.downloads(由于某种原因未定义)[重复]
【发布时间】:2017-08-17 03:38:00
【问题描述】:

我正在尝试使用 chrome.downloads 通过 Chrome 扩展程序从 URL 下载文件(图像),但由于某种原因,chrome.downloadsundefined(收到警告:Cannot read property 'download' of undefined)。我的尝试基于example from Google

我的测试扩展没有任何弹出窗口,只有一个基本清单和一个极其简单的 JavaScript 文件。

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

那么,我该怎么做呢?

【问题讨论】:

  • 我真的不知道,但我会尝试将链接更改为codepo8.github.io/canvas-images-and-pixels/img/horse.png
  • 代码和清单对我来说似乎没问题。您使用什么应用程序来测试您的扩展程序(桌面 chrome,也许?)?你用的是什么版本?
  • @Rico,我实际使用的链接是我要下载的网站上的有效图片链接。不过没关系,问题是chrome.downloads是未定义的...
  • @Wikiti 我正在使用 chrome canary 浏览器版本 59.0.3048.0 canary (64-bit) for Mac...

标签: javascript google-chrome-extension


【解决方案1】:

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

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

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(){
  }

背景页.html

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

【讨论】:

  • 相当蹩脚,但它演示了 chrome.downloads.download 的东西
  • 不回答问题
  • 我知道,我正在制作回答问题的扩展程序,截至目前,给我一个小时左右
  • 是的,但无论如何我都需要扩展,所以我正在制作它;)
  • 对您的回答的建议。您可能不需要 backgroundpage.html 来托管 background.js。相反,您可以将 background.js 添加到 manifest.json 下的 "background" : { "scripts" : ["background.js"] }
【解决方案2】:

你不能使用很多铬。包括从内容脚本下载的方法。将背景或事件页面与扩展消息一起使用,或直接从扩展弹出窗口中使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-08
    • 2020-10-02
    • 2019-07-03
    • 2019-09-20
    • 2021-08-05
    • 2021-02-08
    • 2019-06-03
    • 1970-01-01
    相关资源
    最近更新 更多