-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
137 lines (120 loc) · 3.98 KB
/
Copy pathcontent.js
File metadata and controls
137 lines (120 loc) · 3.98 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
let isEnabled = true;
let audioContext = null;
let audioBuffer = null;
let alerts = {};
let refreshTimers = {};
// Initialize audio context and load sound
async function initAudio() {
try {
// Create audio context
audioContext = new AudioContext();
// Load the sound file
const response = await fetch(chrome.runtime.getURL('bell.mp3'));
const arrayBuffer = await response.arrayBuffer();
audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
} catch (error) {
console.error('Error initializing audio:', error);
}
}
// Function to play alert sound
function playAlertSound() {
if (!audioContext || !audioBuffer) {
console.log('Audio not initialized yet');
return;
}
try {
// Create source
const source = audioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioContext.destination);
// Play the sound
source.start(0);
} catch (error) {
console.error('Error playing sound:', error);
}
}
// Load settings from storage
function loadSettings() {
chrome.storage.sync.get(['alerts', 'isEnabled'], (result) => {
if (result.alerts) alerts = result.alerts;
if (result.isEnabled !== undefined) isEnabled = result.isEnabled;
// Set up refresh intervals for all alerts
setupRefreshIntervals();
});
}
// Scans page for the keywords
function scanPage() {
if (!isEnabled) return;
Object.entries(alerts).forEach(([id, config]) => {
if (window.location.href.includes(config.targetUrl) &&
document.body.textContent.includes(config.targetWord)) {
chrome.runtime.sendMessage({
action: "triggerAlarm",
alertId: id,
config: config
});
}
});
}
// Setup refresh intervals
function setupRefreshIntervals() {
// Clear all existing intervals
Object.values(refreshTimers).forEach(timer => clearInterval(timer));
refreshTimers = {};
// Only set up refresh if enabled
if (!isEnabled) return;
// Set up intervals for each alert
Object.entries(alerts).forEach(([id, config]) => {
if (window.location.href.includes(config.targetUrl)) {
console.log(`Setting up refresh interval for alert ${id}: ${config.refreshInterval} seconds`);
refreshTimers[id] = setInterval(() => {
console.log(`Refreshing page for alert ${id}...`);
location.reload();
}, config.refreshInterval * 1000);
}
});
}
// Listen for messages from popup and background
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "toggle") {
isEnabled = !isEnabled;
chrome.storage.sync.set({ isEnabled: isEnabled });
setupRefreshIntervals(); // Reset refresh intervals when toggled
sendResponse({ isEnabled: isEnabled });
} else if (request.action === "playAlertSound") {
// Initialize audio if not already done
if (!audioContext) {
initAudio().then(() => {
playAlertSound();
});
} else {
playAlertSound();
}
}
});
// Listen for storage changes
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'sync') {
if (changes.alerts || changes.isEnabled) {
loadSettings(); // Reload settings when they change
}
if (changes.isEnabled) {
isEnabled = changes.isEnabled.newValue;
}
}
});
// Initialize audio when user interacts with the page
document.addEventListener('click', () => {
if (!audioContext) {
initAudio();
}
}, { once: true });
// Run on page load
window.addEventListener("load", () => {
console.log('Page loaded, initializing...');
loadSettings();
// Initial scan after 3 seconds
setTimeout(() => {
scanPage();
}, 3000);
});