";
});
tableBody.innerHTML = newElement;
tableSettings.innerHTML += tableBody.innerHTML;
}
function addListeners(){
var settingsElement = document.getElementsByClassName("setting");
for (var i = settingsElement.length - 1; i >= 0; i--) {
settingsElement[i].addEventListener('change', saveSettings);
}
var rangeElements = document.getElementsByClassName("rangeSelector");
for (var i = rangeElements.length - 1; i >= 0; i--) {
rangeElements[i].addEventListener('change', function( element ){
var labelIdNumber = this.id.split(/_(.+)?/)[1];
updateRangeValueLabel( labelIdNumber, this.value);
});
};
}
//saves the data into storage
function saveSettings(){
var setting = this.id;
setting = setting.split(/_(.+)?/)[1];
var type = this.getAttribute('data-type');
switch (type){
case "checkbox":
var value = this.childNodes[1].childNodes[0].checked;
break;
case "radio":
var value = document.querySelector('input[name = "radio_'+setting+'"]:checked').value;
break;
case "text":
case "range":
case "dropdown":
var value = this.childNodes[1].childNodes[0].value;
break;
}
var obj = {};
obj[setting] = value;
chrome.storage.sync.set(
obj
, function(){
//callback function after saving the preferences
document.getElementById("bar").innerHTML = "Preferences saved, refresh 9gag pages to enable new preferences.";
});
}
//get the value stored in the storage and will be shown in the text box.
function loadSettings( ){
chrome.storage.sync.get( null ,function(obj){
updateSettings(obj);
});
}
function updateSettings( storedSettings ){
for( var settingId in storedSettings ){
settingId = String( settingId );
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;
}
}
}
}
function updateRangeValueLabel( labelIdNumber, value ){// console.log(this.value);
var label = document.getElementById("range_value_"+labelIdNumber);
label.innerHTML = value;
}