11var timeoutId ;
22const notes = document . getElementById ( "notes" ) ;
3+ // Add event listener for keyup to save notes automatically
34document . addEventListener ( "keyup" , logKey ) ;
45
6+ // Determine browser type and set appropriate browser object
57const browser_type = getBrowser ( ) ;
68if ( browser_type === "Chrome" ) {
79 var browser_obj = chrome ;
810} else {
911 var browser_obj = browser ;
1012}
1113
14+ // Listen for tab and window focus changes
1215browser_obj . tabs . onActivated . addListener ( tabOpen ) ;
1316browser_obj . windows . onFocusChanged . addListener ( tabOpen ) ;
1417
18+ // Debounce the save operation to prevent too frequent storage updates
1519function logKey ( e ) {
1620 clearTimeout ( timeoutId ) ;
1721 timeoutId = setTimeout ( function ( ) {
1822 saveToDB ( ) ;
1923 } , 10 ) ;
2024}
2125
26+ // Detect browser type for compatibility
2227function getBrowser ( ) {
2328 if ( typeof chrome !== "undefined" ) {
2429 if ( typeof browser !== "undefined" ) {
@@ -31,6 +36,7 @@ function getBrowser() {
3136 }
3237}
3338
39+ // Save notes to browser's sync storage
3440function saveToDB ( ) {
3541 data = {
3642 tab_note : document . querySelector ( "#notes" ) . value ,
@@ -42,6 +48,7 @@ function saveToDB() {
4248 }
4349}
4450
51+ // Load notes when tab changes or window focuses
4552function tabOpen ( tab ) {
4653 if ( browser_type === "Chrome" ) {
4754 chrome . storage . sync . get ( [ "tab_note" ] , function ( result ) {
@@ -58,6 +65,7 @@ function tabOpen(tab) {
5865 }
5966}
6067
68+ // Initialize notes when page loads
6169window . addEventListener ( "load" , ( ) => {
6270 tabOpen ( ) ;
6371} ) ;
0 commit comments