1+ const title = document . getElementById ( "title" ) ;
2+ const author = document . getElementById ( "author" ) ;
3+ const pages = document . getElementById ( "pages" ) ;
4+ const check = document . getElementById ( "check" ) ;
5+ const table = document . getElementById ( "display" ) ;
6+ const submitBtn = document . getElementById ( "submit-book-btn" ) ;
7+
18let myLibrary = [ ] ;
29
3- window . addEventListener ( "load" , function ( e ) {
10+ window . addEventListener ( "load" , function ( ) {
411 populateStorage ( ) ;
512 render ( ) ;
613} ) ;
714
15+ submitBtn . addEventListener ( "click" , addBook ) ;
16+
817function populateStorage ( ) {
9- if ( myLibrary . length == 0 ) {
18+ const storedLibrary = localStorage . getItem ( "myLibrary" ) ;
19+ if ( storedLibrary ) {
20+ myLibrary = JSON . parse ( storedLibrary ) ;
21+ } else {
22+ // Default books if nothing in storage
1023 let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , "252" , true ) ;
1124 let book2 = new Book (
1225 "The Old Man and the Sea" ,
@@ -16,30 +29,35 @@ function populateStorage() {
1629 ) ;
1730 myLibrary . push ( book1 ) ;
1831 myLibrary . push ( book2 ) ;
19- render ( ) ;
32+ saveStorage ( ) ;
2033 }
2134}
2235
23- const title = document . getElementById ( "title" ) ;
24- const author = document . getElementById ( "author" ) ;
25- const pages = document . getElementById ( "pages" ) ;
26- const check = document . getElementById ( "check" ) ;
36+ function saveStorage ( ) {
37+ localStorage . setItem ( "myLibrary" , JSON . stringify ( myLibrary ) ) ;
38+ }
2739
2840//check the right input from forms and if its ok -> add the new book (object in array)
2941//via Book function and start render function
30- function submit ( ) {
31- if (
32- title . value == null ||
33- title . value == "" ||
34- pages . value == null ||
35- pages . value == ""
36- ) {
42+ function addBook ( e ) {
43+ // Prevent form submission if we wrap this in a form later,
44+ // though currently it's just a button. Good practice.
45+ if ( e ) e . preventDefault ( ) ;
46+
47+ if ( ! title . value || ! author . value || ! pages . value ) {
3748 alert ( "Please fill all fields!" ) ;
3849 return false ;
3950 } else {
40- let book = new Book ( title . value , title . value , pages . value , check . checked ) ;
41- library . push ( book ) ;
51+ let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
52+ myLibrary . push ( book ) ;
53+ saveStorage ( ) ;
4254 render ( ) ;
55+
56+ // Clear inputs
57+ title . value = "" ;
58+ author . value = "" ;
59+ pages . value = "" ;
60+ check . checked = false ;
4361 }
4462}
4563
@@ -51,53 +69,59 @@ function Book(title, author, pages, check) {
5169}
5270
5371function render ( ) {
54- let table = document . getElementById ( "display" ) ;
5572 let rowsNumber = table . rows . length ;
5673 //delete old table
57- for ( let n = rowsNumber - 1 ; n > 0 ; n -- {
74+ for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
5875 table . deleteRow ( n ) ;
5976 }
6077 //insert updated row and cells
6178 let length = myLibrary . length ;
6279 for ( let i = 0 ; i < length ; i ++ ) {
63- let row = table . insertRow ( 1 ) ;
80+ // Insert at the end of the table
81+ let row = table . insertRow ( - 1 ) ;
6482 let titleCell = row . insertCell ( 0 ) ;
6583 let authorCell = row . insertCell ( 1 ) ;
6684 let pagesCell = row . insertCell ( 2 ) ;
6785 let wasReadCell = row . insertCell ( 3 ) ;
6886 let deleteCell = row . insertCell ( 4 ) ;
69- titleCell . innerHTML = myLibrary [ i ] . title ;
70- authorCell . innerHTML = myLibrary [ i ] . author ;
71- pagesCell . innerHTML = myLibrary [ i ] . pages ;
87+
88+ // SECURITY: Use textContent to prevent XSS
89+ titleCell . textContent = myLibrary [ i ] . title ;
90+ authorCell . textContent = myLibrary [ i ] . author ;
91+ pagesCell . textContent = myLibrary [ i ] . pages ;
7292
7393 //add and wait for action for read/unread button
7494 let changeBut = document . createElement ( "button" ) ;
75- changeBut . id = i ;
95+ // Removed arbitrary index-based ID
7696 changeBut . className = "btn btn-success" ;
7797 wasReadCell . appendChild ( changeBut ) ;
98+
7899 let readStatus = "" ;
79100 if ( myLibrary [ i ] . check == false ) {
80- readStatus = "Yes" ;
81- } else {
82101 readStatus = "No" ;
102+ } else {
103+ readStatus = "Yes" ;
83104 }
84- changeBut . innerText = readStatus ;
105+ changeBut . textContent = readStatus ;
85106
86107 changeBut . addEventListener ( "click" , function ( ) {
87108 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
109+ saveStorage ( ) ;
88110 render ( ) ;
89111 } ) ;
90112
91113 //add delete button to every row and render again
92114 let delButton = document . createElement ( "button" ) ;
93- delBut . id = i + 5 ;
94- deleteCell . appendChild ( delBut ) ;
95- delBut . className = "btn btn-warning" ;
96- delBut . innerHTML = "Delete" ;
97- delBut . addEventListener ( "clicks" , function ( ) {
98- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
99- myLibrary . splice ( i , 1 ) ;
100- render ( ) ;
115+ // Removed arbitrary index-based ID
116+ deleteCell . appendChild ( delButton ) ;
117+ delButton . className = "btn btn-warning" ;
118+ delButton . textContent = "Delete" ;
119+ delButton . addEventListener ( "click" , function ( ) {
120+ if ( confirm ( `Are you sure you want to delete: ${ myLibrary [ i ] . title } ?` ) ) {
121+ myLibrary . splice ( i , 1 ) ;
122+ saveStorage ( ) ;
123+ render ( ) ;
124+ }
101125 } ) ;
102126 }
103127}
0 commit comments