2021-01-01 14:27:58 +00:00
|
|
|
/* global API msg */// msg.js
|
|
|
|
/* global debounce deepMerge */// toolbox.js - not used in content scripts
|
2017-04-11 10:51:40 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
(() => {
|
|
|
|
if (window.INJECTED === 1) return;
|
|
|
|
|
2020-10-28 06:53:02 +00:00
|
|
|
const STORAGE_KEY = 'settings';
|
2021-01-01 14:27:58 +00:00
|
|
|
const clone = typeof deepMerge === 'function'
|
|
|
|
? deepMerge
|
|
|
|
: val =>
|
|
|
|
typeof val === 'object' && val
|
|
|
|
? JSON.parse(JSON.stringify(val))
|
|
|
|
: val;
|
|
|
|
/**
|
|
|
|
* @type PrefsValues
|
|
|
|
* @namespace PrefsValues
|
|
|
|
*/
|
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
|
2020-10-18 13:37:42 +00:00
|
|
|
'openEditInWindow.popup': false, // new editor opens in a simplified browser window without omnibox
|
2017-08-21 18:58:24 +00:00
|
|
|
'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
|
2020-10-22 19:16:55 +00:00
|
|
|
'styleViaXhr': false, // early style injection to avoid FOUC
|
2020-11-18 11:17:15 +00:00
|
|
|
'patchCsp': false, // add data: and popular image hosting sites to strict CSP
|
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
|
2017-11-29 16:05:47 +00:00
|
|
|
// UI element state: expanded/collapsed
|
2020-10-02 15:10:52 +00:00
|
|
|
'manage.actions.expanded': true,
|
2017-12-05 21:05:23 +00:00
|
|
|
'manage.backup.expanded': true,
|
|
|
|
'manage.filters.expanded': true,
|
2017-11-25 13:24:07 +00:00
|
|
|
// the new compact layout doesn't look good on Android yet
|
|
|
|
'manage.newUI': !navigator.appVersion.includes('Android'),
|
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.*
|
2020-11-08 08:12:42 +00:00
|
|
|
'editor.toc.expanded': true, // UI element state: expanded/collapsed
|
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
|
|
|
},
|
2020-06-22 16:14:41 +00:00
|
|
|
'editor.beautify.hotkey': '',
|
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
|
2020-10-28 06:53:02 +00:00
|
|
|
// "Delete" item in context menu for browsers that don't have it
|
|
|
|
'editor.contextDelete': null,
|
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': '',
|
2020-10-26 15:03:41 +00:00
|
|
|
'editor.colorpicker.maxHeight': 300,
|
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
|
|
|
|
|
2020-11-20 14:00:20 +00:00
|
|
|
'ui.sliders': true, // use sliders instead of checkboxes for style entries
|
|
|
|
|
2017-08-21 18:58:24 +00:00
|
|
|
'updateInterval': 24, // user-style automatic update interval, hours (0 = disable)
|
2017-04-11 10:51:40 +00:00
|
|
|
};
|
2021-01-01 14:27:58 +00:00
|
|
|
const knownKeys = Object.keys(defaults);
|
|
|
|
/** @type {PrefsValues} */
|
2020-10-28 06:53:02 +00:00
|
|
|
const values = clone(defaults);
|
2017-04-20 18:27:10 +00:00
|
|
|
const onChange = {
|
|
|
|
any: new Set(),
|
2020-10-28 06:53:02 +00:00
|
|
|
specific: {},
|
2017-04-20 18:27:10 +00:00
|
|
|
};
|
2021-01-01 14:27:58 +00:00
|
|
|
// API fails in the active tab during Chrome startup as it loads the tab before bg
|
|
|
|
/** @type {Promise|boolean} will be `true` to avoid wasting a microtask tick on each `await` */
|
|
|
|
let ready = (msg.isBg ? readStorage() : API.prefs.getValues().catch(readStorage))
|
|
|
|
.then(data => {
|
|
|
|
setAll(data);
|
|
|
|
ready = true;
|
|
|
|
});
|
2017-04-11 10:51:40 +00:00
|
|
|
|
2020-10-28 06:53:02 +00:00
|
|
|
chrome.storage.onChanged.addListener(async (changes, area) => {
|
|
|
|
const data = area === 'sync' && changes[STORAGE_KEY];
|
|
|
|
if (data) {
|
2021-01-01 14:27:58 +00:00
|
|
|
if (ready.then) await ready;
|
2020-10-28 06:53:02 +00:00
|
|
|
setAll(data.newValue);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2017-12-16 00:40:41 +00:00
|
|
|
});
|
2017-04-11 10:51:40 +00:00
|
|
|
|
2020-10-28 06:53:02 +00:00
|
|
|
const prefs = window.prefs = {
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2020-11-08 10:31:07 +00:00
|
|
|
STORAGE_KEY,
|
2021-01-01 14:27:58 +00:00
|
|
|
knownKeys,
|
|
|
|
ready,
|
|
|
|
/** @type {PrefsValues} */
|
|
|
|
defaults: new Proxy({}, {
|
|
|
|
get: (_, key) => clone(defaults[key]),
|
|
|
|
}),
|
|
|
|
/** @type {PrefsValues} */
|
2020-11-08 10:29:15 +00:00
|
|
|
get values() {
|
2021-01-01 14:27:58 +00:00
|
|
|
return clone(values);
|
2020-11-08 10:29:15 +00:00
|
|
|
},
|
2021-01-01 14:27:58 +00:00
|
|
|
|
|
|
|
__defaults: defaults, // direct reference, be careful!
|
|
|
|
__values: values, // direct reference, be careful!
|
|
|
|
|
2020-10-28 06:53:02 +00:00
|
|
|
get(key) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const res = values[key];
|
|
|
|
if (res !== undefined || isKnown(key)) {
|
|
|
|
return clone(res);
|
|
|
|
}
|
2020-10-28 06:53:02 +00:00
|
|
|
},
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2020-11-04 09:50:24 +00:00
|
|
|
set(key, val, isSynced) {
|
2020-10-28 06:53:02 +00:00
|
|
|
if (!isKnown(key)) return;
|
|
|
|
const oldValue = values[key];
|
|
|
|
const type = typeof defaults[key];
|
2020-11-04 09:50:24 +00:00
|
|
|
if (type !== typeof val) {
|
|
|
|
if (type === 'string') val = String(val);
|
|
|
|
if (type === 'number') val = Number(val) || 0;
|
|
|
|
if (type === 'boolean') val = val === 'true' || val !== 'false' && Boolean(val);
|
|
|
|
// 'true' and 'false' strings are used by manage.html in `.invert` elements
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2020-11-04 09:50:24 +00:00
|
|
|
if (val !== oldValue && !simpleDeepEqual(val, oldValue)) {
|
|
|
|
values[key] = val;
|
|
|
|
emitChange(key, val, isSynced);
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
},
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2020-10-28 06:53:02 +00:00
|
|
|
reset(key) {
|
|
|
|
prefs.set(key, clone(defaults[key]));
|
2017-04-11 10:51:40 +00:00
|
|
|
},
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2020-10-28 06:53:02 +00:00
|
|
|
/**
|
|
|
|
* @param {?string|string[]} keys - pref ids or a falsy value to subscribe to everything
|
2021-01-01 14:27:58 +00:00
|
|
|
* @param {function(key:string?, value:any?)} fn
|
2020-10-28 06:53:02 +00:00
|
|
|
* @param {Object} [opts]
|
2021-01-01 14:27:58 +00:00
|
|
|
* @param {boolean} [opts.runNow] - when truthy, the listener is called immediately:
|
2020-10-28 06:53:02 +00:00
|
|
|
* 1) if `keys` is an array of keys, each `key` will be fired separately with a real `value`
|
|
|
|
* 2) if `keys` is falsy, no key/value will be provided
|
|
|
|
*/
|
2021-01-01 14:27:58 +00:00
|
|
|
async subscribe(keys, fn, {runNow} = {}) {
|
|
|
|
const toRun = [];
|
2017-04-20 18:27:10 +00:00
|
|
|
if (keys) {
|
2020-10-28 06:53:02 +00:00
|
|
|
for (const key of Array.isArray(keys) ? keys : [keys]) {
|
|
|
|
if (!isKnown(key)) continue;
|
|
|
|
const listeners = onChange.specific[key] ||
|
|
|
|
(onChange.specific[key] = new Set());
|
|
|
|
listeners.add(fn);
|
2021-01-01 14:27:58 +00:00
|
|
|
if (runNow) toRun.push({fn, key});
|
2017-04-20 18:27:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-10-28 06:53:02 +00:00
|
|
|
onChange.any.add(fn);
|
2021-01-01 14:27:58 +00:00
|
|
|
if (runNow) toRun.push({fn});
|
|
|
|
}
|
|
|
|
if (toRun.length) {
|
|
|
|
if (ready.then) await ready;
|
|
|
|
toRun.forEach(({fn, key}) => fn(key, values[key]));
|
2017-04-20 18:27:10 +00:00
|
|
|
}
|
|
|
|
},
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
subscribeMany(data, opts) {
|
|
|
|
for (const [k, fn] of Object.entries(data)) {
|
|
|
|
prefs.subscribe(k, fn, opts);
|
|
|
|
}
|
|
|
|
},
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2020-10-28 06:53:02 +00:00
|
|
|
unsubscribe(keys, fn) {
|
2017-12-09 14:38:00 +00:00
|
|
|
if (keys) {
|
|
|
|
for (const key of keys) {
|
2020-10-28 06:53:02 +00:00
|
|
|
const listeners = onChange.specific[key];
|
|
|
|
if (listeners) {
|
|
|
|
listeners.delete(fn);
|
|
|
|
if (!listeners.size) {
|
|
|
|
delete onChange.specific[key];
|
2017-12-09 14:38:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-10-28 06:53:02 +00:00
|
|
|
onChange.all.remove(fn);
|
2017-12-09 14:38:00 +00:00
|
|
|
}
|
|
|
|
},
|
2018-11-07 06:09:29 +00:00
|
|
|
};
|
2017-04-17 15:08:49 +00:00
|
|
|
|
2020-10-28 06:53:02 +00:00
|
|
|
function isKnown(key) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const res = knownKeys.includes(key);
|
2020-10-28 06:53:02 +00:00
|
|
|
if (!res) console.warn('Unknown preference "%s"', key);
|
|
|
|
return res;
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 06:53:02 +00:00
|
|
|
function setAll(settings) {
|
|
|
|
for (const [key, value] of Object.entries(settings || {})) {
|
|
|
|
prefs.set(key, value, true);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 06:53:02 +00:00
|
|
|
function emitChange(key, value, isSynced) {
|
|
|
|
for (const fn of onChange.specific[key] || []) {
|
|
|
|
fn(key, value);
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2020-10-28 06:53:02 +00:00
|
|
|
for (const fn of onChange.any) {
|
|
|
|
fn(key, value);
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2020-10-28 06:53:02 +00:00
|
|
|
if (!isSynced) {
|
|
|
|
/* browser.storage is slow and can randomly lose values if the tab was closed immediately
|
|
|
|
so we're sending the value to the background script which will save it to the storage;
|
|
|
|
the extra bonus is that invokeAPI is immediate in extension tabs */
|
|
|
|
if (msg.isBg) {
|
|
|
|
debounce(updateStorage);
|
|
|
|
} else {
|
2021-01-01 14:27:58 +00:00
|
|
|
API.prefs.set(key, value);
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-29 16:54:16 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function readStorage() {
|
|
|
|
return (await browser.storage.sync.get(STORAGE_KEY))[STORAGE_KEY];
|
2020-11-11 10:26:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 06:53:02 +00:00
|
|
|
function updateStorage() {
|
|
|
|
return browser.storage.sync.set({[STORAGE_KEY]: values});
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2020-10-31 17:59:29 +00:00
|
|
|
|
|
|
|
function simpleDeepEqual(a, b) {
|
|
|
|
return !a || !b || typeof a !== 'object' || typeof b !== 'object' ? a === b :
|
|
|
|
Object.keys(a).length === Object.keys(b).length &&
|
|
|
|
Object.keys(a).every(key => b.hasOwnProperty(key) && simpleDeepEqual(a[key], b[key]));
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
})();
|