11import { isAllowedUrl } from "./shared/allowed-patterns" ;
22
33function updateIcon ( tabId , isAllowed ) {
4- // console.log(`Updating icon for tab ${tabId}: ${isAllowed ? 'allowed' : 'not allowed'}`);
54 chrome . action . setIcon ( {
65 tabId,
76 path : isAllowed
8- ? {
9- 128 : "icons/icon.png"
10- }
11- : {
12- 128 : "icons/icon-disabled.png" ,
13- }
7+ ? { 128 : "icons/icon.png" }
8+ : { 128 : "icons/icon-disabled.png" }
149 } ) ;
1510}
1611
@@ -25,3 +20,62 @@ chrome.tabs.onActivated.addListener(({ tabId }) => {
2520 updateIcon ( tabId , isAllowedUrl ( tab . url ) ) ;
2621 } ) ;
2722} ) ;
23+
24+ function openSidebar ( tab ) {
25+ if ( typeof chrome . sidePanel !== "undefined" && chrome . sidePanel . open ) {
26+ // Chrome Side Panel API
27+ chrome . sidePanel . open ( { windowId : tab . windowId } ) ;
28+ } else if ( chrome . sidebarAction && chrome . sidebarAction . open ) {
29+ // Firefox Sidebar API
30+ chrome . sidebarAction . open ( ) ;
31+ } else {
32+ console . warn ( "No native sidebar API available." ) ;
33+ }
34+ }
35+
36+ function sendWhenReady ( msg , retries = 10 , delay = 150 ) {
37+ function attempt ( remaining ) {
38+ chrome . runtime . sendMessage ( { type : "ping" } , ( response ) => {
39+ if ( response === "pong" ) {
40+ chrome . runtime . sendMessage ( msg ) ;
41+ } else if ( remaining > 0 ) {
42+ setTimeout ( ( ) => attempt ( remaining - 1 ) , delay ) ;
43+ }
44+ } ) ;
45+ }
46+ attempt ( retries ) ;
47+ }
48+
49+ function showUnsupportedNotification ( tab ) {
50+ try {
51+ chrome . notifications . create (
52+ // use a unique id so repeated clicks update the same notification
53+ "marian-unsupported" ,
54+ {
55+ type : "basic" ,
56+ iconUrl : "icons/icon-shush.png" ,
57+ title : "Shhhh..." ,
58+ message : "You cannot do that here!"
59+ } ,
60+ ( id ) => {
61+ setTimeout ( ( ) => chrome . notifications . clear ( id || "marian-unsupported" ) , 3000 ) ;
62+ }
63+ ) ;
64+ } catch ( e ) {
65+ console . warn ( "Notification failed:" , e ) ;
66+ }
67+ }
68+
69+ chrome . action . onClicked . addListener ( ( tab ) => {
70+ if ( ! tab ?. url ) return ;
71+
72+ if ( ! isAllowedUrl ( tab . url ) ) {
73+ showUnsupportedNotification ( tab ) ;
74+ return ;
75+ }
76+
77+ openSidebar ( tab ) ;
78+ setTimeout ( ( ) => {
79+ sendWhenReady ( { type : "REFRESH_SIDEBAR" , url : tab . url } ) ;
80+ } , 300 ) ; // give the sidebar a moment to load
81+ } ) ;
0 commit comments