Skip to content

Commit

Permalink
feat: support audio play in csp url
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon406 committed May 22, 2024
1 parent 30ab589 commit 33395b3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "__MSG_appName__",
"description": "__MSG_appDesc__",
"default_locale": "zh_CN",
"version": "0.3.3",
"version": "0.3.4",
"icons": {
"48": "assets/result48.png",
"128": "assets/icon_128.png"
Expand Down
4 changes: 3 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ function initialize_extension() {
console.log('request fetchArrayBuffer', request)
fetch(request.audioUrl)
.then((response) => response.arrayBuffer())
.then((buffer) => JSON.stringify({ data: new Uint8Array(buffer) }))
.then((buffer) => JSON.stringify({
data: Array.apply(null, new Uint8Array(buffer))
}))
.then(sendResponse)
return true // Will respond asynchronously.
}
Expand Down
28 changes: 13 additions & 15 deletions src/content_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const options = {
}

const cache = new LRUCache(options)
const cacheAudio = new LRUCache(options)

let dict_words = null
let dict_idioms = null
Expand Down Expand Up @@ -111,32 +112,29 @@ function addPhoneticClickEvent() {
}

function play(audioUrl) {
try {
new Audio(audioUrl).play()
}catch (e) {
console.error(e)
// todo fix arraybuffer play
let cached = cacheAudio.get(audioUrl)
if (cached) {
console.log('use Cache arraybuffer')
playArrayBuffer(cached)
} else {
console.debug('play', audioUrl)
// try {
new Audio(audioUrl).play()
.catch((e) => {
console.error(e)
let cached = cacheAudio.get(audioUrl)
if (cached) {
console.log('use Cache arraybuffer')
playArrayBuffer(cached)
} else {
chrome.runtime.sendMessage(
{
type: 'fetchArrayBuffer',
audioUrl
},
(res) => {
console.log('arraybuffer', res)
console.log('arraybuffer2', JSON.parse(res).data)
const arraybuffer = new Uint8Array(JSON.parse(res).data).buffer
console.log('arraybuffer', arraybuffer)
cacheAudio.set(audioUrl, arraybuffer)
playArrayBuffer(arraybuffer)
}
)
}
}
}
})
}

/**播放 ArrayBuffer 音频*/
Expand Down

0 comments on commit 33395b3

Please sign in to comment.