update to manifest-v3

This commit is contained in:
makaveli10
2023-05-30 11:31:28 +05:30
parent f113290129
commit 4c52ff26cf
25 changed files with 429 additions and 1407 deletions
+94 -94
View File
@@ -1,100 +1,100 @@
document.addEventListener('DOMContentLoaded', () => {
const mute = document.getElementById('mute');
const maxTime = document.getElementById('maxTime');
const save = document.getElementById('save');
const status = document.getElementById('status');
const mp3Select = document.getElementById('mp3');
const wavSelect = document.getElementById('wav');
const quality = document.getElementById("quality");
const qualityLi = document.getElementById("qualityLi");
const limitRemoved = document.getElementById("removeLimit");
const doVad = document.getElementById("doVad");
let currentFormat;
//initial settings
chrome.storage.sync.get({
muteTab: false,
maxTime: 1200000,
format: "mp3",
quality: 192,
limitRemoved: false,
asr: false,
doVad: false
}, (options) => {
mute.checked = options.muteTab;
limitRemoved.checked = options.limitRemoved;
maxTime.disabled = options.limitRemoved;
maxTime.value = options.maxTime/60000;
currentFormat = options.format;
doVad.checked = options.doVad;
if (options.format === "mp3") {
mp3Select.checked = true;
qualityLi.style.display = "block";
} else {
wavSelect.checked = true;
}
if (options.quality === "96") {
quality.selectedIndex = 0;
} else if(options.quality === "192") {
quality.selectedIndex = 1;
} else {
quality.selectedIndex = 2;
}
/**
* Captures audio from the active tab in Google Chrome.
* @returns {Promise<MediaStream>} A promise that resolves with the captured audio stream.
*/
function captureTabAudio() {
return new Promise((resolve) => {
chrome.tabCapture.capture(
{
audio: true,
video: false,
},
(stream) => {
resolve(stream);
}
);
});
}
mute.onchange = () => {
status.innerHTML = "";
}
doVad.onchange = () => {
status.innerHTML = "";
}
maxTime.onchange = () => {
status.innerHTML = "";
if(maxTime.value > 20) {
maxTime.value = 20;
} else if (maxTime.value < 1) {
maxTime.value = 1;
} else if (isNaN(maxTime.value)) {
maxTime.value = 20;
}
}
mp3Select.onclick = () => {
currentFormat = "mp3";
qualityLi.style.display = "block";
status.innerHTML = "";
}
wavSelect.onclick = () => {
currentFormat = "wav";
qualityLi.style.display = "none";
status.innerHTML = "";
}
quality.onchange = (e) => {
status.innerHTML = "";
}
limitRemoved.onchange = () => {
if(limitRemoved.checked) {
maxTime.disabled = true;
status.innerHTML = "WARNING: Recordings that are too long may not save properly!"
} else {
maxTime.disabled = false;
status.innerHTML = "";
}
}
save.onclick = () => {
chrome.storage.sync.set({
muteTab: mute.checked,
maxTime: maxTime.value*60000,
format: currentFormat,
quality: quality.value,
limitRemoved: limitRemoved.checked,
doVad: doVad.checked
/**
* Sends a message to a specific tab in Google Chrome.
* @param {number} tabId - The ID of the tab to send the message to.
* @param {any} data - The data to be sent as the message.
* @returns {Promise<any>} A promise that resolves with the response from the tab.
*/
function sendMessageToTab(tabId, data) {
return new Promise((resolve) => {
chrome.tabs.sendMessage(tabId, data, (response) => {
resolve(response);
});
status.innerHTML = "Settings saved!"
});
}
/**
* Starts recording audio from the captured tab.
* @param {Object} option - The options object containing the currentTabId.
*/
async function startRecord(option) {
const stream = await captureTabAudio();
if (stream) {
// call when the stream inactive
stream.oninactive = () => {
window.close();
};
const socket = new WebSocket("ws://localhost:9090/");
socket.onopen = function(e) {
socket.send("handshake");
};
socket.onmessage = async (event) => {
// console.log(event.data);
await sendMessageToTab(option.currentTabId, {
data: event.data,
});
};
const audioDataCache = [];
const context = new AudioContext({sampleRate: 16000});
const mediaStream = context.createMediaStreamSource(stream);
const recorder = context.createScriptProcessor(4096, 1, 1);
recorder.onaudioprocess = async (event) => {
if (!context) return;
const inputData = event.inputBuffer.getChannelData(0);
audioDataCache.push(inputData);
socket.send(inputData);
};
// Prevent page mute
mediaStream.connect(recorder);
recorder.connect(context.destination);
mediaStream.connect(context.destination);
} else {
window.close();
}
}
/**
* Listener for incoming messages from the extension's background script.
* @param {Object} request - The message request object.
* @param {Object} sender - The sender object containing information about the message sender.
* @param {Function} sendResponse - The function to send a response back to the message sender.
*/
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
const { type, data } = request;
switch (type) {
case "start_capture":
await startRecord(data);
break;
default:
break;
}
sendResponse({});
});