尝试在 chrome 扩展中播放来自 URL 的音频时出错
getting error while trying to play Audio from URL in chrome extension
我尝试了很多方法来播放来自 URL 的声音,但它不起作用。
当我检查页面时有错误控制台:
chrome-extension://invalid/:1 GET chrome-extension://invalid/ net::ERR_FAILED
Uncaught (in promise) DOMException: Failed to load because no supported source was found.
manifest.json:
"options_page": "./html/content.html",
"permissions": [
"activeTab",
"storage",
"contextMenus",
"http://*/*",
"https://*/*",
"tabs"
],
"web_accessible_resources": [
"*.mp3",
"*.ogg"
]
我在 options_page 即 content.html 中执行此操作。我也给了脚本 '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();
})
}
});
请指出上面代码中任何可以解决此问题的更改。提前致谢。
将架构添加到 URL:
let getAudio = "https:" + getWordsObj[e.target.id].audio;
我尝试了很多方法来播放来自 URL 的声音,但它不起作用。
当我检查页面时有错误控制台:
chrome-extension://invalid/:1 GET chrome-extension://invalid/ net::ERR_FAILED
Uncaught (in promise) DOMException: Failed to load because no supported source was found.
manifest.json:
"options_page": "./html/content.html",
"permissions": [
"activeTab",
"storage",
"contextMenus",
"http://*/*",
"https://*/*",
"tabs"
],
"web_accessible_resources": [
"*.mp3",
"*.ogg"
]
我在 options_page 即 content.html 中执行此操作。我也给了脚本 '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();
})
}
});
请指出上面代码中任何可以解决此问题的更改。提前致谢。
将架构添加到 URL:
let getAudio = "https:" + getWordsObj[e.target.id].audio;