-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
146 lines (123 loc) · 4.71 KB
/
popup.js
File metadata and controls
146 lines (123 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Popup script for controlling the extension
document.addEventListener('DOMContentLoaded', function() {
const toggleSwitch = document.getElementById('toggleSwitch');
const statusElement = document.getElementById('status');
const testTinyBtn = document.getElementById('testTiny');
const testSmallBtn = document.getElementById('testSmall');
const testMediumBtn = document.getElementById('testMedium');
const testLargeBtn = document.getElementById('testLarge');
const testXLargeBtn = document.getElementById('testXLarge');
// Audio context for test tones
let audioContext;
function initAudioContext() {
try {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
} catch (error) {
console.warn('Web Audio API not supported:', error);
}
}
// Initialize audio context
initAudioContext();
// Load current status
chrome.runtime.sendMessage({ type: 'GET_STATUS' }, (response) => {
if (response && response.enabled !== undefined) {
updateToggleState(response.enabled);
updateStatus(response.enabled);
}
});
// Toggle switch functionality
toggleSwitch.addEventListener('click', function() {
const isCurrentlyEnabled = toggleSwitch.classList.contains('active');
const newState = !isCurrentlyEnabled;
chrome.runtime.sendMessage({
type: 'TOGGLE_ENABLED',
enabled: newState
}, (response) => {
if (response && response.success) {
updateToggleState(newState);
updateStatus(newState);
}
});
});
function updateToggleState(enabled) {
if (enabled) {
toggleSwitch.classList.add('active');
} else {
toggleSwitch.classList.remove('active');
}
}
function updateStatus(enabled) {
statusElement.textContent = enabled ?
'Extension is active - listening for requests' :
'Extension is disabled';
}
// Test tone functions
function playTestTone(frequency, duration, volume = 0.1) {
if (!audioContext) {
initAudioContext();
if (!audioContext) return;
}
// Resume audio context if suspended
if (audioContext.state === 'suspended') {
audioContext.resume();
}
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.type = 'sine';
oscillator.frequency.setValueAtTime(frequency, audioContext.currentTime);
// Volume envelope
const now = audioContext.currentTime;
const fadeTime = 0.01;
gainNode.gain.setValueAtTime(0, now);
gainNode.gain.linearRampToValueAtTime(volume, now + fadeTime);
gainNode.gain.linearRampToValueAtTime(volume, now + duration/1000 - fadeTime);
gainNode.gain.linearRampToValueAtTime(0, now + duration/1000);
oscillator.start(now);
oscillator.stop(now + duration/1000);
}
// Test button event listeners
testTinyBtn.addEventListener('click', function() {
// Simulate tiny file (1KB) - high pitch, short duration
playTestTone(1500, 40, 0.05);
this.style.transform = 'scale(0.95)';
setTimeout(() => this.style.transform = '', 60);
console.log(`Playing Tiny test tone: 1500Hz, 40ms`);
});
// Test button event listeners
testSmallBtn.addEventListener('click', function() {
// Simulate small file (5KB) - high pitch, short duration
playTestTone(700, 80, 0.08);
this.style.transform = 'scale(0.95)';
setTimeout(() => this.style.transform = '', 100);
console.log(`Playing Small test tone: 700Hz, 80ms`);
});
testMediumBtn.addEventListener('click', function() {
// Simulate medium file (50KB) - medium pitch, medium duration
playTestTone(350, 150, 0.12);
this.style.transform = 'scale(0.95)';
setTimeout(() => this.style.transform = '', 150);
console.log(`Playing Medium test tone: 350Hz, 150ms`);
});
testLargeBtn.addEventListener('click', function() {
// Simulate large file (500KB) - low pitch, long duration
playTestTone(150, 250, 0.15);
this.style.transform = 'scale(0.95)';
setTimeout(() => this.style.transform = '', 250);
console.log(`Playing Large test tone: 150Hz, 250ms`);
});
testXLargeBtn.addEventListener('click', function() {
// Simulate Xlarge file (1500KB) - low pitch, long duration
playTestTone(100, 400, 0.18);
this.style.transform = 'scale(0.95)';
setTimeout(() => this.style.transform = '', 400);
console.log(`Playing X-Large test tone: 100Hz, 400ms`);
});
// Handle user gesture requirement for audio
document.addEventListener('click', function() {
if (audioContext && audioContext.state === 'suspended') {
audioContext.resume();
}
}, { once: true });
});