lainchan/settings.js

65 lines
2.5 KiB
JavaScript
Raw Normal View History

// Scripts obtain settings by calling this function
function setting(name) {
return JSON.parse(localStorage[name]);
}
// Default settings
function setDefault(name, value) {
if (!(name in localStorage)) {
localStorage[name] = JSON.stringify(value);
}
}
setDefault("videoexpand", true);
setDefault("videohover", false);
2014-03-04 19:45:53 -05:00
setDefault("videovolume", 1.0);
// Create settings menu
var settingsMenu = document.createElement("span");
settingsMenu.className = "settings";
settingsMenu.innerHTML = '<span>[Settings]</span>'
2013-11-14 01:41:35 -05:00
+ '<div style="display: none; text-align: left; position: absolute; right: 0px; margin-left: -999em; margin-top: -1px; padding-top: 1px;">'
+ '<label><input type="checkbox" name="videoexpand">Expand videos inline</label><br>'
2013-11-09 17:28:49 -05:00
+ '<label><input type="checkbox" name="videohover">Play videos on hover</label><br>'
2014-03-04 19:45:53 -05:00
+ '<label><input type="range" name="videovolume" min="0" max="1" step="0.01" style="width: 4em; height: 1ex; vertical-align: middle; margin: 0px;">Default volume</label><br>'
2013-11-09 04:11:22 -05:00
+ '</div>';
function refreshSettings() {
var settingsItems = settingsMenu.getElementsByTagName("input");
2013-11-09 04:11:22 -05:00
for (var i = 0; i < settingsItems.length; i++) {
2014-03-04 19:45:53 -05:00
var control = settingsItems[i];
if (control.type == "checkbox") {
control.checked = setting(control.name);
} else if (control.type == "range") {
control.value = setting(control.name);
}
2013-11-09 04:11:22 -05:00
}
}
2014-03-04 19:45:53 -05:00
function setupControl(control) {
if (control.addEventListener) control.addEventListener("change", function(e) {
if (control.type == "checkbox") {
localStorage[control.name] = JSON.stringify(control.checked);
} else if (control.type == "range") {
localStorage[control.name] = JSON.stringify(control.value);
}
}, false);
2013-11-09 04:11:22 -05:00
}
refreshSettings();
var settingsItems = settingsMenu.getElementsByTagName("input");
2013-11-09 04:11:22 -05:00
for (var i = 0; i < settingsItems.length; i++) {
2014-03-04 19:45:53 -05:00
setupControl(settingsItems[i]);
2013-11-09 04:11:22 -05:00
}
if (settingsMenu.addEventListener) {
settingsMenu.addEventListener("mouseover", function(e) {
refreshSettings();
settingsMenu.getElementsByTagName("span")[0].style.fontWeight = "bold";
settingsMenu.getElementsByTagName("div")[0].style.display = "block";
}, false);
settingsMenu.addEventListener("mouseout", function(e) {
settingsMenu.getElementsByTagName("span")[0].style.fontWeight = "normal";
settingsMenu.getElementsByTagName("div")[0].style.display = "none";
}, false);
}