Skip to content

Instantly share code, notes, and snippets.

@guest271314
Last active March 15, 2024 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guest271314/f587e955f90ce7bd468f2e26ee414c29 to your computer and use it in GitHub Desktop.
Save guest271314/f587e955f90ce7bd468f2e26ee414c29 to your computer and use it in GitHub Desktop.
// 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);
}
});
{
"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": {}
}
"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);
}
});
@dazzletune
Copy link

Thank you.

@guest271314
Copy link
Author

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