lainchan/settings.js

57 lines
2.0 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);
setDefault("videomuted", false);
// Create settings menu
var settingsMenu = document.createElement("span");
settingsMenu.className = "settings";
settingsMenu.innerHTML = '<span>[Settings]</span>'
+ '<div style="display: none; text-align: left; position: absolute; right: 0px; margin-left: -999em; margin-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>'
+ '<label><input type="checkbox" name="videomuted">Open videos muted</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++) {
var box = settingsItems[i];
box.checked = setting(box.name);
2013-11-09 04:11:22 -05:00
}
}
function setupCheckbox(box) {
if (box.addEventListener) box.addEventListener("change", function(e) {
2013-11-09 04:11:22 -05:00
localStorage[box.name] = JSON.stringify(box.checked);
}, 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++) {
setupCheckbox(settingsItems[i]);
}
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);
}