【问题标题】:getting error while trying to play Audio from URL in chrome extension尝试从 chrome 扩展中的 URL 播放音频时出错
【发布时间】:2021-11-14 11:17:48
【问题描述】:

我尝试了很多方法来播放来自 URL 的声音,但都不起作用。

当我检查页面有错误控制台:

chrome-extension://invalid/:1 GET chrome-extension://invalid/ net::ERR_FAILED

未捕获(承诺中)DOMException:加载失败,因为找不到支持的源。

manifest.json:

"options_page": "./html/content.html",
"permissions": [
    "activeTab",
    "storage",
    "contextMenus",
    "http://*/*",
    "https://*/*",
    "tabs"
],
"web_accessible_resources": [
    "*.mp3",
    "*.ogg"
]

我在 content.html 的 options_page 中执行此操作。我还授予了脚本的“web_accessible_resources”权限,但仍然没有成功。所有音频链接都存储在 chrome 存储中。

content.html 中附加的脚本:

document.addEventListener('click', function (e) { 
e.preventDefault();
if (e.target.matches('.audioBtn')) {   
    chrome.storage.local.get({ meanifyWords: [] }, (result) => { 
        let getWordsObj = result.meanifyWords;
        let getAudio = getWordsObj[e.target.id].audio;  //getAudio="//ssl.gstatic.com/dictionary/static/sounds/20200429/experience--_gb_1.8.mp3"
        let sound = new Audio(getAudio);
        sound.play();
    })
}
});

请指出上述代码中将解决此问题的任何更改。提前致谢。

【问题讨论】:

    标签: javascript google-chrome-extension manifest.json


    【解决方案1】:

    将架构添加到 URL:

    let getAudio = "https:" + getWordsObj[e.target.id].audio;
    

    【讨论】:

    • 是的,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    相关资源
    最近更新 更多