From a634a6d6d3c1d6a18af61498477bca06fc3969c8 Mon Sep 17 00:00:00 2001 From: makaveli10 Date: Thu, 13 Jul 2023 00:59:48 +0530 Subject: [PATCH] add option to use collabora server --- Audio-Transcription-Firefox/content.js | 7 ++- Audio-Transcription-Firefox/manifest.json | 1 + Audio-Transcription-Firefox/popup.html | 4 ++ Audio-Transcription-Firefox/popup.js | 56 +++++++++++++++++++++-- Audio-Transcription-Firefox/style.css | 4 ++ Audio-Transcription/background.js | 7 +-- Audio-Transcription/options.js | 2 +- Audio-Transcription/popup.html | 4 ++ Audio-Transcription/popup.js | 33 ++++++++++++- Audio-Transcription/style.css | 4 ++ 10 files changed, 109 insertions(+), 13 deletions(-) diff --git a/Audio-Transcription-Firefox/content.js b/Audio-Transcription-Firefox/content.js index 51ef0cd..eb8a32a 100644 --- a/Audio-Transcription-Firefox/content.js +++ b/Audio-Transcription-Firefox/content.js @@ -1,4 +1,3 @@ -console.log("Content script injected."); let socket = null; let isCapturing = false; let mediaStream = null; @@ -39,8 +38,8 @@ function resampleTo16kHZ(audioData, origSampleRate = 44100) { return resampledData; } -function startRecording() { - socket = new WebSocket("ws://localhost:9090/"); +function startRecording(host, port) { + socket = new WebSocket(`ws://${host}:${port}/`); socket.onopen = function(e) { socket.send("handshake"); }; @@ -210,7 +209,7 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => { const { action, data } = request; if (action === "startCapture") { isCapturing = true; - startRecording(); + startRecording(data.host, data.port); } else if (action === "stopCapture") { isCapturing = false; diff --git a/Audio-Transcription-Firefox/manifest.json b/Audio-Transcription-Firefox/manifest.json index caa447c..9c0114e 100644 --- a/Audio-Transcription-Firefox/manifest.json +++ b/Audio-Transcription-Firefox/manifest.json @@ -4,6 +4,7 @@ "version": "1.0", "description": "Transcribe audio from any webpage.", "permissions": [ + "storage", "activeTab", "" ], diff --git a/Audio-Transcription-Firefox/popup.html b/Audio-Transcription-Firefox/popup.html index 0955461..3766564 100644 --- a/Audio-Transcription-Firefox/popup.html +++ b/Audio-Transcription-Firefox/popup.html @@ -11,5 +11,9 @@
Start Capture
Stop Capture
+
+ + +
\ No newline at end of file diff --git a/Audio-Transcription-Firefox/popup.js b/Audio-Transcription-Firefox/popup.js index bc37fc4..1ef684d 100644 --- a/Audio-Transcription-Firefox/popup.js +++ b/Audio-Transcription-Firefox/popup.js @@ -2,11 +2,49 @@ document.addEventListener("DOMContentLoaded", function() { var startButton = document.getElementById("startCapture"); var stopButton = document.getElementById("stopCapture"); + const useServerCheckbox = document.getElementById("useServerCheckbox"); + browser.storage.local.get("capturingState") + .then(function(result) { + var capturingState = result.capturingState; + if (capturingState && capturingState.isCapturing) { + toggleCaptureButtons(true); + } else { + toggleCaptureButtons(false); + } + // Enable the startButton + startButton.disabled = false; + }) + .catch(function(error) { + console.error("Error retrieving capturing state:", error); + // Enable the startButton + startButton.disabled = false; + }); + + browser.storage.local.get("useServerState", ({ useServerState }) => { + if (useServerState !== undefined) { + useServerCheckbox.checked = useServerState; + } + }); + startButton.addEventListener("click", function() { + let host = "localhost"; + let port = "9090"; + const useCollaboraServer = useServerCheckbox.checked; + + if (useCollaboraServer){ + host = "transcription.kurg.org" + port = "7090" + } + browser.tabs.query({ active: true, currentWindow: true }) .then(function(tabs) { - browser.tabs.sendMessage(tabs[0].id, { action: "startCapture" }); + browser.tabs.sendMessage( + tabs[0].id, { action: "startCapture", data: {host, port} }); toggleCaptureButtons(true); + browser.storage.local.set({ capturingState: { isCapturing: true } }) + .catch(function(error) { + console.error("Error storing capturing state:", error); + }); }) .catch(function(error) { console.error("Error sending startCapture message:", error); @@ -18,8 +56,11 @@ document.addEventListener("DOMContentLoaded", function() { .then(function(tabs) { browser.tabs.sendMessage(tabs[0].id, { action: "stopCapture" }) .then(function(response) { - console.log(response); toggleCaptureButtons(false); + browser.storage.local.set({ capturingState: { isCapturing: false } }) + .catch(function(error) { + console.error("Error storing capturing state:", error); + }); }) .catch(function(error) { console.error("Error sending stopCapture message:", error); @@ -34,5 +75,14 @@ document.addEventListener("DOMContentLoaded", function() { function toggleCaptureButtons(isCapturing) { startButton.disabled = isCapturing; stopButton.disabled = !isCapturing; + useServerCheckbox.disabled = isCapturing; // Disable checkbox if capturing + startButton.classList.toggle("disabled", isCapturing); + stopButton.classList.toggle("disabled", !isCapturing); } -}); \ No newline at end of file + + // Save the checkbox state when it's toggled + useServerCheckbox.addEventListener("change", () => { + const useServerState = useServerCheckbox.checked; + browser.storage.local.set({ useServerState }); + }); +}); diff --git a/Audio-Transcription-Firefox/style.css b/Audio-Transcription-Firefox/style.css index c1c56ca..f8bba91 100644 --- a/Audio-Transcription-Firefox/style.css +++ b/Audio-Transcription-Firefox/style.css @@ -101,3 +101,7 @@ label { width: 30px; text-align: center; } + +.checkbox-container { + padding: 10px; +} diff --git a/Audio-Transcription/background.js b/Audio-Transcription/background.js index 25ea75c..09de7dc 100644 --- a/Audio-Transcription/background.js +++ b/Audio-Transcription/background.js @@ -129,7 +129,8 @@ async function getTab(tabId) { * @param {number} tabId - The ID of the tab to start capturing. * @returns {Promise} - A Promise that resolves when the capture process is started successfully. */ -async function startCapture(tabId) { +async function startCapture(options) { + const { tabId } = options; const optionTabId = await getLocalStorageValue("optionTabId"); if (optionTabId) { await removeChromeTab(optionTabId); @@ -149,7 +150,7 @@ async function startCapture(tabId) { await sendMessageToTab(optionTab.id, { type: "start_capture", - data: { currentTabId: currentTab.id }, + data: { currentTabId: currentTab.id, host: options.host, port: options.port }, }); } else { console.log("No Audio"); @@ -184,7 +185,7 @@ async function stopCapture() { */ chrome.runtime.onMessage.addListener((message) => { if (message.action === "startCapture") { - startCapture(message.tabId); + startCapture(message); } else if (message.action === "stopCapture") { stopCapture(); } diff --git a/Audio-Transcription/options.js b/Audio-Transcription/options.js index 8f2092d..ad7dfb1 100644 --- a/Audio-Transcription/options.js +++ b/Audio-Transcription/options.js @@ -80,7 +80,7 @@ async function startRecord(option) { window.close(); }; - const socket = new WebSocket("ws://localhost:9090/"); + const socket = new WebSocket(`ws://${option.host}:${option.port}/`); let isServerReady = false; socket.onopen = function(e) { socket.send("handshake"); diff --git a/Audio-Transcription/popup.html b/Audio-Transcription/popup.html index b961819..5777929 100644 --- a/Audio-Transcription/popup.html +++ b/Audio-Transcription/popup.html @@ -11,5 +11,9 @@
Start Capture
Stop Capture
+
+ + +
\ No newline at end of file diff --git a/Audio-Transcription/popup.js b/Audio-Transcription/popup.js index a1fe516..20b061c 100644 --- a/Audio-Transcription/popup.js +++ b/Audio-Transcription/popup.js @@ -3,6 +3,8 @@ document.addEventListener("DOMContentLoaded", function () { const startButton = document.getElementById("startCapture"); const stopButton = document.getElementById("stopCapture"); + const useServerCheckbox = document.getElementById("useServerCheckbox"); + // Add click event listeners to the buttons startButton.addEventListener("click", startCapture); stopButton.addEventListener("click", stopCapture); @@ -16,6 +18,13 @@ document.addEventListener("DOMContentLoaded", function () { } }); + // Retrieve checkbox state from storage on popup open + chrome.storage.local.get("useServerState", ({ useServerState }) => { + if (useServerState !== undefined) { + useServerCheckbox.checked = useServerState; + } + }); + // Function to handle the start capture button click event async function startCapture() { // Ignore click if the button is disabled @@ -27,7 +36,20 @@ document.addEventListener("DOMContentLoaded", function () { const currentTab = await getCurrentTab(); // Send a message to the background script to start capturing - chrome.runtime.sendMessage({ action: "startCapture", tabId: currentTab.id }, () => { + let host = "localhost"; + let port = "9090"; + const useCollaboraServer = useServerCheckbox.checked; + if (useCollaboraServer){ + host = "transcription.kurg.org" + port = "7090" + } + + chrome.runtime.sendMessage( + { + action: "startCapture", + tabId: currentTab.id, + host: host, + port: port }, () => { // Update capturing state in storage and toggle the buttons chrome.storage.local.set({ capturingState: { isCapturing: true } }, () => { toggleCaptureButtons(true); @@ -64,7 +86,14 @@ document.addEventListener("DOMContentLoaded", function () { function toggleCaptureButtons(isCapturing) { startButton.disabled = isCapturing; stopButton.disabled = !isCapturing; + useServerCheckbox.disabled = isCapturing; startButton.classList.toggle("disabled", isCapturing); stopButton.classList.toggle("disabled", !isCapturing); } -}); \ No newline at end of file + + // Save the checkbox state when it's toggled + useServerCheckbox.addEventListener("change", () => { + const useServerState = useServerCheckbox.checked; + chrome.storage.local.set({ useServerState }); + }); +}); diff --git a/Audio-Transcription/style.css b/Audio-Transcription/style.css index c1c56ca..f8bba91 100644 --- a/Audio-Transcription/style.css +++ b/Audio-Transcription/style.css @@ -101,3 +101,7 @@ label { width: 30px; text-align: center; } + +.checkbox-container { + padding: 10px; +}