2017-08-28 10:29:56 +00:00
|
|
|
/* global prefs: true, contextMenus, FIREFOX_NO_DOM_STORAGE */
|
2017-04-11 10:51:40 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var prefs = new function Prefs() {
|
|
|
|
const defaults = {
|
2017-08-21 18:58:24 +00:00
|
|
|
'openEditInWindow': false, // new editor opens in a own browser window
|
|
|
|
'windowPosition': {}, // detached window position
|
|
|
|
'show-badge': true, // display text on popup menu icon
|
|
|
|
'disableAll': false, // boss key
|
|
|
|
'exposeIframes': false, // Add 'stylus-iframe' attribute to HTML element in all iframes
|
2017-11-09 00:21:42 +00:00
|
|
|
'newStyleAsUsercss': false, // create new style in usercss format
|
2017-08-21 18:58:24 +00:00
|
|
|
|
|
|
|
'popup.breadcrumbs': true, // display 'New style' links as URL breadcrumbs
|
|
|
|
'popup.breadcrumbs.usePath': false, // use URL path for 'this URL'
|
|
|
|
'popup.enabledFirst': true, // display enabled styles before disabled styles
|
|
|
|
'popup.stylesFirst': true, // display enabled styles before disabled styles
|
2017-11-14 08:13:03 +00:00
|
|
|
'popup.borders': false, // add white borders on the sides
|
2017-08-21 18:58:24 +00:00
|
|
|
|
|
|
|
'manage.onlyEnabled': false, // display only enabled styles
|
|
|
|
'manage.onlyLocal': false, // display only styles created locally
|
2017-08-21 18:07:41 +00:00
|
|
|
'manage.onlyEnabled.invert': false, // display only disabled styles
|
|
|
|
'manage.onlyLocal.invert': false, // display only externally installed styles
|
2017-08-21 18:58:24 +00:00
|
|
|
'manage.newUI': true, // use the new compact layout
|
|
|
|
'manage.newUI.favicons': false, // show favicons for the sites in applies-to
|
|
|
|
'manage.newUI.faviconsGray': true, // gray out favicons
|
|
|
|
'manage.newUI.targets': 3, // max number of applies-to targets visible: 0 = none
|
|
|
|
|
|
|
|
'editor.options': {}, // CodeMirror.defaults.*
|
2017-09-02 16:32:12 +00:00
|
|
|
'editor.options.expanded': true, // UI element state: expanded/collapsed
|
2017-08-21 18:58:24 +00:00
|
|
|
'editor.lineWrapping': true, // word wrap
|
|
|
|
'editor.smartIndent': true, // 'smart' indent
|
|
|
|
'editor.indentWithTabs': false, // smart indent with tabs
|
|
|
|
'editor.tabSize': 4, // tab width, in spaces
|
2017-04-11 10:51:40 +00:00
|
|
|
'editor.keyMap': navigator.appVersion.indexOf('Windows') > 0 ? 'sublime' : 'default',
|
2017-08-21 18:58:24 +00:00
|
|
|
'editor.theme': 'default', // CSS theme
|
|
|
|
'editor.beautify': { // CSS beautifier
|
2017-04-11 10:51:40 +00:00
|
|
|
selector_separator_newline: true,
|
|
|
|
newline_before_open_brace: false,
|
|
|
|
newline_after_open_brace: true,
|
|
|
|
newline_between_properties: true,
|
|
|
|
newline_before_close_brace: true,
|
|
|
|
newline_between_rules: false,
|
|
|
|
end_with_newline: false,
|
2017-05-26 17:48:26 +00:00
|
|
|
indent_conditional: true,
|
2017-04-11 10:51:40 +00:00
|
|
|
},
|
2017-08-21 18:58:24 +00:00
|
|
|
'editor.lintDelay': 500, // lint gutter marker update delay, ms
|
2017-08-28 05:22:19 +00:00
|
|
|
'editor.linter': 'csslint', // 'csslint' or 'stylelint' or ''
|
2017-08-21 18:58:24 +00:00
|
|
|
'editor.lintReportDelay': 4500, // lint report update delay, ms
|
|
|
|
'editor.matchHighlight': 'token', // token = token/word under cursor even if nothing is selected
|
|
|
|
// selection = only when something is selected
|
|
|
|
// '' (empty string) = disabled
|
|
|
|
'editor.autocompleteOnTyping': false, // show autocomplete dropdown on typing a word token
|
|
|
|
'editor.contextDelete': contextDeleteMissing(), // "Delete" item in context menu
|
|
|
|
|
2017-09-14 01:58:22 +00:00
|
|
|
'editor.appliesToLineWidget': true, // show applies-to line widget on the editor
|
|
|
|
|
2017-08-21 18:58:24 +00:00
|
|
|
'iconset': 0, // 0 = dark-themed icon
|
|
|
|
// 1 = light-themed icon
|
|
|
|
|
|
|
|
'badgeDisabled': '#8B0000', // badge background color when disabled
|
|
|
|
'badgeNormal': '#006666', // badge background color
|
|
|
|
|
|
|
|
'popupWidth': 246, // popup width in pixels
|
|
|
|
|
|
|
|
'updateInterval': 24, // user-style automatic update interval, hours (0 = disable)
|
2017-04-11 10:51:40 +00:00
|
|
|
};
|
|
|
|
const values = deepCopy(defaults);
|
|
|
|
|
|
|
|
const affectsIcon = [
|
|
|
|
'show-badge',
|
|
|
|
'disableAll',
|
|
|
|
'badgeDisabled',
|
|
|
|
'badgeNormal',
|
2017-06-28 10:49:04 +00:00
|
|
|
'iconset',
|
2017-04-11 10:51:40 +00:00
|
|
|
];
|
|
|
|
|
2017-04-20 18:27:10 +00:00
|
|
|
const onChange = {
|
|
|
|
any: new Set(),
|
|
|
|
specific: new Map(),
|
|
|
|
};
|
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
// coalesce multiple pref changes in broadcast
|
|
|
|
let broadcastPrefs = {};
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'readOnlyValues', {value: {}});
|
|
|
|
|
|
|
|
Object.assign(Prefs.prototype, {
|
|
|
|
|
|
|
|
get(key, defaultValue) {
|
|
|
|
if (key in values) {
|
|
|
|
return values[key];
|
|
|
|
}
|
|
|
|
if (defaultValue !== undefined) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
if (key in defaults) {
|
|
|
|
return defaults[key];
|
|
|
|
}
|
|
|
|
console.warn("No default preference for '%s'", key);
|
|
|
|
},
|
|
|
|
|
|
|
|
getAll() {
|
|
|
|
return deepCopy(values);
|
|
|
|
},
|
|
|
|
|
2017-04-21 09:33:24 +00:00
|
|
|
set(key, value, {broadcast = true, sync = true, fromBroadcast} = {}) {
|
2017-04-11 13:12:40 +00:00
|
|
|
const oldValue = values[key];
|
2017-04-11 13:12:18 +00:00
|
|
|
switch (typeof defaults[key]) {
|
|
|
|
case typeof value:
|
|
|
|
break;
|
|
|
|
case 'string':
|
|
|
|
value = String(value);
|
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
value |= 0;
|
|
|
|
break;
|
|
|
|
case 'boolean':
|
|
|
|
value = value === true || value === 'true';
|
|
|
|
break;
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
values[key] = value;
|
|
|
|
defineReadonlyProperty(this.readOnlyValues, key, value);
|
2017-04-20 18:27:10 +00:00
|
|
|
const hasChanged = !equal(value, oldValue);
|
2017-04-21 09:33:24 +00:00
|
|
|
if (!fromBroadcast) {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (BG && BG !== window) {
|
2017-04-21 09:33:24 +00:00
|
|
|
BG.prefs.set(key, BG.deepCopy(value), {broadcast, sync});
|
|
|
|
} else {
|
2017-07-16 18:02:00 +00:00
|
|
|
localStorage[key] = typeof defaults[key] === 'object'
|
2017-04-21 09:33:24 +00:00
|
|
|
? JSON.stringify(value)
|
|
|
|
: value;
|
|
|
|
if (broadcast && hasChanged) {
|
|
|
|
this.broadcast(key, value, {sync});
|
|
|
|
}
|
2017-04-11 11:22:00 +00:00
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2017-04-20 18:27:10 +00:00
|
|
|
if (hasChanged) {
|
|
|
|
const listener = onChange.specific.get(key);
|
|
|
|
if (listener) {
|
|
|
|
listener(key, value);
|
|
|
|
}
|
|
|
|
for (const listener of onChange.any.values()) {
|
|
|
|
listener(key, value);
|
|
|
|
}
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
remove: key => this.set(key, undefined),
|
|
|
|
|
|
|
|
reset: key => this.set(key, deepCopy(defaults[key])),
|
|
|
|
|
2017-04-21 09:33:24 +00:00
|
|
|
broadcast(key, value, {sync = true} = {}) {
|
2017-04-11 10:51:40 +00:00
|
|
|
broadcastPrefs[key] = value;
|
|
|
|
debounce(doBroadcast);
|
2017-04-21 09:33:24 +00:00
|
|
|
if (sync) {
|
2017-04-11 10:51:40 +00:00
|
|
|
debounce(doSyncSet);
|
|
|
|
}
|
|
|
|
},
|
2017-04-20 18:27:10 +00:00
|
|
|
|
2017-09-03 17:06:20 +00:00
|
|
|
subscribe(keys, listener) {
|
|
|
|
// keys: string[] ids
|
|
|
|
// or a falsy value to subscribe to everything
|
|
|
|
// listener: function (key, value)
|
2017-04-20 18:27:10 +00:00
|
|
|
if (keys) {
|
|
|
|
for (const key of keys) {
|
|
|
|
onChange.specific.set(key, listener);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
onChange.any.add(listener);
|
|
|
|
}
|
|
|
|
},
|
2017-04-11 10:51:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Unlike sync, HTML5 localStorage is ready at browser startup
|
|
|
|
// so we'll mirror the prefs to avoid using the wrong defaults
|
|
|
|
// during the startup phase
|
|
|
|
for (const key in defaults) {
|
|
|
|
const defaultValue = defaults[key];
|
|
|
|
let value = localStorage[key];
|
2017-07-16 18:02:00 +00:00
|
|
|
if (typeof value === 'string') {
|
2017-04-11 10:51:40 +00:00
|
|
|
switch (typeof defaultValue) {
|
|
|
|
case 'boolean':
|
|
|
|
value = value.toLowerCase() === 'true';
|
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
value |= 0;
|
|
|
|
break;
|
|
|
|
case 'object':
|
|
|
|
value = tryJSONparse(value) || defaultValue;
|
|
|
|
break;
|
|
|
|
}
|
2017-08-28 10:29:56 +00:00
|
|
|
} else if (FIREFOX_NO_DOM_STORAGE && BG) {
|
2017-09-03 16:55:32 +00:00
|
|
|
value = BG.localStorage[key];
|
|
|
|
value = value === undefined ? defaultValue : value;
|
2017-04-11 10:51:40 +00:00
|
|
|
} else {
|
|
|
|
value = defaultValue;
|
|
|
|
}
|
2017-07-16 18:02:00 +00:00
|
|
|
if (BG === window) {
|
2017-04-17 15:08:49 +00:00
|
|
|
// when in bg page, .set() will write to localStorage
|
2017-04-21 09:33:24 +00:00
|
|
|
this.set(key, value, {broadcast: false, sync: false});
|
2017-04-17 15:08:49 +00:00
|
|
|
} else {
|
|
|
|
values[key] = value;
|
|
|
|
defineReadonlyProperty(this.readOnlyValues, key, value);
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-16 18:02:00 +00:00
|
|
|
if (!BG || BG === window) {
|
2017-04-21 10:02:01 +00:00
|
|
|
affectsIcon.forEach(key => this.broadcast(key, values[key], {sync: false}));
|
|
|
|
|
2017-08-16 18:44:59 +00:00
|
|
|
const importFromSync = (synced = {}) => {
|
2017-08-13 14:33:57 +00:00
|
|
|
for (const key in defaults) {
|
|
|
|
if (key in synced) {
|
|
|
|
this.set(key, synced[key], {sync: false});
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-13 14:33:57 +00:00
|
|
|
};
|
|
|
|
|
2017-08-24 13:30:13 +00:00
|
|
|
getSync().get('settings', ({settings} = {}) => importFromSync(settings));
|
2017-04-17 15:08:49 +00:00
|
|
|
|
|
|
|
chrome.storage.onChanged.addListener((changes, area) => {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (area === 'sync' && 'settings' in changes) {
|
2017-04-17 15:08:49 +00:00
|
|
|
const synced = changes.settings.newValue;
|
|
|
|
if (synced) {
|
2017-08-13 14:33:57 +00:00
|
|
|
importFromSync(synced);
|
2017-04-17 15:08:49 +00:00
|
|
|
} else {
|
|
|
|
// user manually deleted our settings, we'll recreate them
|
|
|
|
getSync().set({'settings': values});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-04-21 09:33:24 +00:00
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
|
2017-04-21 09:33:24 +00:00
|
|
|
// any access to chrome API takes time due to initialization of bindings
|
|
|
|
window.addEventListener('load', function _() {
|
|
|
|
window.removeEventListener('load', _);
|
2017-04-17 15:08:49 +00:00
|
|
|
chrome.runtime.onMessage.addListener(msg => {
|
|
|
|
if (msg.prefs) {
|
|
|
|
for (const id in msg.prefs) {
|
2017-04-21 09:33:24 +00:00
|
|
|
prefs.set(id, msg.prefs[id], {fromBroadcast: true});
|
2017-04-17 15:08:49 +00:00
|
|
|
}
|
2017-04-11 11:22:00 +00:00
|
|
|
}
|
2017-04-17 15:08:49 +00:00
|
|
|
});
|
2017-04-21 09:33:24 +00:00
|
|
|
});
|
2017-04-17 15:08:49 +00:00
|
|
|
|
|
|
|
return;
|
2017-04-11 11:22:00 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function doBroadcast() {
|
2017-09-03 18:25:19 +00:00
|
|
|
if (BG && BG === window && !BG.dbExec.initialized) {
|
|
|
|
window.addEventListener('storageReady', function _() {
|
|
|
|
window.removeEventListener('storageReady', _);
|
|
|
|
doBroadcast();
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2017-04-28 23:36:10 +00:00
|
|
|
const affects = {
|
|
|
|
all: 'disableAll' in broadcastPrefs
|
|
|
|
|| 'exposeIframes' in broadcastPrefs,
|
|
|
|
};
|
2017-04-11 10:51:40 +00:00
|
|
|
if (!affects.all) {
|
|
|
|
for (const key in broadcastPrefs) {
|
|
|
|
affects.icon = affects.icon || affectsIcon.includes(key);
|
|
|
|
affects.popup = affects.popup || key.startsWith('popup');
|
|
|
|
affects.editor = affects.editor || key.startsWith('editor');
|
|
|
|
affects.manager = affects.manager || key.startsWith('manage');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notifyAllTabs({method: 'prefChanged', prefs: broadcastPrefs, affects});
|
|
|
|
broadcastPrefs = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
function doSyncSet() {
|
|
|
|
getSync().set({'settings': values});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Polyfill for Firefox < 53 https://bugzilla.mozilla.org/show_bug.cgi?id=1220494
|
|
|
|
function getSync() {
|
|
|
|
if ('sync' in chrome.storage) {
|
|
|
|
return chrome.storage.sync;
|
|
|
|
}
|
|
|
|
const crappyStorage = {};
|
|
|
|
return {
|
|
|
|
get(key, callback) {
|
|
|
|
callback(crappyStorage[key] || {});
|
|
|
|
},
|
|
|
|
set(source, callback) {
|
|
|
|
for (const property in source) {
|
|
|
|
if (source.hasOwnProperty(property)) {
|
|
|
|
crappyStorage[property] = source[property];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function defineReadonlyProperty(obj, key, value) {
|
|
|
|
const copy = deepCopy(value);
|
2017-07-16 18:02:00 +00:00
|
|
|
if (typeof copy === 'object') {
|
2017-04-11 10:51:40 +00:00
|
|
|
Object.freeze(copy);
|
|
|
|
}
|
|
|
|
Object.defineProperty(obj, key, {value: copy, configurable: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
function equal(a, b) {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (!a || !b || typeof a !== 'object' || typeof b !== 'object') {
|
2017-04-11 10:51:40 +00:00
|
|
|
return a === b;
|
|
|
|
}
|
2017-07-16 18:02:00 +00:00
|
|
|
if (Object.keys(a).length !== Object.keys(b).length) {
|
2017-04-11 10:51:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const k in a) {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (typeof a[k] === 'object') {
|
2017-04-11 13:12:40 +00:00
|
|
|
if (!equal(a[k], b[k])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (a[k] !== b[k]) {
|
2017-04-11 10:51:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-29 16:54:16 +00:00
|
|
|
|
|
|
|
function contextDeleteMissing() {
|
2017-11-14 07:52:54 +00:00
|
|
|
return CHROME && (
|
2017-04-29 16:54:16 +00:00
|
|
|
// detect browsers without Delete by looking at the end of UA string
|
|
|
|
/Vivaldi\/[\d.]+$/.test(navigator.userAgent) ||
|
|
|
|
// Chrome and co.
|
|
|
|
/Safari\/[\d.]+$/.test(navigator.userAgent) &&
|
|
|
|
// skip forks with Flash as those are likely to have the menu e.g. CentBrowser
|
2017-07-16 18:02:00 +00:00
|
|
|
!Array.from(navigator.plugins).some(p => p.name === 'Shockwave Flash')
|
2017-04-29 16:54:16 +00:00
|
|
|
);
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
|
|
// Accepts an array of pref names (values are fetched via prefs.get)
|
|
|
|
// and establishes a two-way connection between the document elements and the actual prefs
|
2017-04-21 16:39:34 +00:00
|
|
|
function setupLivePrefs(
|
|
|
|
IDs = Object.getOwnPropertyNames(prefs.readOnlyValues)
|
2017-08-21 00:05:08 +00:00
|
|
|
.filter(id => $('#' + id))
|
2017-04-21 16:39:34 +00:00
|
|
|
) {
|
2017-04-18 07:54:06 +00:00
|
|
|
const checkedProps = {};
|
|
|
|
for (const id of IDs) {
|
2017-08-21 00:05:08 +00:00
|
|
|
const element = $('#' + id);
|
2017-07-16 18:02:00 +00:00
|
|
|
checkedProps[id] = element.type === 'checkbox' ? 'checked' : 'value';
|
2017-04-18 07:54:06 +00:00
|
|
|
updateElement({id, element, force: true});
|
|
|
|
element.addEventListener('change', onChange);
|
|
|
|
}
|
2017-09-03 17:06:20 +00:00
|
|
|
prefs.subscribe(IDs, (id, value) => updateElement({id, value}));
|
2017-04-20 18:27:10 +00:00
|
|
|
|
2017-04-18 07:54:06 +00:00
|
|
|
function onChange() {
|
|
|
|
const value = this[checkedProps[this.id]];
|
2017-07-16 18:02:00 +00:00
|
|
|
if (prefs.get(this.id) !== value) {
|
2017-04-18 07:54:06 +00:00
|
|
|
prefs.set(this.id, value);
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2017-04-18 07:54:06 +00:00
|
|
|
function updateElement({
|
|
|
|
id,
|
|
|
|
value = prefs.get(id),
|
2017-08-21 00:05:08 +00:00
|
|
|
element = $('#' + id),
|
2017-04-18 07:54:06 +00:00
|
|
|
force,
|
|
|
|
}) {
|
|
|
|
const prop = checkedProps[id];
|
2017-07-16 18:02:00 +00:00
|
|
|
if (force || element[prop] !== value) {
|
2017-04-18 07:54:06 +00:00
|
|
|
element[prop] = value;
|
|
|
|
element.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
}
|