2018-11-07 06:09:29 +00:00
|
|
|
/* global promisify */
|
2017-04-11 10:51:40 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-02-12 14:39:00 +00:00
|
|
|
self.prefs = self.INJECTED === 1 ? self.prefs : (() => {
|
2017-04-11 10:51:40 +00:00
|
|
|
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
|
2019-01-10 04:43:28 +00:00
|
|
|
'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
|
|
|
|
2017-12-06 20:33:48 +00:00
|
|
|
// checkbox in style config dialog
|
|
|
|
'config.autosave': true,
|
|
|
|
|
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
|
2019-01-01 05:11:45 +00:00
|
|
|
'popup.autoResort': false, // auto resort styles after toggling
|
2017-11-14 08:13:03 +00:00
|
|
|
'popup.borders': false, // add white borders on the sides
|
2017-12-10 01:03:04 +00:00
|
|
|
'popup.findStylesInline': true, // use the inline style search
|
2017-08-21 18:58:24 +00:00
|
|
|
|
|
|
|
'manage.onlyEnabled': false, // display only enabled styles
|
|
|
|
'manage.onlyLocal': false, // display only styles created locally
|
2017-11-22 14:21:50 +00:00
|
|
|
'manage.onlyUsercss': false, // display only usercss styles
|
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-11-22 14:21:50 +00:00
|
|
|
'manage.onlyUsercss.invert': false, // display only non-usercss (standard) styles
|
2018-12-17 04:48:20 +00:00
|
|
|
|
2017-08-21 18:58:24 +00:00
|
|
|
'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
|
2017-12-23 00:11:46 +00:00
|
|
|
'manage.newUI.sort': 'title,asc',
|
2017-08-21 18:58:24 +00:00
|
|
|
|
|
|
|
'editor.options': {}, // CodeMirror.defaults.*
|
2017-09-02 16:32:12 +00:00
|
|
|
'editor.options.expanded': true, // UI element state: expanded/collapsed
|
2017-11-23 05:10:35 +00:00
|
|
|
'editor.lint.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
|
2019-09-11 11:56:19 +00:00
|
|
|
// CSS beautifier
|
|
|
|
'editor.beautify': {
|
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,
|
2018-07-22 21:28:11 +00:00
|
|
|
preserve_newlines: true,
|
2017-04-11 10:51:40 +00:00
|
|
|
end_with_newline: false,
|
2017-05-26 17:48:26 +00:00
|
|
|
indent_conditional: true,
|
2017-04-11 10:51:40 +00:00
|
|
|
},
|
2017-12-26 20:39:52 +00:00
|
|
|
'editor.lintDelay': 300, // lint gutter marker update delay, ms
|
2017-08-28 05:22:19 +00:00
|
|
|
'editor.linter': 'csslint', // 'csslint' or 'stylelint' or ''
|
2017-12-26 20:39:52 +00:00
|
|
|
'editor.lintReportDelay': 500, // lint report update delay, ms
|
2017-08-21 18:58:24 +00:00
|
|
|
'editor.matchHighlight': 'token', // token = token/word under cursor even if nothing is selected
|
|
|
|
// selection = only when something is selected
|
|
|
|
// '' (empty string) = disabled
|
2018-03-13 17:43:19 +00:00
|
|
|
'editor.autoCloseBrackets': true, // auto-add a closing pair when typing an opening one of ()[]{}''""
|
2017-08-21 18:58:24 +00:00
|
|
|
'editor.autocompleteOnTyping': false, // show autocomplete dropdown on typing a word token
|
|
|
|
'editor.contextDelete': contextDeleteMissing(), // "Delete" item in context menu
|
2018-04-18 16:33:36 +00:00
|
|
|
'editor.selectByTokens': true,
|
2017-08-21 18:58:24 +00:00
|
|
|
|
2017-09-14 01:58:22 +00:00
|
|
|
'editor.appliesToLineWidget': true, // show applies-to line widget on the editor
|
2018-01-10 18:56:14 +00:00
|
|
|
'editor.livePreview': true,
|
2017-09-14 01:58:22 +00:00
|
|
|
|
2017-11-15 12:59:24 +00:00
|
|
|
// show CSS colors as clickable colored rectangles
|
|
|
|
'editor.colorpicker': true,
|
|
|
|
// #DEAD or #beef
|
|
|
|
'editor.colorpicker.hexUppercase': false,
|
2017-11-21 15:39:13 +00:00
|
|
|
// default hotkey
|
|
|
|
'editor.colorpicker.hotkey': '',
|
|
|
|
// last color
|
|
|
|
'editor.colorpicker.color': '',
|
2017-11-15 12:59:24 +00:00
|
|
|
|
2018-04-12 18:02:34 +00:00
|
|
|
// Firefox-only chrome.commands.update
|
|
|
|
'hotkey._execute_browser_action': '',
|
|
|
|
'hotkey.openManage': '',
|
|
|
|
'hotkey.styleDisableAll': '',
|
|
|
|
|
2019-11-05 19:30:45 +00:00
|
|
|
'sync.enabled': 'none',
|
|
|
|
|
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);
|
|
|
|
|
2017-04-20 18:27:10 +00:00
|
|
|
const onChange = {
|
|
|
|
any: new Set(),
|
|
|
|
specific: new Map(),
|
|
|
|
};
|
|
|
|
|
2019-11-05 19:30:45 +00:00
|
|
|
const syncSet = promisify(chrome.storage.sync.set.bind(chrome.storage.sync));
|
|
|
|
const syncGet = promisify(chrome.storage.sync.get.bind(chrome.storage.sync));
|
|
|
|
|
|
|
|
const initializing = syncGet('settings')
|
2018-11-07 06:09:29 +00:00
|
|
|
.then(result => {
|
|
|
|
if (result.settings) {
|
|
|
|
setAll(result.settings, true);
|
|
|
|
}
|
|
|
|
});
|
2017-04-11 10:51:40 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
chrome.storage.onChanged.addListener((changes, area) => {
|
|
|
|
if (area !== 'sync' || !changes.settings || !changes.settings.newValue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
initializing.then(() => setAll(changes.settings.newValue, true));
|
2017-12-16 00:40:41 +00:00
|
|
|
});
|
2017-04-11 10:51:40 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
let timer;
|
|
|
|
|
|
|
|
// coalesce multiple pref changes in broadcast
|
|
|
|
// let changes = {};
|
2017-04-11 10:51:40 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
return {
|
|
|
|
initializing,
|
|
|
|
defaults,
|
2017-04-11 10:51:40 +00:00
|
|
|
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);
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
set,
|
|
|
|
reset: key => set(key, deepCopy(defaults[key])),
|
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) {
|
2017-11-16 08:45:48 +00:00
|
|
|
const existing = onChange.specific.get(key);
|
|
|
|
if (!existing) {
|
|
|
|
onChange.specific.set(key, listener);
|
|
|
|
} else if (existing instanceof Set) {
|
|
|
|
existing.add(listener);
|
|
|
|
} else {
|
|
|
|
onChange.specific.set(key, new Set([existing, listener]));
|
|
|
|
}
|
2017-04-20 18:27:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
onChange.any.add(listener);
|
|
|
|
}
|
|
|
|
},
|
2017-12-09 14:38:00 +00:00
|
|
|
unsubscribe(keys, listener) {
|
|
|
|
if (keys) {
|
|
|
|
for (const key of keys) {
|
|
|
|
const existing = onChange.specific.get(key);
|
|
|
|
if (existing instanceof Set) {
|
|
|
|
existing.delete(listener);
|
|
|
|
if (!existing.size) {
|
|
|
|
onChange.specific.delete(key);
|
|
|
|
}
|
|
|
|
} else if (existing) {
|
|
|
|
onChange.specific.delete(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
onChange.all.remove(listener);
|
|
|
|
}
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
};
|
2017-04-17 15:08:49 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function setAll(settings, synced) {
|
|
|
|
for (const [key, value] of Object.entries(settings)) {
|
|
|
|
set(key, value, synced);
|
|
|
|
}
|
2018-04-13 11:30:36 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function set(key, value, synced = false) {
|
|
|
|
const oldValue = values[key];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (equal(value, oldValue)) {
|
2017-09-03 18:25:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
values[key] = value;
|
|
|
|
emitChange(key, value);
|
2019-11-05 19:30:45 +00:00
|
|
|
if (!synced && !timer) {
|
|
|
|
timer = syncPrefsLater();
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2019-11-05 19:30:45 +00:00
|
|
|
return timer;
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function emitChange(key, value) {
|
|
|
|
const specific = onChange.specific.get(key);
|
|
|
|
if (typeof specific === 'function') {
|
|
|
|
specific(key, value);
|
|
|
|
} else if (specific instanceof Set) {
|
|
|
|
for (const listener of specific.values()) {
|
|
|
|
listener(key, value);
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
for (const listener of onChange.any.values()) {
|
|
|
|
listener(key, value);
|
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 19:30:45 +00:00
|
|
|
function syncPrefsLater() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
timer = null;
|
|
|
|
syncSet({settings: values})
|
|
|
|
.then(resolve, reject);
|
|
|
|
});
|
|
|
|
});
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2018-11-07 06:09:29 +00:00
|
|
|
return /Chrome\/\d+/.test(navigator.userAgent) && (
|
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-20 18:27:10 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function deepCopy(obj) {
|
|
|
|
if (!obj || typeof obj !== 'object') {
|
|
|
|
return obj;
|
2017-12-09 14:38:00 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
if (Array.isArray(obj)) {
|
|
|
|
return obj.map(deepCopy);
|
2017-04-18 07:54:06 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
return Object.keys(obj).reduce((output, key) => {
|
|
|
|
output[key] = deepCopy(obj[key]);
|
|
|
|
return output;
|
|
|
|
}, {});
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
})();
|