javascript - chrome.downloads.download 给出 "Error during downloads.download: Invalid URL."

标签 javascript google-chrome google-chrome-extension download

我正在为 Google Chrome 制作一个使用 Downloads API 的扩展程序。 我想从网站下载 .jpg 文件。

我已在 list 中使用了这些权限:

"permissions": ["downloads", "*://www.thatsite.com/*"]

我使用的命令是:

chrome.downloads.download({"url":"https://www.thatsite.com/images/image1.jpg"});

这会产生以下错误

"Error during downloads.download: Invalid URL."

有什么想法可以解决这个问题吗?

最佳答案

我尝试使用以下代码,它按预期工作。我希望您不在稳定 channel 。

把你的完整代码贴出来,问题完全出在URL上。

您可以引用以下代码作为引用

引用

manifest.json

使用 list 文件注册浏览器操作权限

{
    "name": "Download Demo",
    "description": "http://stackoverflow.com/questions/14560465/chrome-downloads-download-gives-error-during-downloads-download-invalid-url",
    "manifest_version": 2,
    "version": "1",
    "browser_action": {
        "default_popup": "popup.html"
    },
    "permissions": [
        "downloads",
        "*://www.google.co.in/*"
    ]
}

popup.html

添加了 <script> 文件以符合CSP

<html>

    <head>
        <script src="popup.js"></script>
    </head>

    <body>
        <button id="download">Download</button>
    </body>

</html>

popup.js

只需将 URL 传递给 download API 即可。

document.addEventListener("DOMContentLoaded", function () {
    document.getElementById("download").addEventListener("click", function () {
        chrome.downloads.download({
            "url": "http://www.google.co.in/images/srpr/logo3w.png"
        }, function () {
            console.log("downloaded");
        });
    });
});

关于javascript - chrome.downloads.download 给出 "Error during downloads.download: Invalid URL.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14560465/

相关文章:

c# - 如何访问 CheckBoxList 中 ListItems 的键

javascript - 获取 YouTube 播放列表的引用

cordova - 如何在Chrome开发工具中检查请求的大小(上传内容)

html - 如何链接到 Google Chrome 扩展程序的本地镜像?

javascript - Chrome 扩展 : Creating a new tab after clicking on the notification

javascript - 从 JavaScript 对象中选择随机项目?

javascript - Canvas 中的两个(任意)区域 : figuring out which is bigger

html - Chrome 媒体中心 API

javascript - 在没有监听器的情况下将消息从 background.js 发送到 popup.js

javascript - Chrome 网上商店服务器拒绝带有 "Error : The manifest must define a version."的扩展