2017-03-25 05:54:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-04-21 17:35:22 +00:00
|
|
|
if (!navigator.userAgent.includes('Windows')) {
|
2017-03-25 20:39:21 +00:00
|
|
|
document.documentElement.classList.add('non-windows');
|
|
|
|
}
|
|
|
|
|
2017-04-08 10:19:44 +00:00
|
|
|
// polyfill for old browsers to enable [...results] and for-of
|
|
|
|
for (const type of [NodeList, NamedNodeMap, HTMLCollection, HTMLAllCollection]) {
|
|
|
|
if (!type.prototype[Symbol.iterator]) {
|
|
|
|
type.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 07:15:51 +00:00
|
|
|
{
|
|
|
|
// display a full text tooltip on buttons with ellipsis overflow and no inherent title
|
|
|
|
const addTooltipsToEllipsized = () => {
|
2017-08-23 12:37:35 +00:00
|
|
|
for (const btn of document.getElementsByTagName('button')) {
|
2017-08-18 07:15:51 +00:00
|
|
|
if (btn.title && !btn.titleIsForEllipsis ||
|
|
|
|
btn.clientWidth === btn.preresizeClientWidth) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
btn.preresizeClientWidth = btn.clientWidth;
|
|
|
|
const padding = btn.offsetWidth - btn.clientWidth;
|
|
|
|
const displayedWidth = btn.getBoundingClientRect().width - padding;
|
|
|
|
if (btn.scrollWidth > displayedWidth) {
|
|
|
|
btn.title = btn.textContent;
|
|
|
|
btn.titleIsForEllipsis = true;
|
|
|
|
} else if (btn.title) {
|
|
|
|
btn.title = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// enqueue after DOMContentLoaded/load events
|
|
|
|
setTimeout(addTooltipsToEllipsized);
|
|
|
|
// throttle on continuous resizing
|
|
|
|
window.addEventListener('resize', () => debounce(addTooltipsToEllipsized, 100));
|
|
|
|
}
|
|
|
|
|
2017-09-03 20:52:06 +00:00
|
|
|
onDOMready().then(() => $('#firefox-transitions-bug-suppressor').remove());
|
|
|
|
|
2017-09-02 15:36:32 +00:00
|
|
|
if (navigator.userAgent.includes('Firefox')) {
|
2017-09-03 20:52:06 +00:00
|
|
|
// die if unable to access BG directly
|
2017-09-02 15:36:32 +00:00
|
|
|
chrome.windows.getCurrent(wnd => {
|
|
|
|
if (!BG && wnd.incognito) {
|
|
|
|
// private windows can't get bg page
|
|
|
|
location.href = '/msgbox/dysfunctional.html';
|
|
|
|
throw 0;
|
|
|
|
}
|
|
|
|
});
|
2017-09-03 20:52:06 +00:00
|
|
|
// add favicon in Firefox
|
2017-09-02 15:36:32 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
if (!window.prefs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const iconset = ['', 'light/'][prefs.get('iconset')] || '';
|
|
|
|
for (const size of [38, 32, 19, 16]) {
|
|
|
|
document.head.appendChild($element({
|
|
|
|
tag: 'link',
|
|
|
|
rel: 'icon',
|
|
|
|
href: `/images/icon/${iconset}${size}.png`,
|
|
|
|
sizes: size + 'x' + size,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
2017-08-27 09:56:54 +00:00
|
|
|
// set hyphenation language
|
|
|
|
document.documentElement.setAttribute('lang', chrome.i18n.getUILanguage());
|
2017-09-02 15:36:32 +00:00
|
|
|
}
|
2017-05-16 22:09:31 +00:00
|
|
|
|
2017-03-25 20:39:21 +00:00
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
function onDOMready() {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (document.readyState !== 'loading') {
|
2017-03-25 05:54:58 +00:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return new Promise(resolve => {
|
|
|
|
document.addEventListener('DOMContentLoaded', function _() {
|
|
|
|
document.removeEventListener('DOMContentLoaded', _);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-18 15:58:59 +00:00
|
|
|
function onDOMscripted(scripts) {
|
2017-08-29 14:12:46 +00:00
|
|
|
const queue = onDOMscripted.queue = onDOMscripted.queue || [];
|
2017-08-18 15:58:59 +00:00
|
|
|
if (scripts) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
addResolver(resolve);
|
2017-08-29 14:12:46 +00:00
|
|
|
queue.push(...scripts.filter(el => !queue.includes(el)));
|
2017-08-18 15:58:59 +00:00
|
|
|
loadNextScript();
|
|
|
|
});
|
|
|
|
}
|
2017-08-29 14:12:46 +00:00
|
|
|
if (queue.length) {
|
2017-08-18 15:58:59 +00:00
|
|
|
return new Promise(resolve => addResolver(resolve));
|
|
|
|
}
|
2017-08-18 16:13:03 +00:00
|
|
|
if (document.readyState !== 'loading') {
|
2017-08-18 15:58:59 +00:00
|
|
|
if (onDOMscripted.resolveOnReady) {
|
|
|
|
onDOMscripted.resolveOnReady.forEach(r => r());
|
|
|
|
onDOMscripted.resolveOnReady = null;
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return onDOMready().then(onDOMscripted);
|
|
|
|
|
|
|
|
function loadNextScript() {
|
2017-08-29 14:12:46 +00:00
|
|
|
const empty = !queue.length;
|
|
|
|
const next = !empty && queue.shift();
|
|
|
|
if (empty) {
|
2017-08-18 15:58:59 +00:00
|
|
|
onDOMscripted();
|
2017-08-18 16:13:03 +00:00
|
|
|
} else if (typeof next === 'function') {
|
2017-08-18 15:58:59 +00:00
|
|
|
Promise.resolve(next())
|
|
|
|
.then(loadNextScript);
|
2017-08-18 16:13:03 +00:00
|
|
|
} else {
|
2017-08-18 15:58:59 +00:00
|
|
|
Promise.all(
|
|
|
|
(next instanceof Array ? next : [next]).map(next =>
|
2017-08-18 16:13:03 +00:00
|
|
|
typeof next === 'function'
|
2017-08-18 15:58:59 +00:00
|
|
|
? next()
|
|
|
|
: injectScript({src: next, async: true})
|
|
|
|
)
|
|
|
|
).then(loadNextScript);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addResolver(r) {
|
|
|
|
if (!onDOMscripted.resolveOnReady) {
|
|
|
|
onDOMscripted.resolveOnReady = [];
|
|
|
|
}
|
|
|
|
onDOMscripted.resolveOnReady.push(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function injectScript(properties) {
|
2017-08-18 16:13:03 +00:00
|
|
|
if (typeof properties === 'string') {
|
2017-08-18 15:58:59 +00:00
|
|
|
properties = {src: properties};
|
|
|
|
}
|
|
|
|
if (!properties || !properties.src) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-29 11:28:13 +00:00
|
|
|
if (injectScript.cache) {
|
|
|
|
if (injectScript.cache.has(properties.src)) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
injectScript.cache = new Set();
|
|
|
|
}
|
|
|
|
injectScript.cache.add(properties.src);
|
2017-08-18 15:58:59 +00:00
|
|
|
const script = document.head.appendChild(document.createElement('script'));
|
|
|
|
Object.assign(script, properties);
|
|
|
|
if (!properties.onload) {
|
|
|
|
return new Promise(resolve => {
|
2017-08-20 19:10:58 +00:00
|
|
|
script.onload = () => {
|
|
|
|
script.onload = null;
|
|
|
|
resolve();
|
|
|
|
};
|
2017-08-18 15:58:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function injectCSS(url) {
|
|
|
|
if (!url) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
document.head.appendChild($element({
|
|
|
|
tag: 'link',
|
|
|
|
rel: 'stylesheet',
|
|
|
|
href: url
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
function scrollElementIntoView(element) {
|
|
|
|
// align to the top/bottom of the visible area if wasn't visible
|
|
|
|
const bounds = element.getBoundingClientRect();
|
|
|
|
if (bounds.top < 0 || bounds.top > innerHeight - bounds.height) {
|
|
|
|
element.scrollIntoView(bounds.top < 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-27 20:00:24 +00:00
|
|
|
function animateElement(
|
|
|
|
element, {
|
|
|
|
className = 'highlight',
|
|
|
|
removeExtraClasses = [],
|
2017-08-31 10:23:12 +00:00
|
|
|
onComplete,
|
2017-06-27 20:00:24 +00:00
|
|
|
} = {}) {
|
|
|
|
return element && new Promise(resolve => {
|
2017-03-25 05:54:58 +00:00
|
|
|
element.addEventListener('animationend', function _() {
|
|
|
|
element.removeEventListener('animationend', _);
|
2017-05-14 11:26:51 +00:00
|
|
|
element.classList.remove(
|
|
|
|
className,
|
2017-06-30 12:02:09 +00:00
|
|
|
// In Firefox, `resolve()` might be called one frame later.
|
|
|
|
// This is helpful to clean-up on the same frame
|
|
|
|
...removeExtraClasses
|
2017-05-14 11:26:51 +00:00
|
|
|
);
|
2017-08-31 10:23:12 +00:00
|
|
|
// TODO: investigate why animation restarts for 'display' modification in .then()
|
|
|
|
if (typeof onComplete === 'function') {
|
|
|
|
onComplete.call(element);
|
2017-03-25 05:54:58 +00:00
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
element.classList.add(className);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function enforceInputRange(element) {
|
|
|
|
const min = Number(element.min);
|
|
|
|
const max = Number(element.max);
|
2017-04-13 06:12:40 +00:00
|
|
|
const doNotify = () => element.dispatchEvent(new Event('change', {bubbles: true}));
|
|
|
|
const onChange = ({type}) => {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (type === 'input' && element.checkValidity()) {
|
2017-04-13 06:12:40 +00:00
|
|
|
doNotify();
|
2017-07-16 18:02:00 +00:00
|
|
|
} else if (type === 'change' && !element.checkValidity()) {
|
2017-04-13 06:12:40 +00:00
|
|
|
element.value = Math.max(min, Math.min(max, Number(element.value)));
|
|
|
|
doNotify();
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
element.addEventListener('change', onChange);
|
|
|
|
element.addEventListener('input', onChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
function $(selector, base = document) {
|
2017-04-26 00:05:41 +00:00
|
|
|
// we have ids with . like #manage.onlyEnabled which looks like #id.class
|
2017-03-25 05:54:58 +00:00
|
|
|
// so since getElementById is superfast we'll try it anyway
|
|
|
|
const byId = selector.startsWith('#') && document.getElementById(selector.slice(1));
|
|
|
|
return byId || base.querySelector(selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function $$(selector, base = document) {
|
|
|
|
return [...base.querySelectorAll(selector)];
|
|
|
|
}
|
2017-04-17 20:25:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
function $element(opt) {
|
2017-04-24 11:07:35 +00:00
|
|
|
// tag: string, default 'div', may include namespace like 'ns#tag'
|
2017-07-19 12:09:29 +00:00
|
|
|
// appendChild: element/string or an array of elements/strings
|
2017-04-17 20:25:32 +00:00
|
|
|
// dataset: object
|
|
|
|
// any DOM property: assigned as is
|
2017-04-24 11:07:35 +00:00
|
|
|
const [ns, tag] = opt.tag && opt.tag.includes('#')
|
|
|
|
? opt.tag.split('#')
|
|
|
|
: [null, opt.tag];
|
|
|
|
const element = ns
|
2017-07-16 18:02:00 +00:00
|
|
|
? document.createElementNS(ns === 'SVG' || ns === 'svg' ? 'http://www.w3.org/2000/svg' : ns, tag)
|
2017-04-24 11:07:35 +00:00
|
|
|
: document.createElement(tag || 'div');
|
2017-07-19 12:09:29 +00:00
|
|
|
const children = opt.appendChild instanceof Array ? opt.appendChild : [opt.appendChild];
|
|
|
|
for (const child of children) {
|
|
|
|
if (child) {
|
|
|
|
element.appendChild(child instanceof Node ? child : document.createTextNode(child));
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 20:25:32 +00:00
|
|
|
delete opt.appendChild;
|
|
|
|
delete opt.tag;
|
|
|
|
if (opt.dataset) {
|
|
|
|
Object.assign(element.dataset, opt.dataset);
|
|
|
|
delete opt.dataset;
|
|
|
|
}
|
2017-08-31 19:25:05 +00:00
|
|
|
if (opt.attributes) {
|
|
|
|
for (const attr in opt.attributes) {
|
|
|
|
element.setAttribute(attr, opt.attributes[attr]);
|
|
|
|
}
|
|
|
|
delete opt.attributes;
|
|
|
|
}
|
2017-04-24 11:07:35 +00:00
|
|
|
if (ns) {
|
|
|
|
for (const attr in opt) {
|
|
|
|
element.setAttributeNS(null, attr, opt[attr]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Object.assign(element, opt);
|
|
|
|
}
|
|
|
|
return element;
|
2017-04-17 20:25:32 +00:00
|
|
|
}
|