Skip to content

Commit cf7318a

Browse files
committed
Added badges that show the amount of summaries found for every domain (resolve issue #10)
1 parent 171f465 commit cf7318a

File tree

3 files changed

+115
-6
lines changed

3 files changed

+115
-6
lines changed

background.js

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,6 @@ chrome.runtime.onMessage.addListener(
198198
}
199199
});
200200
});
201-
} else if (request.showForm) {
202-
// console.log("Received message to show form");
203201
} else if (request.type === "showPreloader") {
204202
console.log("RECIEVED showPreloader message for" + request.summaryName + " on " + request.domain);
205203
// Add the link to loadingSummaries
@@ -510,4 +508,82 @@ function rootDomain(hostname) {
510508
return parts.join('.');
511509

512510
return parts.slice(-2).join('.');
513-
}
511+
}
512+
513+
514+
515+
516+
517+
518+
519+
520+
521+
522+
523+
524+
525+
526+
527+
528+
529+
530+
531+
532+
533+
534+
535+
chrome.tabs.onActivated.addListener(activeInfo => {
536+
updateBadgeOnTab(activeInfo.tabId);
537+
});
538+
539+
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
540+
if (changeInfo.status === 'complete' && tab.active) {
541+
updateBadgeOnTab(tabId);
542+
}
543+
});
544+
545+
function updateBadgeOnTab(tabId) {
546+
chrome.tabs.get(tabId, (tab) => {
547+
if (chrome.runtime.lastError || !tab || !tab.url) {
548+
console.log(chrome.runtime.lastError ? chrome.runtime.lastError.message : 'No tab or URL found');
549+
return;
550+
}
551+
const url = new URL(tab.url);
552+
const domain = rootDomain(url.hostname);
553+
checkAndFetchSummaryCount(domain, tabId);
554+
});
555+
}
556+
557+
function checkAndFetchSummaryCount(domain, tabId) {
558+
chrome.storage.local.get([domain], function(result) {
559+
if (result[domain] && new Date().getTime() - result[domain].timestamp < 86400000) { // 86400000 ms = 1 day
560+
// Data is still valid
561+
chrome.action.setBadgeText({text: result[domain].count.toString(), tabId: tabId});
562+
chrome.action.setBadgeBackgroundColor({color: '#22c55e', tabId: tabId});
563+
} else {
564+
// Data is outdated or not present, fetch new data
565+
fetchSummaryCount(domain, tabId);
566+
}
567+
});
568+
}
569+
570+
function fetchSummaryCount(domain, tabId) {
571+
fetch('https://docdecoder.app/getsumcount', {
572+
method: 'POST',
573+
headers: {
574+
'Content-Type': 'application/json'
575+
},
576+
body: JSON.stringify({ domain: domain })
577+
})
578+
.then(response => response.json())
579+
.then(data => {
580+
if (data && data.summariesCount) {
581+
chrome.storage.local.set({[domain]: {count: data.summariesCount, timestamp: new Date().getTime()}});
582+
chrome.action.setBadgeText({text: data.summariesCount.toString(), tabId: tabId});
583+
chrome.action.setBadgeBackgroundColor({color: '#22c55e', tabId: tabId});
584+
} else {
585+
chrome.action.setBadgeText({text: '', tabId: tabId});
586+
}
587+
})
588+
.catch(error => console.error('Error fetching summary count:', error));
589+
}

content.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,37 @@ function rootDomain(hostname) {
159159

160160

161161

162-
// works now, but only one preloader is showing at a time. Not huge deal, but should be fixed eventually
162+
// works now, but only one preloader is showing at a time. Not huge deal, but should be fixed eventually
163+
164+
165+
166+
167+
168+
169+
170+
171+
// Add a route that just takes the domain and returns the summary count for that domain. Then display a badge on the extension icon with the number of summaries for that domain.
172+
173+
// call to docdecoder.app/getsumcount with post and send the domain as the json body
174+
// response will be the number of summaries for that domain, as count
175+
// set the badge text to count
176+
177+
// const currentDomain = rootDomain(new URL(window.location.href).hostname);
178+
// const summaryCountRequestData = {
179+
// action: "getSummaryCount",
180+
// domain: currentDomain
181+
// };
182+
// fetch('https://docdecoder.app/getsumcount', {
183+
// method: 'POST',
184+
// headers: {
185+
// 'Content-Type': 'application/json'
186+
// },
187+
// body: JSON.stringify(summaryCountRequestData)
188+
// })
189+
// .then(response => response.json())
190+
// .then(data => {
191+
// console.log(data.summariesCount);
192+
// if (data.summariesCount > 0) {
193+
// chrome.runtime.sendMessage({badgeText: data.summariesCount.toString()});
194+
// }
195+
// });

manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 3,
33
"name": "DocDecoder - Summarize Web Policies With AI",
44
"description": "DocDecoder uses GPT-4 to generate clear, concise summaries of any site's legal policies for you to skim over before you accept them.",
5-
"version": "1.2.6",
5+
"version": "1.2.7",
66
"action": {
77
"default_popup": "popup.html",
88
"default_icon": {
@@ -19,7 +19,7 @@
1919
"background": {
2020
"service_worker": "background.js"
2121
},
22-
"permissions": ["activeTab", "storage", "notifications", "tabs"],
22+
"permissions": ["activeTab", "storage", "notifications", "tabs", "action"],
2323
"content_scripts": [
2424
{
2525
"matches": ["<all_urls>"],

0 commit comments

Comments
 (0)