11var storage = new LocalStorage ( ) ;
22var blackList = [ ] ;
33var restrictionList = [ ] ;
4- var blockBtnList = [ 'settingsBtn' , 'restrictionsBtn' , 'aboutBtn' ] ;
5- var blockList = [ 'settingsBlock' , 'restrictionsBlock' , 'aboutBlock' ] ;
4+ var notifyList = [ ] ;
5+ var blockBtnList = [ 'settingsBtn' , 'restrictionsBtn' , 'notifyBtn' , 'aboutBtn' ] ;
6+ var blockList = [ 'settingsBlock' , 'restrictionsBlock' , 'notifyBlock' , 'aboutBlock' ] ;
7+
68
79document . addEventListener ( 'DOMContentLoaded' , function ( ) {
810 document . getElementById ( 'settingsBtn' ) . addEventListener ( 'click' , function ( ) {
@@ -11,6 +13,9 @@ document.addEventListener('DOMContentLoaded', function () {
1113 document . getElementById ( 'restrictionsBtn' ) . addEventListener ( 'click' , function ( ) {
1214 setBlockEvent ( 'restrictionsBtn' , 'restrictionsBlock' ) ;
1315 } ) ;
16+ document . getElementById ( 'notifyBtn' ) . addEventListener ( 'click' , function ( ) {
17+ setBlockEvent ( 'notifyBtn' , 'notifyBlock' ) ;
18+ } ) ;
1419 document . getElementById ( 'aboutBtn' ) . addEventListener ( 'click' , function ( ) {
1520 setBlockEvent ( 'aboutBtn' , 'aboutBlock' ) ;
1621 loadVersion ( ) ;
@@ -22,10 +27,13 @@ document.addEventListener('DOMContentLoaded', function () {
2227 exportToCSV ( ) ;
2328 } ) ;
2429 document . getElementById ( 'addBlackSiteBtn' ) . addEventListener ( 'click' , function ( ) {
25- addNewBlackSiteClickHandler ( ) ;
30+ addNewSiteClickHandler ( 'addBlackSiteLbl' , null , actionAddBlackSiteToList , 'notifyForBlackList' ) ;
2631 } ) ;
2732 document . getElementById ( 'addRestrictionSiteBtn' ) . addEventListener ( 'click' , function ( ) {
28- addNewRestrictionSiteClickHandler ( ) ;
33+ addNewSiteClickHandler ( 'addRestrictionSiteLbl' , 'addRestrictionTimeLbl' , actionAddRectrictionToList , 'notifyForRestrictionList' ) ;
34+ } ) ;
35+ document . getElementById ( 'addNotifySiteBtn' ) . addEventListener ( 'click' , function ( ) {
36+ addNewSiteClickHandler ( 'addNotifySiteLbl' , 'addNotifyTimeLbl' , actionAddNotifyToList , 'notifyForNotifyList' ) ;
2937 } ) ;
3038 document . getElementById ( 'viewTimeInBadge' ) . addEventListener ( 'change' , function ( ) {
3139 storage . saveValue ( SETTINGS_VIEW_TIME_IN_BADGE , this . checked ) ;
@@ -84,6 +92,12 @@ function loadSettings() {
8492 restrictionList = [ ] ;
8593 viewRestrictionList ( items ) ;
8694 } ) ;
95+ storage . getValue ( STORAGE_NOTIFICATION_LIST , function ( items ) {
96+ notifyList = items ;
97+ if ( notifyList === undefined )
98+ notifyList = [ ] ;
99+ viewNotificationList ( items ) ;
100+ } ) ;
87101 checkPermissions ( ) ;
88102}
89103
@@ -128,10 +142,18 @@ function setUIForAnyPermission() {
128142 document . getElementById ( 'grantPermission' ) . setAttribute ( 'disabled' , 'true' ) ;
129143}
130144
145+ function viewNotificationList ( items ) {
146+ if ( items !== undefined ) {
147+ for ( var i = 0 ; i < items . length ; i ++ ) {
148+ addDomainToEditableListBox ( items [ i ] , 'notifyList' , actionEditSite , deleteNotificationSite , updateItemFromNotifyList , updateNotificationList ) ;
149+ }
150+ }
151+ }
152+
131153function viewRestrictionList ( items ) {
132154 if ( items !== undefined ) {
133155 for ( var i = 0 ; i < items . length ; i ++ ) {
134- addDomainToRestrictionListBox ( items [ i ] ) ;
156+ addDomainToEditableListBox ( items [ i ] , 'restrictionsList' , actionEditSite , deleteRestrictionSite , updateItemFromResctrictoinList , updateRestrictionList ) ;
135157 }
136158 }
137159}
@@ -174,37 +196,60 @@ function viewNotify(elementName) {
174196 setTimeout ( function ( ) { document . getElementById ( elementName ) . hidden = true ; } , 3000 ) ;
175197}
176198
177- function addNewBlackSiteClickHandler ( ) {
178- var newBlackSite = document . getElementById ( 'addBlackSiteLbl' ) . value ;
179- if ( newBlackSite !== '' ) {
180- if ( ! isContainsBlackSite ( newBlackSite ) ) {
181- addDomainToListBox ( newBlackSite ) ;
182- if ( blackList === undefined )
183- blackList = [ ] ;
184- blackList . push ( newBlackSite ) ;
185- document . getElementById ( 'addBlackSiteLbl' ) . value = '' ;
186-
187- updateBlackList ( ) ;
188- } else viewNotify ( 'notifyForBlackList' ) ;
189- }
199+ function actionAddRectrictionToList ( newSite , newTime ) {
200+ if ( ! isContainsRestrictionSite ( newSite ) ) {
201+ var restriction = new Restriction ( newSite , newTime ) ;
202+ addDomainToEditableListBox ( restriction , 'restrictionsList' , actionEditSite , deleteRestrictionSite , updateItemFromResctrictoinList , updateRestrictionList ) ;
203+ if ( restrictionList === undefined )
204+ restrictionList = [ ] ;
205+ restrictionList . push ( restriction ) ;
206+ document . getElementById ( 'addRestrictionSiteLbl' ) . value = '' ;
207+ document . getElementById ( 'addRestrictionTimeLbl' ) . value = '' ;
208+
209+ updateRestrictionList ( ) ;
210+
211+ return true ;
212+ } else return false ;
190213}
191214
192- function addNewRestrictionSiteClickHandler ( ) {
193- var newRestrictionSite = document . getElementById ( 'addRestrictionSiteLbl' ) . value ;
194- var newRestrictionTime = document . getElementById ( 'addRestrictionTimeLbl' ) . value ;
195- if ( newRestrictionSite !== '' && newRestrictionTime !== '' ) {
196- if ( ! isContainsRestrictionSite ( newRestrictionSite ) ) {
197- var restriction = new Restriction ( newRestrictionSite , newRestrictionTime ) ;
198- addDomainToRestrictionListBox ( restriction ) ;
199- if ( restrictionList === undefined )
200- restrictionList = [ ] ;
201- restrictionList . push ( restriction ) ;
202- document . getElementById ( 'addRestrictionSiteLbl' ) . value = '' ;
203- document . getElementById ( 'addRestrictionTimeLbl' ) . value = '' ;
204-
205- updateRestrictionList ( ) ;
206- }
207- else viewNotify ( 'notifyForRestrictionList' ) ;
215+ function actionAddBlackSiteToList ( newSite ) {
216+ if ( ! isContainsBlackSite ( newSite ) ) {
217+ addDomainToListBox ( newSite ) ;
218+ if ( blackList === undefined )
219+ blackList = [ ] ;
220+ blackList . push ( newSite ) ;
221+ document . getElementById ( 'addBlackSiteLbl' ) . value = '' ;
222+
223+ updateBlackList ( ) ;
224+
225+ return true ;
226+ } else return false ;
227+ }
228+
229+ function actionAddNotifyToList ( newSite , newTime ) {
230+ if ( ! isContainsNotificationSite ( newSite ) ) {
231+ var notify = new Notification ( newSite , newTime ) ;
232+ addDomainToEditableListBox ( notify , 'notifyList' , actionEditSite , deleteNotificationSite , updateItemFromNotifyList , updateNotificationList ) ;
233+ if ( notifyList === undefined )
234+ notifyList = [ ] ;
235+ notifyList . push ( notify ) ;
236+ document . getElementById ( 'addNotifySiteLbl' ) . value = '' ;
237+ document . getElementById ( 'addNotifyTimeLbl' ) . value = '' ;
238+
239+ updateNotificationList ( ) ;
240+
241+ return true ;
242+ } else return false ;
243+ }
244+
245+ function addNewSiteClickHandler ( lblName , timeName , actionCheck , notifyBlock ) {
246+ var newSite = document . getElementById ( lblName ) . value ;
247+ var newTime ;
248+ if ( timeName != null )
249+ newTime = document . getElementById ( timeName ) . value ;
250+ if ( newSite !== '' && ( newTime === undefined || ( newTime !== undefined && newTime !== '' ) ) ) {
251+ if ( ! actionCheck ( newSite , newTime ) )
252+ viewNotify ( notifyBlock ) ;
208253 }
209254}
210255
@@ -220,13 +265,13 @@ function addDomainToListBox(domain) {
220265 document . getElementById ( 'blackList' ) . appendChild ( li ) . appendChild ( del ) ;
221266}
222267
223- function addDomainToRestrictionListBox ( resctiction ) {
268+ function addDomainToEditableListBox ( entity , elementId , actionEdit , actionDelete , actionUpdateTimeFromList , actionUpdateList ) {
224269 var li = document . createElement ( 'li' ) ;
225270
226271 var domainLbl = document . createElement ( 'input' ) ;
227272 domainLbl . type = 'text' ;
228- domainLbl . classList . add ( 'readonly-input' , 'inline-block' , 'restriction -item' ) ;
229- domainLbl . value = resctiction . domain ;
273+ domainLbl . classList . add ( 'readonly-input' , 'inline-block' , 'element -item' ) ;
274+ domainLbl . value = entity . domain ;
230275 domainLbl . readOnly = true ;
231276 domainLbl . setAttribute ( 'name' , 'domain' ) ;
232277
@@ -235,15 +280,15 @@ function addDomainToRestrictionListBox(resctiction) {
235280 edit . height = 14 ;
236281 edit . src = '/icons/edit.png' ;
237282 edit . addEventListener ( 'click' , function ( e ) {
238- editRestrictionSite ( e ) ;
283+ actionEdit ( e , actionUpdateTimeFromList , actionUpdateList ) ;
239284 } ) ;
240285
241286 var del = document . createElement ( 'img' ) ;
242287 del . height = 12 ;
243288 del . src = '/icons/delete.png' ;
244289 del . classList . add ( 'margin-left-5' ) ;
245290 del . addEventListener ( 'click' , function ( e ) {
246- deleteRestrictionSite ( e ) ;
291+ actionDelete ( e , actionUpdateTimeFromList , actionUpdateList ) ;
247292 } ) ;
248293
249294 var bloc = document . createElement ( 'div' ) ;
@@ -256,11 +301,11 @@ function addDomainToRestrictionListBox(resctiction) {
256301 timeInput . classList . add ( 'clock' , 'clock-li-readonly' ) ;
257302 timeInput . setAttribute ( 'readonly' , true ) ;
258303 timeInput . setAttribute ( 'name' , 'time' ) ;
259- timeInput . value = convertShortSummaryTimeToString ( resctiction . time ) ;
304+ timeInput . value = convertShortSummaryTimeToString ( entity . time ) ;
260305 bloc . appendChild ( timeInput ) ;
261306
262307 var hr = document . createElement ( 'hr' ) ;
263- var li = document . getElementById ( 'restrictionsList' ) . appendChild ( li ) ;
308+ var li = document . getElementById ( elementId ) . appendChild ( li ) ;
264309 li . appendChild ( domainLbl ) ;
265310 li . appendChild ( del ) ;
266311 li . appendChild ( edit ) ;
@@ -284,7 +329,16 @@ function deleteRestrictionSite(e) {
284329 updateRestrictionList ( ) ;
285330}
286331
287- function editRestrictionSite ( e ) {
332+ function deleteNotificationSite ( e ) {
333+ var targetElement = e . path [ 1 ] ;
334+ var itemValue = targetElement . querySelector ( "[name='domain']" ) . value ;
335+ var item = notifyList . find ( x => x . domain == itemValue ) ;
336+ notifyList . splice ( notifyList . indexOf ( item ) , 1 ) ;
337+ document . getElementById ( 'notifyList' ) . removeChild ( targetElement ) ;
338+ updateNotificationList ( ) ;
339+ }
340+
341+ function actionEditSite ( e , actionUpdateTimeFromList , actionUpdateList ) {
288342 var targetElement = e . path [ 1 ] ;
289343 var domainElement = targetElement . querySelector ( '[name="domain"]' ) ;
290344 var timeElement = targetElement . querySelector ( '[name="time"]' ) ;
@@ -307,8 +361,8 @@ function editRestrictionSite(e) {
307361 var resultTime = convertShortSummaryTimeToString ( convertTimeToSummaryTime ( time ) ) ;
308362 timeElement . value = resultTime ;
309363
310- updateItemFromResctrictoinList ( domain , time ) ;
311- updateRestrictionList ( ) ;
364+ actionUpdateTimeFromList ( domain , time ) ;
365+ actionUpdateList ( ) ;
312366 }
313367 }
314368}
@@ -317,6 +371,10 @@ function isContainsRestrictionSite(domain) {
317371 return restrictionList . find ( x => x . domain == domain ) != undefined ;
318372}
319373
374+ function isContainsNotificationSite ( domain ) {
375+ return notifyList . find ( x => x . domain == domain ) != undefined ;
376+ }
377+
320378function isContainsBlackSite ( domain ) {
321379 return blackList . find ( x => x == domain ) != undefined ;
322380}
@@ -325,10 +383,18 @@ function updateItemFromResctrictoinList(domain, time) {
325383 restrictionList . find ( x => x . domain === domain ) . time = convertTimeToSummaryTime ( time ) ;
326384}
327385
386+ function updateItemFromNotifyList ( domain , time ) {
387+ notifyList . find ( x => x . domain === domain ) . time = convertTimeToSummaryTime ( time ) ;
388+ }
389+
328390function updateBlackList ( ) {
329391 storage . saveValue ( STORAGE_BLACK_LIST , blackList ) ;
330392}
331393
332394function updateRestrictionList ( ) {
333395 storage . saveValue ( STORAGE_RESTRICTION_LIST , restrictionList ) ;
396+ }
397+
398+ function updateNotificationList ( ) {
399+ storage . saveValue ( STORAGE_NOTIFICATION_LIST , notifyList ) ;
334400}
0 commit comments