1
1
var timeoutId ;
2
2
const notes = document . getElementById ( "notes" ) ;
3
+ // Add event listener for keyup to save notes automatically
3
4
document . addEventListener ( "keyup" , logKey ) ;
4
5
6
+ // Determine browser type and set appropriate browser object
5
7
const browser_type = getBrowser ( ) ;
6
8
if ( browser_type === "Chrome" ) {
7
9
var browser_obj = chrome ;
8
10
} else {
9
11
var browser_obj = browser ;
10
12
}
11
13
14
+ // Listen for tab and window focus changes
12
15
browser_obj . tabs . onActivated . addListener ( tabOpen ) ;
13
16
browser_obj . windows . onFocusChanged . addListener ( tabOpen ) ;
14
17
18
+ // Debounce the save operation to prevent too frequent storage updates
15
19
function logKey ( e ) {
16
20
clearTimeout ( timeoutId ) ;
17
21
timeoutId = setTimeout ( function ( ) {
18
22
saveToDB ( ) ;
19
23
} , 10 ) ;
20
24
}
21
25
26
+ // Detect browser type for compatibility
22
27
function getBrowser ( ) {
23
28
if ( typeof chrome !== "undefined" ) {
24
29
if ( typeof browser !== "undefined" ) {
@@ -31,6 +36,7 @@ function getBrowser() {
31
36
}
32
37
}
33
38
39
+ // Save notes to browser's sync storage
34
40
function saveToDB ( ) {
35
41
data = {
36
42
tab_note : document . querySelector ( "#notes" ) . value ,
@@ -42,6 +48,7 @@ function saveToDB() {
42
48
}
43
49
}
44
50
51
+ // Load notes when tab changes or window focuses
45
52
function tabOpen ( tab ) {
46
53
if ( browser_type === "Chrome" ) {
47
54
chrome . storage . sync . get ( [ "tab_note" ] , function ( result ) {
@@ -58,6 +65,7 @@ function tabOpen(tab) {
58
65
}
59
66
}
60
67
68
+ // Initialize notes when page loads
61
69
window . addEventListener ( "load" , ( ) => {
62
70
tabOpen ( ) ;
63
71
} ) ;
0 commit comments