From 444515b9ccdc3ddb8119004a21cfa0eb067e3adb Mon Sep 17 00:00:00 2001 From: edoardoo Date: Fri, 10 Jul 2015 16:40:29 +0200 Subject: [PATCH] fixed missing setting crashes js on update interface --- main.js | 17 ++++++++-- preferences/preferences.js | 69 +++++++++++++++++++++++--------------- 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/main.js b/main.js index b7acd1b..238e0e5 100644 --- a/main.js +++ b/main.js @@ -7,6 +7,14 @@ function setupVideoObject(){ }; } +// not actually working: +// function getSetting( setting ){ +// var value = ""; +// var value = chrome.storage.sync.get( setting, function(obj){ +// value = obj[setting]; +// }); +// return value; +// } function setLilikLogo(){ jQuery(" header#top-nav a.logo").css({ @@ -129,16 +137,21 @@ function nightMode(){ if( hasComments()) setInvertCommentImages(); - if( isNightTime() ){ + if( isNightTime()){ toggleNight("on"); } } function isNightTime(){ - var nightHour = 19; var morningHour = 7; + + // if( getSetting("night_mode_starting_hour") ){ + // nightHour = getSetting("night_mode_starting_hour"); + // console.log(nightHour); + // } + var date = new Date(); var hours = date.getHours(); if( hours >= nightHour || hours <= morningHour) diff --git a/preferences/preferences.js b/preferences/preferences.js index db80db5..c7aa915 100644 --- a/preferences/preferences.js +++ b/preferences/preferences.js @@ -1,26 +1,35 @@ var settings = [ { - id: "1", + id: "long_post_visualization", name: "Long Post Visualization", description: "Select which way you prefer to display long posts", type: "radio", options: [ "Sidebar", "In place"] },{ - id: "2", + id: "night_mode_enabler", name: "Night Mode", description: "Enable or disable night mode", type: "checkbox" },{ - id: "3", + id: "night_mode_starting_hour", name: "Night Mode Starting Hour", description: "in hours. Ex: 19", - type: "text" + type: "range", + options: { + min: "0", + max: "24" + } },{ - id: "4", + id: "night_mode_ending_hour", name: "Night Mode Ending Hour", description: "in hours. Ex: 7", - type: "text" - }, + type: "range", + options: { + min: "0", + max: "24" + } + } + ]; // var settings = [ @@ -136,7 +145,7 @@ function addListeners(){ for (var i = rangeElements.length - 1; i >= 0; i--) { rangeElements[i].addEventListener('change', function( element ){ - var labelIdNumber = this.id.split("_")[1]; + var labelIdNumber = this.id.split(/_(.+)?/)[1]; updateRangeValueLabel( labelIdNumber, this.value); }); }; @@ -145,7 +154,7 @@ function addListeners(){ //saves the data into storage function saveSettings(){ var setting = this.id; - setting = setting.split("_")[1]; + setting = setting.split(/_(.+)?/)[1]; var type = this.getAttribute('data-type'); switch (type){ case "checkbox": @@ -184,27 +193,33 @@ function loadSettings( ){ } function updateSettings( storedSettings ){ + for( var settingId in storedSettings ){ settingId = String( settingId ); - settingObject = document.getElementById( "setting_"+settingId ); - type = settingObject.getAttribute('data-type'); - switch( type ){ - case ( "checkbox" ): - settingObject.childNodes[1].childNodes[0].checked = storedSettings[settingId]; - break; - case ( "radio" ): - document.getElementById( "radio_"+ settingId +"_"+ storedSettings[settingId] ).checked = true; - break; - case "range": - settingObject.childNodes[1].childNodes[0].value = storedSettings[settingId]; - updateRangeValueLabel( settingId , storedSettings[settingId] ); - break; - - case "text": - case "dropdown": - settingObject.childNodes[1].childNodes[0].value = storedSettings[settingId]; - break; + settingObject = document.getElementById( "setting_"+settingId ); + if( typeof settingObject != "undefined" && settingObject != null){ + + type = settingObject.getAttribute('data-type'); + + switch( type ){ + case ( "checkbox" ): + settingObject.childNodes[1].childNodes[0].checked = storedSettings[settingId]; + break; + case ( "radio" ): + document.getElementById( "radio_"+ settingId +"_"+ storedSettings[settingId] ).checked = true; + break; + case "range": + settingObject.childNodes[1].childNodes[0].value = storedSettings[settingId]; + updateRangeValueLabel( settingId , storedSettings[settingId] ); + break; + + case "text": + case "dropdown": + settingObject.childNodes[1].childNodes[0].value = storedSettings[settingId]; + break; + } + } }