-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
24 lines (23 loc) · 975 Bytes
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
chrome.runtime.onInstalled.addListener(() => {
console.log('Medium Reads Logger extension installed!');
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'logRead') {
console.log('Logging read:', message);
chrome.storage.local.get('mediumReads', (data) => {
const reads = data.mediumReads || [];
const isAlreadyLogged = reads.some(read => read.url === message.url);
if (!isAlreadyLogged) {
reads.push({ title: message.title, url: message.url, timestamp: new Date().toISOString() });
chrome.storage.local.set({ mediumReads: reads }, () => {
console.log('Read logged successfully!');
sendResponse({ success: true });
});
} else {
console.log('URL already logged, skipping.');
sendResponse({ success: false, message: 'Duplicate URL' });
}
});
return true; // Keep the message channel open for async response
}
});