首页 > 解决方案 > 是否可以在内容脚本中使用 Chrome API?

问题描述

我目前正在尝试通过内容脚本下载文件。然而,它似乎什么也没做。我还尝试向我的后台脚本发送消息,然后从后台文件下载它,但是似乎没有收到任何消息。

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    if(message == "handle_all") {
        let is_playing = document.getElementsByClassName("playControl")[0].innerHTML == "Pause current";
        let volume = document.getElementsByClassName("volume__sliderWrapper")[0].getAttribute('aria-valuenow');

        SC.initialize({
            client_id: 'censored',
        });

        if(is_playing) {
            SC.resolve('https://soundcloud.com/moow-beatmaker/it-wasnt-me').then(function(track) {
                chrome.downloads.download({
                    url: "track.download_url + '?client_id=censored'",
                });
            });
        }
    }
});

什么都没发生。没有下载,什么都没有。当我自己访问下载页面时,我收到错误代码 406“不可接受”。

这是无法在内容脚本中使用 Chrome API 的问题,还是我从中下载文件的 API 有问题?

标签: javascriptgoogle-chrome-extension

解决方案


推荐阅读