Last active
March 15, 2024 20:01
-
-
Save guest271314/f587e955f90ce7bd468f2e26ee414c29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Helper function to play audio | |
function playAudio(audioFile) { | |
console.log(audioFile, chrome.runtime.getURL(audioFile)); | |
const audio = new Audio(chrome.runtime.getURL(audioFile)); // Use chrome.runtime.getURL for packaged extension resources | |
// Play the audio only when the EventListener for click is triggered | |
audio.play(); | |
} | |
// Helper function for setting volume | |
function setAudioVolume(volumeLevel) { | |
const audioElements = document.querySelectorAll('audio'); | |
audioElements.forEach(audio => { | |
audio.volume = volumeLevel; | |
}); | |
} | |
// Receive messages from your service worker | |
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { | |
console.log(message, sender, sendResponse); | |
switch (message.action) { | |
case 'playNavigationSound': | |
playAudio('test.wav'); | |
break; | |
case 'playDownloadCompleteSound': | |
playAudio('test.wav'); | |
break; | |
case 'playDownloadErrorSound': | |
playAudio('test.wav'); | |
break; | |
case 'playMuteWarningSound': | |
playAudio('test.wav'); | |
break; | |
case 'changeVolme': | |
setAudioVolume(0.75); | |
break; | |
default: | |
console.log('Unknown message action:', message.action); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"manifest_version": 3, | |
"name": "PopupSound", | |
"description": "Plays a click sound when you click on a link. Also plays a trumpet sound when you finish downloading a file.", | |
"author": "Michael Gunter", | |
"version": "3.0", | |
"offline_enabled": true, | |
"background": { | |
"service_worker": "service-worker.js" | |
}, | |
"content_scripts": [ | |
{ | |
"matches": ["<all_urls>"], | |
"js": ["content-script.js"], | |
"run_at": "document_end" | |
} | |
], | |
"web_accessible_resources": [ | |
{ | |
"resources": [ | |
"*.wav" | |
], | |
"matches": [ | |
"<all_urls>" | |
], | |
"extensions": [] | |
} | |
], | |
"permissions": [ | |
"webNavigation", | |
"downloads", | |
"tabs", | |
"activeTab" | |
], | |
"host_permissions": ["<all_urls>"], | |
"content_security_policy": {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function sendAudioMessage(actionType, audioFile) { | |
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { | |
tabs.forEach(tab => { | |
console.log(tab); | |
if (tab.url.startsWith("chrome")) return false; | |
chrome.tabs.sendMessage(tab.id, { action: actionType }); | |
}); | |
}); | |
} | |
chrome.webNavigation.onCreatedNavigationTarget.addListener(onNav); | |
chrome.webNavigation.onBeforeNavigate.addListener(onNav); | |
chrome.webNavigation.onReferenceFragmentUpdated.addListener(onNav); | |
chrome.webNavigation.onHistoryStateUpdated.addListener(onNav); | |
function onNav(props) { | |
const {frameId} = props; | |
console.log(props); | |
if(frameId > 0) return; | |
sendAudioMessage('playNavigationSound'); | |
} | |
chrome.downloads.onChanged.addListener(delta => { | |
if(delta.state && delta.state.current === "complete") { | |
sendAudioMessage('playDownloadCompleteSound'); | |
} | |
if(delta.error && delta.error.current) { | |
sendAudioMessage('playDownloadErrorSound'); | |
} | |
}); | |
chrome.tabs.onUpdated.addListener((tabId, {mutedInfo}) => { | |
if(mutedInfo && mutedInfo.reason === "user") { | |
sendAudioMessage('playMuteWarningSound'); | |
} | |
}); | |
function changeVolume(volumeLevel) { | |
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { | |
tabs.forEach(tab => { | |
if (tab.url.startsWith("chrome")) return; | |
chrome.tabs.sendMessage(tab.id, { action: 'changeVolume', volume: volumeLevel }); | |
}); | |
}); | |
} | |
// *** Message Handler *** | |
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { | |
console.log(message, sender, sendResponse); | |
switch (message.action) { | |
case 'playNavigationSound': | |
sendAudioMessage('playNavigationSound'); | |
break; | |
case 'playDownloadCompleteSound': | |
sendAudioMessage('playDownloadCompleteSound'); | |
break; | |
case 'playDownloadErrorSound': | |
sendAudioMessage('playDownloadErrorSound'); | |
break; | |
case 'playMuteWarningSound': | |
sendAudioMessage('playMuteWarningSound'); | |
break; | |
case 'changeVolume': | |
changeVolume(0.75); | |
break; | |
default: | |
console.log('Unknown message action:', message.action); | |
} | |
}); |
No worries. You'll have to do some error handling and firther debugging to find out where and why Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.
is occurring.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you.