stylus/js/polyfill.js
tophf 420733b93a
PatchCSP + tweaks/fixes/features (#1107)
* add Patch CSP option
* show style version, size, and update age in manager
* add scope selector to style search in manager
* keep scroll position and selections in tab's session
* directly install usercss from raw github links
* ditch localStorage, use on-demand SessionStore proxy
* simplify localization
* allow <code> tag in i18n-html
* keep &nbsp; nodes in HTML templates
* API.getAllStyles is actually faster with code untouched
* fix fitToContent when applies-to is taller than window
* dedupe linter.enableForEditor calls
* prioritize visible CMs in refreshOnViewListener
* don't scroll to last style on editing a new one
* delay colorview for invisible CMs
* eslint comma-dangle error + autofix files
* styleViaXhr: also toggle for disableAll pref
* styleViaXhr: allow cookies for sandbox CSP
* simplify notes in options
* simplify getStylesViaXhr
* oldUI fixups:
  * remove separator before 1st applies-to
  * center name bubbles
* fix updateToc focus on a newly added section
* fix fitToContent when cloning section
* remove CSS `contain` as it makes no difference
* replace overrides with declarative CSS + code cosmetics
* simplify adjustWidth and make it work in FF
2020-11-18 14:17:15 +03:00

87 lines
2.8 KiB
JavaScript

'use strict';
// eslint-disable-next-line no-unused-expressions
self.INJECTED !== 1 && (() => {
//#region for content scripts and our extension pages
if (!((window.browser || {}).runtime || {}).sendMessage) {
/* Auto-promisifier with a fallback to direct call on signature error.
The fallback isn't used now since we call all synchronous methods via `chrome` */
const directEvents = ['addListener', 'removeListener', 'hasListener', 'hasListeners'];
// generated by tools/chrome-api-no-cb.js
const directMethods = {
alarms: ['create'],
extension: ['getBackgroundPage', 'getExtensionTabs', 'getURL', 'getViews', 'setUpdateUrlData'],
i18n: ['getMessage', 'getUILanguage'],
identity: ['getRedirectURL'],
runtime: ['connect', 'connectNative', 'getManifest', 'getURL', 'reload', 'restart'],
tabs: ['connect'],
};
const promisify = function (fn, ...args) {
let res;
try {
let resolve, reject;
/* Some callbacks have 2 parameters so we're resolving as an array in that case.
For example, chrome.runtime.requestUpdateCheck and chrome.webRequest.onAuthRequired */
args.push((...results) =>
chrome.runtime.lastError ?
reject(new Error(chrome.runtime.lastError.message)) :
resolve(results.length <= 1 ? results[0] : results));
fn.apply(this, args);
res = new Promise((...rr) => ([resolve, reject] = rr));
} catch (err) {
if (!err.message.includes('No matching signature')) {
throw err;
}
args.pop();
res = fn.apply(this, args);
}
return res;
};
const proxify = (src, srcName, target, key) => {
let res = src[key];
if (res && typeof res === 'object') {
res = createProxy(res, key); // eslint-disable-line no-use-before-define
} else if (typeof res === 'function') {
res = (directMethods[srcName] || directEvents).includes(key)
? res.bind(src)
: promisify.bind(src, res);
}
target[key] = res;
return res;
};
const createProxy = (src, srcName) =>
new Proxy({}, {
get(target, key) {
return target[key] || proxify(src, srcName, target, key);
},
});
window.browser = createProxy(chrome);
}
//#endregion
if (!chrome.tabs) return;
//#region for our extension pages
if (!(new URLSearchParams({foo: 1})).get('foo')) {
// TODO: remove when minimum_chrome_version >= 61
window.URLSearchParams = class extends URLSearchParams {
constructor(init) {
if (init && typeof init === 'object') {
super();
for (const [key, val] of Object.entries(init)) {
this.set(key, val);
}
} else {
super(...arguments);
}
}
};
}
//#endregion
})();