|
17 | 17 | */ |
18 | 18 |
|
19 | 19 | import {log} from '../core/log.js'; |
20 | | -import {toError} from '../core/to-error.js'; |
21 | 20 | import {WebExtension} from '../extension/web-extension.js'; |
22 | 21 | import {Backend} from './backend.js'; |
23 | 22 |
|
24 | | -/** |
25 | | - * @param {string} url |
26 | | - * @returns {Promise<{data: string, contentType: string}>} |
27 | | - */ |
28 | | -async function fetchLocalAudioData(url) { |
29 | | - const response = await fetch(url); |
30 | | - if (!response.ok) { |
31 | | - throw new Error(`Local server responded with HTTP status code ${response.status}`); |
32 | | - } |
33 | | - |
34 | | - const contentType = response.headers.get('content-type') || 'audio/mpeg'; |
35 | | - const arrayBuffer = await response.arrayBuffer(); |
36 | | - |
37 | | - let binary = ''; |
38 | | - const bytes = new Uint8Array(arrayBuffer); |
39 | | - for (let i = 0; i < bytes.byteLength; i++) { |
40 | | - binary += String.fromCharCode(bytes[i]); |
41 | | - } |
42 | | - |
43 | | - return { |
44 | | - data: btoa(binary), |
45 | | - contentType: contentType, |
46 | | - }; |
47 | | -} |
48 | | - |
49 | | -chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => { |
50 | | - if (message.type === 'FETCH_LOCAL_AUDIO_DATA') { |
51 | | - const url = /** @type {unknown} */ (message.url); |
52 | | - if (typeof url !== 'string') { |
53 | | - sendResponse({success: false, error: 'Invalid or missing URL parameter'}); |
54 | | - return false; |
55 | | - } |
56 | | - fetchLocalAudioData(url) |
57 | | - .then((result) => sendResponse({success: true, data: result.data, contentType: result.contentType})) |
58 | | - .catch((error) => sendResponse({success: false, error: toError(error).message})); |
59 | | - return true; |
60 | | - } |
61 | | -}); |
62 | | - |
63 | 23 | /** Entry point. */ |
64 | 24 | async function main() { |
65 | 25 | const webExtension = new WebExtension(); |
|
0 commit comments