@@ -10,6 +10,8 @@ function save_options() {
10
10
var style_right = document . getElementById ( "style_right" ) . value ;
11
11
var style_top = document . getElementById ( "style_top" ) . value ;
12
12
var z_index = document . getElementById ( "z_index" ) . value ;
13
+ var domains_array = domains_list_to_array ( ) ;
14
+
13
15
14
16
chrome . storage . sync . set ( {
15
17
"mil_time" : mil_time ,
@@ -21,7 +23,8 @@ function save_options() {
21
23
"font_family" : font_family ,
22
24
"style_right" : style_right ,
23
25
"style_top" : style_top ,
24
- "z_index" : z_index
26
+ "z_index" : z_index ,
27
+ "domains_array" : domains_array
25
28
} ) ;
26
29
27
30
// Update status to let user know options were saved.
@@ -44,12 +47,28 @@ function restore_options() {
44
47
document . getElementById ( "style_right" ) . value = values [ "style_right" ] ;
45
48
document . getElementById ( "style_top" ) . value = values [ "style_top" ] ;
46
49
document . getElementById ( "z_index" ) . value = values [ "z_index" ] ;
50
+ domains_array = values [ "domains_array" ] ;
51
+ recreate_domains_list_in_html ( domains_array ) ;
47
52
} ) ;
48
53
}
49
54
50
55
document . addEventListener ( 'DOMContentLoaded' , restore_options ) ;
51
56
document . querySelector ( '#save' ) . addEventListener ( 'click' , save_options ) ;
52
57
58
+ // To let function get not Window as 'this', but exactly those element, which called function.
59
+ document . getElementById ( "add_domain" ) . addEventListener ( 'click' , function ( ) {
60
+ add_this_domain ( this ) ;
61
+ } ) ;
62
+
63
+ document . getElementById ( "switch_to_page_1" ) . addEventListener ( 'click' , function ( ) {
64
+ // to hide, to show
65
+ switch_options_page ( "page_2" , "page_1" ) ;
66
+ switch_options_page ( "page_2_button" , "page_1_button" ) ;
67
+ } ) ;
68
+ document . getElementById ( "switch_to_page_2" ) . addEventListener ( 'click' , function ( ) {
69
+ switch_options_page ( "page_1" , "page_2" ) ;
70
+ switch_options_page ( "page_1_button" , "page_2_button" ) ;
71
+ } ) ;
53
72
54
73
let clock_visibility = document . getElementById ( "hide_clock_checkbox" ) ;
55
74
set_state_of_hide_clock_checkbox ( ) ;
@@ -116,3 +135,128 @@ function send_message_to_tabs( tabs ) {
116
135
// }
117
136
}
118
137
}
138
+
139
+ function switch_options_page ( to_hide , to_show ) {
140
+ // console.log( "switch_options_page " + to_hide + " " + to_show + " worked" );
141
+ document . getElementById ( to_hide ) . style . display = "none" ;
142
+ document . getElementById ( to_show ) . style . display = "block" ;
143
+ }
144
+
145
+
146
+ // For each saved domain create an div with it and add to page_2.
147
+ function recreate_domains_list_in_html ( domains ) {
148
+ domains . forEach ( function ( value ) {
149
+ domains_container . appendChild ( create_domain_element_div ( value ) ) ;
150
+ } )
151
+ }
152
+
153
+ // helper function
154
+ // Returns new element.
155
+ function create_domain_element_div ( value ) {
156
+ let domains_container = document . getElementById ( "domains_container" ) ;
157
+ let new_element = document . createElement ( "div" ) ;
158
+ // Or classList.add( "domain_element" ); if there some else classes
159
+ new_element . className = "domain_element" ;
160
+
161
+ let domain_input = document . createElement ( "input" ) ;
162
+ domain_input . value = value ;
163
+ domain_input . type = "text" ;
164
+ domain_input . placeholder = "www.domain.name" ;
165
+ domain_input . className = "domains" ;
166
+ // both work
167
+ // domain_input.setAttribute( "readonly", true);
168
+ domain_input . readOnly = true ;
169
+
170
+ let domain_button = document . createElement ( "input" ) ;
171
+ domain_button . type = "button" ;
172
+ domain_button . title = "Delete this domain" ;
173
+ domain_button . value = "❌" ;
174
+ domain_button . className = "delete_domain" ;
175
+ // instead of this, could be body.eventListener for all class members
176
+ domain_button . addEventListener ( 'click' , function ( ) {
177
+ delete_exactly_this_domain ( this ) ;
178
+ } ) ;
179
+
180
+ new_element . appendChild ( domain_input ) ;
181
+ new_element . appendChild ( domain_button ) ;
182
+ return new_element ;
183
+ }
184
+
185
+ // Also sort result
186
+ function domains_list_to_array (
187
+ /* takes nothing, as saves current state of html elements */
188
+ ) {
189
+ let domains_container = document . getElementById ( "domains_container" ) ;
190
+ const domains_elements = domains_container . getElementsByClassName ( "domains" ) ;
191
+ let temp_array = [ ] ;
192
+
193
+ Array . prototype . filter . call (
194
+ domains_elements , ( domains_element ) => {
195
+ if ( domains_element . value != "" ) {
196
+ temp_array . push ( domains_element . value ) ;
197
+ }
198
+ } ) ;
199
+ return temp_array . sort ( ) ;
200
+ }
201
+
202
+ function delete_exactly_this_domain ( input_button ) {
203
+ // if( input_button == "undefined" ) {
204
+ // console.log( "delete_exactly_this_domain: input_button is undefined" );
205
+ // return;
206
+ // }
207
+ let parent = input_button . parentNode ;
208
+ let first_child = parent . firstElementChild ;
209
+ let value_to_delete = first_child . value ;
210
+ // console.log( "delete_exactly_this_domain: value_to_delete = " + value_to_delete );
211
+ // Creates new array without one element.
212
+ domains_array = domains_array . filter ( element => element != value_to_delete ) ;
213
+ parent . remove ( ) ;
214
+ // no auto-save of changes
215
+ }
216
+
217
+ // Almost the same as upper one.
218
+ // Can't be used in file:///options.html debug out of add-on debugging.
219
+ function add_this_domain ( input_button ) {
220
+ // if( input_button == undefined ) {
221
+ // console.log( "add_this_domain: input_button is absent" );
222
+ // return;
223
+ // }
224
+ // .domain_element
225
+ let parent = input_button . parentNode ;
226
+ // if( parent == undefined ) {
227
+ // console.log( "cannot add_this_domain, input object is missing = "
228
+ // + input_button );
229
+ // return;
230
+ // }
231
+ let first_child = parent . firstElementChild ;
232
+ let value_to_add = first_child . value ;
233
+ // maybe should be check for at least one '.' in domain name
234
+ if ( value_to_add != "" ) {
235
+ // console.log( "add_this_domain: value_to_add " + value_to_add );
236
+ if ( is_element_unique_for_array ( value_to_add , domains_array ) ) {
237
+ domains_array . push ( value_to_add ) ;
238
+ domains_array . sort ( ) ;
239
+ // console.log( "add_this_domain: sorted array = " + domains_array );
240
+ let new_element = create_domain_element_div ( value_to_add )
241
+ // #domains_container
242
+ let parent_of_parent = parent . parentNode ;
243
+ // insert right after "#add_domain".parent; aka insertAfter
244
+ parent_of_parent . insertBefore ( new_element , parent . nextSibling ) ;
245
+ // have to click save settings for saving that list as for now
246
+ }
247
+ }
248
+ first_child . value = "" ; // clear the field
249
+ }
250
+
251
+ // helper function
252
+ // what, into what
253
+ function is_element_unique_for_array ( element , array ) {
254
+ let answer = true ;
255
+ for ( let index = 0 ; index < array . length ; index ++ ) {
256
+ if ( array [ index ] == element ) {
257
+ answer = false ;
258
+ break ;
259
+ }
260
+ }
261
+ return answer ;
262
+ }
0 commit comments