voice_audio_multi/examples/browser_extension/supertonic_speaker/background.js
Fabio c36510c21e
Some checks failed
Linux Build / Linux cpu (push) Has been cancelled
Linux Build / Linux cpu-clang (push) Has been cancelled
Linux Build / Linux vulkan (push) Has been cancelled
mac-build / macOS CPU (push) Has been cancelled
Nix Build / Nix (cpu) (push) Has been cancelled
Nix Build / Nix (vulkan) (push) Has been cancelled
windows-build / Windows CPU (push) Has been cancelled
Initial import: audio.cpp with Orange Pi documentation
2026-11-04 19:05:22 +01:00

58 lines
1.5 KiB
JavaScript

"use strict";
const MENU_SELECTION = "audiocpp-read-selection";
const MENU_PAGE = "audiocpp-read-page";
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: MENU_SELECTION,
title: "Read selection",
contexts: ["selection"],
});
chrome.contextMenus.create({
id: MENU_PAGE,
title: "Read page",
contexts: ["page", "selection"],
});
});
async function pageText(tabId) {
const [{ result }] = await chrome.scripting.executeScript({
target: { tabId },
func: () => document.body.innerText.trim(),
});
return result || "";
}
async function openPlayer(text, source) {
const id = `${Date.now()}-${Math.random().toString(16).slice(2)}`;
await chrome.storage.session.set({
[id]: {
text: text.replace(/\s+/g, " ").trim(),
source,
autoStart: true,
},
});
await chrome.windows.create({
url: chrome.runtime.getURL(`player.html?id=${encodeURIComponent(id)}`),
type: "popup",
width: 380,
height: 640,
focused: true,
});
}
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (!tab || tab.id === undefined) {
return;
}
(async () => {
if (info.menuItemId === MENU_SELECTION) {
await openPlayer(info.selectionText || "", "selection");
} else if (info.menuItemId === MENU_PAGE) {
await openPlayer(await pageText(tab.id), "page");
}
})().catch((error) => {
console.error("audio.cpp speaker context menu failed:", error);
});
});