From 1b07176cefc52f9df98372beb4ed071f0d584598 Mon Sep 17 00:00:00 2001 From: pliny <133052465+elder-plinius@users.noreply.github.com> Date: Sat, 12 Aug 2023 17:16:44 -0700 Subject: [PATCH] Create content.js --- content.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 content.js diff --git a/content.js b/content.js new file mode 100644 index 0000000..be6372a --- /dev/null +++ b/content.js @@ -0,0 +1,53 @@ +let audioContext; +let oscillatorLeft; +let oscillatorRight; +let gainNode; +let isPlaying = false; + +function startBinauralBeats(frequency, delta, volume) { + if (audioContext) { + audioContext.close(); + } + + audioContext = new AudioContext(); + + oscillatorLeft = audioContext.createOscillator(); + oscillatorRight = audioContext.createOscillator(); + gainNode = audioContext.createGain(); + + oscillatorLeft.frequency.setValueAtTime(frequency, audioContext.currentTime); + oscillatorRight.frequency.setValueAtTime(frequency + delta, audioContext.currentTime); + + gainNode.gain.setValueAtTime(volume, audioContext.currentTime); + + oscillatorLeft.connect(gainNode); + oscillatorRight.connect(gainNode); + gainNode.connect(audioContext.destination); + + oscillatorLeft.start(); + oscillatorRight.start(); + + isPlaying = true; +} + +function stopBinauralBeats() { + if (oscillatorLeft) { + oscillatorLeft.stop(); + } + if (oscillatorRight) { + oscillatorRight.stop(); + } + isPlaying = false; +} + +chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { + if (message.action === 'togglePlayPause') { + if (isPlaying) { + stopBinauralBeats(); + sendResponse({ status: 'paused' }); + } else { + startBinauralBeats(message.frequency, message.delta, message.volume); + sendResponse({ status: 'playing' }); + } + } +});