// ==UserScript== // @name InstaSynchP Settings // @namespace InstaSynchP // @description Provides the ability to store settings for the plugins // @version 1.1.2 // @author Zod- // @source https://github.com/Zod-/InstaSynchP-Settings // @license MIT // @require https://greasyfork.org/scripts/5647-instasynchp-library/code/code.js?version=49210 // @include *://instasync.com/r/* // @include *://*.instasync.com/r/* // @grant none // @run-at document-start // @downloadURL none // ==/UserScript== function SettingsField(opts) { 'use strict'; this.type = opts.type; this.label = opts.label || ''; this.id = opts.id.replace(/ /g, '-'); this.default = opts.default; this.title = opts.title || ''; this.size = opts.size || 2; this.options = opts.options; this.section = opts.section || ['General']; this.disabled = opts.disabled || false; this.tooltipPlacement = opts.tooltipPlacement || 'top'; this.destination = opts.destination || 'chat'; this.hidden = opts.hidden || false; this.$div = $('
'); this.$input = undefined; this.$tooltip = undefined; this.oldVal = undefined; this.val = undefined; this.init(); this.buildDiv(); } SettingsField.prototype.init = function () { 'use strict'; var _this = this; if (!window.localStorage.hasOwnProperty(_this.id)) { _this.setStorage(_this.default); } _this.getFromStorage(); _this.oldVal = _this.val; }; SettingsField.prototype.getFromStorage = function () { 'use strict'; var _this = this; var val = window.localStorage.getItem(_this.id); switch (_this.type) { case 'checkbox': val = (val === 'true'); break; case 'int': val = Number(val); break; } _this.val = val; }; SettingsField.prototype.updateDisplay = function (fromGUI) { 'use strict'; var _this = this; if (fromGUI) { return; } switch (_this.type) { case 'checkbox': _this.$input.prop('checked', _this.get()); break; case 'int': case 'text': case 'select': _this.$input.val(_this.get()); break; } }; SettingsField.prototype.get = function () { 'use strict'; return this.val; }; SettingsField.prototype.setStorage = function (val) { 'use strict'; var _this = this; switch (_this.type) { case 'checkbox': val = !!val; break; } window.localStorage.setItem(_this.id, val); }; SettingsField.prototype.set = function (val, fromGUI) { 'use strict'; var _this = this; _this.oldVal = _this.val; _this.setStorage(val); _this.getFromStorage(); _this.updateDisplay(fromGUI); if (_this.oldVal !== _this.val) { _this.onChange(); } }; SettingsField.prototype.onChange = function () { 'use strict'; events.fire('SettingChange[{0}]'.format(this.id), [this.oldVal, this.val]); }; SettingsField.prototype.createTooltip = function () { 'use strict'; var _this = this; return $('