@@ -14,6 +14,8 @@ import { MessageHandlerContext } from '@/common/messages/messages.types';
1414import browser from 'webextension-polyfill' ;
1515import { Page } from './models/page' ;
1616
17+ import NotificationsFilter , { INotificationsFilter } from './utils/helpers/notification-filter' ;
18+
1719export interface IMainMessage {
1820 badgeText : string ;
1921 domain : string ;
@@ -43,68 +45,67 @@ export class Main {
4345 * Display how many pages were found by updating the badge text
4446 */
4547 indicateCATPages ( pages : CATWikiPageSearchResults , domMessenger : IDOMMessengerInterface ) : void {
46- const totalPages = pages . totalPagesFound ;
47- console . log ( pages ) ;
48-
49- if ( totalPages > 0 ) {
50- this . onBadgeTextUpdate ( totalPages . toString ( ) , domMessenger ) ;
51-
52- Promise . all ( [
53- Preferences . getPreference ( Preferences . IS_ENABLED_KEY ) ,
54- Preferences . getPreference ( Preferences . BROWSER_NOTIFICATIONS_ENABLED_KEY ) ,
55- ] )
56- . then ( ( [ isEnabled , browserNotificationsEnabled ] ) => {
57- if ( isEnabled && browserNotificationsEnabled ) {
58- // Example: show a notification about the found pages
59- // NOTE: Requires "notifications" permission in your manifest.json
60- void browser . notifications . create ( {
61- type : 'basic' ,
62- iconUrl : browser . runtime . getURL ( 'alert.png' ) ,
63- title : 'CAT Pages Found' ,
64- message : `Found ${ pages . totalPagesFound . toString ( ) } page(s).` ,
65- } ) ;
66- }
67- } )
68- . catch ( ( error : unknown ) =>
69- console . error ( 'Failed to get preferences to send browser notification:' , error )
70- ) ;
71- Promise . all ( [
72- Preferences . getPreference ( Preferences . IS_ENABLED_KEY ) ,
73- Preferences . getPreference ( Preferences . PAGE_NOTIFICATIONS_ENABLED_KEY ) ,
74- ] )
75- . then ( ( [ isEnabled , pageNotificationsEnabled ] ) => {
76- if ( isEnabled && pageNotificationsEnabled ) {
77- const message = `Found ${ pages . totalPagesFound . toString ( ) } CAT page(s).` ;
78- const entries : Page [ ] = [ ] ;
79- pages . pageEntries . forEach ( ( page ) => {
80- entries . push ( page ) ;
81- } ) ;
82-
83- domMessenger
84- . showInPageNotification ( message , entries )
85- . then ( ( ) => console . log ( 'In-page notification shown.' ) )
86- . catch ( ( error : unknown ) => {
87- if ( error instanceof Error && error . message . includes ( 'Receiving end does not exist' ) ) {
88- console . warn (
89- `Failed to send in-page notification (tab might be inactive or closed/navigated away before message was sent): ${ error . message } `
90- ) ;
91- } else {
92- console . error (
93- 'Failed to show in-page notification due to unexpected error:' ,
94- error
95- ) ;
96- }
48+ Promise . all ( [
49+ Preferences . getPreference ( Preferences . IS_ENABLED_KEY ) ,
50+ Preferences . getPreference ( Preferences . BROWSER_NOTIFICATIONS_ENABLED_KEY ) ,
51+ Preferences . getPreference ( Preferences . PAGE_NOTIFICATIONS_ENABLED_KEY ) ,
52+ NotificationsFilter . get ( ) ,
53+ ] )
54+ . then ( ( [ isEnabled , browserNotificationsEnabled , pageNotificationsEnabled , filters ] ) => {
55+ if ( isEnabled ) {
56+ let pageEntries = pages . pageEntries ;
57+ pageEntries = NotificationsFilter . filterPages ( pageEntries , filters , 60 * 60 * 1000 ) ; // check if muted 1 hour
58+
59+ const totalPages = pageEntries . length ;
60+ console . log ( 'pageEntries' , pages . pageEntries , pageEntries ) ;
61+ if ( totalPages > 0 ) {
62+ this . onBadgeTextUpdate ( totalPages . toString ( ) , domMessenger ) ;
63+
64+ if ( browserNotificationsEnabled ) {
65+ // Example: show a notification about the found pages
66+ // NOTE: Requires "notifications" permission in your manifest.json
67+ void browser . notifications . create ( {
68+ type : 'basic' ,
69+ iconUrl : browser . runtime . getURL ( 'alert.png' ) ,
70+ title : 'CAT Pages Found' ,
71+ message : `Found ${ totalPages . toString ( ) } page(s).` ,
9772 } ) ;
73+ }
74+
75+ if ( pageNotificationsEnabled ) {
76+ const message = `Found ${ totalPages . toString ( ) } CAT page(s).` ;
77+ const entries : Page [ ] = [ ] ;
78+ pageEntries . forEach ( ( page ) => {
79+ entries . push ( page ) ;
80+ } ) ;
81+
82+ domMessenger
83+ . showInPageNotification ( message , entries )
84+ . then ( ( ) => console . log ( 'In-page notification shown.' ) )
85+ . catch ( ( error : unknown ) => {
86+ if (
87+ error instanceof Error &&
88+ error . message . includes ( 'Receiving end does not exist' )
89+ ) {
90+ console . warn (
91+ `Failed to send in-page notification (tab might be inactive or closed/navigated away before message was sent): ${ error . message } `
92+ ) ;
93+ } else {
94+ console . error (
95+ 'Failed to show in-page notification due to unexpected error:' ,
96+ error
97+ ) ;
98+ }
99+ } ) ;
100+ }
101+ } else {
102+ // Revert badge text back to "on" or "off" as set by indicateStatus
103+ console . log ( 'Resetting badge text' ) ;
104+ this . indicateStatus ( ) ;
98105 }
99- } )
100- . catch ( ( error : unknown ) =>
101- console . error ( 'Failed to get preferences to send in-page notification:' , error )
102- ) ;
103- } else {
104- // Revert badge text back to "on" or "off" as set by indicateStatus
105- console . log ( 'Resetting badge text' ) ;
106- this . indicateStatus ( ) ;
107- }
106+ }
107+ } )
108+ . catch ( ( error : unknown ) => console . error ( 'Failed to get preferences to send in-page notification:' , error ) ) ;
108109 }
109110
110111 notify ( message : string ) {
@@ -186,6 +187,22 @@ export class Main {
186187 await this . contentScanner . checkPageContents ( scannerParameters ) ;
187188 }
188189
190+ /**
191+ * Called when a page (tab) has finished loading.
192+ * Scans the domain and in-page contents, merges results,
193+ * and indicates how many CAT pages were found.
194+ */
195+ onNotifyUpdate ( pageId : number , action : string ) {
196+ const page : INotificationsFilter = {
197+ timestamp : 0 ,
198+ pageId : pageId ,
199+ } ;
200+ if ( action == 'mute' ) {
201+ page . timestamp = Date . now ( ) ;
202+ }
203+ NotificationsFilter . update ( page ) ;
204+ }
205+
189206 /**
190207 * Called when the extension is installed.
191208 * Initializes default settings and indicates current status.
0 commit comments