2020-11-18 11:17:15 +00:00
|
|
|
/* global
|
|
|
|
$
|
|
|
|
$$
|
|
|
|
$create
|
|
|
|
animateElement
|
|
|
|
API
|
|
|
|
CHROME
|
|
|
|
CHROME_HAS_BORDER_BUG
|
|
|
|
configDialog
|
|
|
|
FIREFOX
|
|
|
|
getActiveTab
|
|
|
|
hotkeys
|
|
|
|
msg
|
|
|
|
onDOMready
|
|
|
|
prefs
|
|
|
|
setupLivePrefs
|
|
|
|
t
|
|
|
|
tryJSONparse
|
|
|
|
URLS
|
|
|
|
*/
|
2017-12-05 21:14:21 +00:00
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
2017-03-16 13:36:33 +00:00
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
/** @type Element */
|
2017-03-21 01:32:38 +00:00
|
|
|
let installed;
|
2020-02-24 23:16:45 +00:00
|
|
|
/** @type string */
|
2017-03-30 23:18:41 +00:00
|
|
|
let tabURL;
|
2017-04-03 04:13:10 +00:00
|
|
|
const handleEvent = {};
|
2015-01-30 17:28:05 +00:00
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
const ABOUT_BLANK = 'about:blank';
|
2017-06-27 20:13:11 +00:00
|
|
|
const ENTRY_ID_PREFIX_RAW = 'style-';
|
2020-10-04 05:30:02 +00:00
|
|
|
|
|
|
|
$.entry = styleOrId => $(`#${ENTRY_ID_PREFIX_RAW}${styleOrId.id || styleOrId}`);
|
2017-06-27 20:13:11 +00:00
|
|
|
|
2020-05-31 05:43:56 +00:00
|
|
|
if (CHROME >= 66 && CHROME <= 69) { // Chrome 66-69 adds a gap, https://crbug.com/821143
|
2020-02-24 23:16:45 +00:00
|
|
|
document.head.appendChild($create('style', 'html { overflow: overlay }'));
|
|
|
|
}
|
|
|
|
|
2017-11-14 08:13:03 +00:00
|
|
|
toggleSideBorders();
|
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
initTabUrls()
|
|
|
|
.then(frames =>
|
|
|
|
Promise.all([
|
|
|
|
onDOMready().then(() => initPopup(frames)),
|
|
|
|
...frames
|
|
|
|
.filter(f => f.url && !f.isDupe)
|
2020-10-04 05:30:02 +00:00
|
|
|
.map(({url}) => getStyleDataMerged(url).then(styles => ({styles, url}))),
|
2020-02-24 23:16:45 +00:00
|
|
|
]))
|
|
|
|
.then(([, ...results]) => {
|
2020-11-20 14:00:20 +00:00
|
|
|
const sliders = prefs.get('ui.sliders');
|
|
|
|
const slot = $('toggle', t.template.style);
|
|
|
|
const toggle = t.template[sliders ? 'toggleSlider' : 'toggleChecker'];
|
|
|
|
slot.parentElement.replaceChild(toggle.cloneNode(true), slot);
|
|
|
|
document.body.classList.toggle('has-sliders', sliders);
|
2020-02-24 23:16:45 +00:00
|
|
|
if (results[0]) {
|
|
|
|
showStyles(results);
|
|
|
|
} else {
|
2018-11-07 06:09:29 +00:00
|
|
|
// unsupported URL;
|
2020-02-02 04:36:54 +00:00
|
|
|
$('#popup-manage-button').removeAttribute('title');
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(console.error);
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
msg.onExtension(onRuntimeMessage);
|
|
|
|
|
|
|
|
prefs.subscribe(['popup.stylesFirst'], (key, stylesFirst) => {
|
|
|
|
const actions = $('body > .actions');
|
|
|
|
const before = stylesFirst ? actions : actions.nextSibling;
|
|
|
|
document.body.insertBefore(installed, before);
|
|
|
|
});
|
|
|
|
prefs.subscribe(['popupWidth'], (key, value) => setPopupWidth(value));
|
2019-09-24 13:21:36 +00:00
|
|
|
|
|
|
|
if (CHROME_HAS_BORDER_BUG) {
|
|
|
|
prefs.subscribe(['popup.borders'], (key, value) => toggleSideBorders(value));
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function onRuntimeMessage(msg) {
|
2020-10-04 05:30:02 +00:00
|
|
|
if (!tabURL) return;
|
|
|
|
let ready = Promise.resolve();
|
2017-03-31 08:46:18 +00:00
|
|
|
switch (msg.method) {
|
|
|
|
case 'styleAdded':
|
|
|
|
case 'styleUpdated':
|
2018-11-07 06:09:29 +00:00
|
|
|
if (msg.reason === 'editPreview' || msg.reason === 'editPreviewEnd') return;
|
2020-10-04 05:30:02 +00:00
|
|
|
ready = handleUpdate(msg);
|
2017-03-31 08:46:18 +00:00
|
|
|
break;
|
|
|
|
case 'styleDeleted':
|
2018-11-07 06:09:29 +00:00
|
|
|
handleDelete(msg.style.id);
|
2017-03-31 08:46:18 +00:00
|
|
|
break;
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2020-10-04 05:30:02 +00:00
|
|
|
ready.then(() => dispatchEvent(new CustomEvent(msg.method, {detail: msg})));
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
|
2017-04-04 17:21:03 +00:00
|
|
|
function setPopupWidth(width = prefs.get('popupWidth')) {
|
|
|
|
document.body.style.width =
|
|
|
|
Math.max(200, Math.min(800, width)) + 'px';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-14 08:13:03 +00:00
|
|
|
function toggleSideBorders(state = prefs.get('popup.borders')) {
|
|
|
|
// runs before <body> is parsed
|
|
|
|
const style = document.documentElement.style;
|
2019-09-24 13:21:36 +00:00
|
|
|
if (CHROME_HAS_BORDER_BUG && state) {
|
2017-11-14 08:13:03 +00:00
|
|
|
style.cssText +=
|
|
|
|
'border-left: 2px solid white !important;' +
|
|
|
|
'border-right: 2px solid white !important;';
|
|
|
|
} else if (style.cssText) {
|
|
|
|
style.borderLeft = style.borderRight = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-11 11:53:40 +00:00
|
|
|
async function initTabUrls() {
|
|
|
|
let tab = await getActiveTab();
|
|
|
|
if (FIREFOX && tab.status === 'loading' && tab.url === ABOUT_BLANK) {
|
|
|
|
tab = await waitForTabUrlFF(tab);
|
|
|
|
}
|
|
|
|
let frames = await browser.webNavigation.getAllFrames({tabId: tab.id});
|
|
|
|
let url = tab.pendingUrl || tab.url || ''; // new Chrome uses pendingUrl while connecting
|
|
|
|
frames = sortTabFrames(frames);
|
|
|
|
if (url === 'chrome://newtab/' && !URLS.chromeProtectsNTP) {
|
|
|
|
url = frames[0].url || '';
|
|
|
|
}
|
|
|
|
if (!URLS.supported(url)) {
|
|
|
|
url = '';
|
|
|
|
frames.length = 1;
|
|
|
|
}
|
|
|
|
tabURL = frames[0].url = url;
|
|
|
|
return frames;
|
2020-02-24 23:16:45 +00:00
|
|
|
}
|
2017-11-14 08:13:03 +00:00
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
/** @param {chrome.webNavigation.GetAllFrameResultDetails[]} frames */
|
2020-11-22 11:09:59 +00:00
|
|
|
async function initPopup(frames) {
|
2017-03-21 01:32:38 +00:00
|
|
|
installed = $('#installed');
|
|
|
|
|
2017-04-04 17:21:03 +00:00
|
|
|
setPopupWidth();
|
2015-03-12 23:00:23 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
// action buttons
|
2017-07-16 16:49:31 +00:00
|
|
|
$('#disableAll').onchange = function () {
|
2017-04-11 11:22:00 +00:00
|
|
|
installed.classList.toggle('disabled', this.checked);
|
|
|
|
};
|
2017-04-21 16:39:34 +00:00
|
|
|
setupLivePrefs();
|
2017-04-09 06:43:51 +00:00
|
|
|
|
2017-08-22 16:29:26 +00:00
|
|
|
Object.assign($('#popup-manage-button'), {
|
|
|
|
onclick: handleEvent.openManager,
|
|
|
|
oncontextmenu: handleEvent.openManager,
|
|
|
|
});
|
2017-04-09 06:43:51 +00:00
|
|
|
|
|
|
|
$('#popup-options-button').onclick = () => {
|
2020-02-02 04:36:54 +00:00
|
|
|
API.openManage({options: true});
|
2017-04-09 06:43:51 +00:00
|
|
|
window.close();
|
|
|
|
};
|
|
|
|
|
2018-07-02 15:28:38 +00:00
|
|
|
$('#popup-wiki-button').onclick = handleEvent.openURLandHide;
|
2018-04-12 18:02:34 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
if (!prefs.get('popup.stylesFirst')) {
|
|
|
|
document.body.insertBefore(
|
|
|
|
$('body > .actions'),
|
|
|
|
installed);
|
|
|
|
}
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
if (!tabURL) {
|
2020-10-04 05:30:02 +00:00
|
|
|
blockPopup();
|
2017-03-21 01:32:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
frames.forEach(createWriterElement);
|
|
|
|
if (frames.length > 1) {
|
|
|
|
const el = $('#write-for-frames');
|
|
|
|
el.hidden = false;
|
|
|
|
el.onclick = () => el.classList.toggle('expanded');
|
|
|
|
}
|
|
|
|
|
2020-11-22 11:09:59 +00:00
|
|
|
const isStore = tabURL.startsWith(URLS.browserWebStore);
|
|
|
|
if (isStore && !FIREFOX) {
|
|
|
|
blockPopup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let retryCountdown = 10; retryCountdown-- > 0;) {
|
|
|
|
const tab = await getActiveTab();
|
|
|
|
if (await msg.sendTab(tab.id, {method: 'ping'}, {frameId: 0}).catch(() => {})) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tab.status === 'complete' && (!FIREFOX || tab.url !== ABOUT_BLANK)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FF and some Chrome forks (e.g. CentBrowser) implement tab-on-demand
|
|
|
|
// so we'll wait a bit to handle popup being invoked right after switching
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
|
|
|
}
|
|
|
|
const info = t.template.unreachableInfo;
|
|
|
|
if (!FIREFOX) {
|
|
|
|
// Chrome "Allow access to file URLs" in chrome://extensions message
|
|
|
|
info.appendChild($create('p', t('unreachableFileHint')));
|
|
|
|
} else if (isStore) {
|
|
|
|
$('label', info).textContent = t('unreachableAMO');
|
|
|
|
const note = (FIREFOX < 59 ? t('unreachableAMOHintOldFF') : t('unreachableAMOHint')) +
|
|
|
|
(FIREFOX < 60 ? '' : '\n' + t('unreachableAMOHintNewFF'));
|
|
|
|
const renderToken = s => s[0] === '<'
|
|
|
|
? $create('a', {
|
|
|
|
textContent: s.slice(1, -1),
|
|
|
|
onclick: handleEvent.copyContent,
|
|
|
|
href: '#',
|
|
|
|
className: 'copy',
|
|
|
|
tabIndex: 0,
|
|
|
|
title: t('copy'),
|
|
|
|
})
|
|
|
|
: s;
|
|
|
|
const renderLine = line => $create('p', line.split(/(<.*?>)/).map(renderToken));
|
|
|
|
const noteNode = $create('fragment', note.split('\n').map(renderLine));
|
|
|
|
info.appendChild(noteNode);
|
|
|
|
}
|
|
|
|
// Inaccessible locally hosted file type, e.g. JSON, PDF, etc.
|
|
|
|
if (tabURL.length - tabURL.lastIndexOf('.') <= 5) {
|
|
|
|
info.appendChild($create('p', t('InaccessibleFileHint')));
|
|
|
|
}
|
|
|
|
document.body.classList.add('unreachable');
|
|
|
|
document.body.insertBefore(info, document.body.firstChild);
|
2020-02-24 23:16:45 +00:00
|
|
|
}
|
2017-04-12 09:57:40 +00:00
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
/** @param {chrome.webNavigation.GetAllFrameResultDetails} frame */
|
|
|
|
function createWriterElement(frame) {
|
|
|
|
const {url, frameId, parentFrameId, isDupe} = frame;
|
|
|
|
const targets = $create('span');
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
// For this URL
|
2020-11-18 11:17:15 +00:00
|
|
|
const urlLink = t.template.writeStyle.cloneNode(true);
|
2020-02-24 23:16:45 +00:00
|
|
|
const isAboutBlank = url === ABOUT_BLANK;
|
2017-03-21 01:32:38 +00:00
|
|
|
Object.assign(urlLink, {
|
2020-02-24 23:16:45 +00:00
|
|
|
href: 'edit.html?url-prefix=' + encodeURIComponent(url),
|
|
|
|
title: `url-prefix("${url}")`,
|
|
|
|
tabIndex: isAboutBlank ? -1 : 0,
|
2017-03-21 01:32:38 +00:00
|
|
|
textContent: prefs.get('popup.breadcrumbs.usePath')
|
2020-02-24 23:16:45 +00:00
|
|
|
? new URL(url).pathname.slice(1)
|
|
|
|
: frameId
|
|
|
|
? isAboutBlank ? url : 'URL'
|
|
|
|
: t('writeStyleForURL').replace(/ /g, '\u00a0'), // this URL
|
|
|
|
onclick: e => handleEvent.openEditor(e, {'url-prefix': url}),
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
if (prefs.get('popup.breadcrumbs')) {
|
|
|
|
urlLink.onmouseenter =
|
|
|
|
urlLink.onfocus = () => urlLink.parentNode.classList.add('url()');
|
|
|
|
urlLink.onmouseleave =
|
|
|
|
urlLink.onblur = () => urlLink.parentNode.classList.remove('url()');
|
|
|
|
}
|
2020-02-24 23:16:45 +00:00
|
|
|
targets.appendChild(urlLink);
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
// For domain
|
2020-02-24 23:16:45 +00:00
|
|
|
const domains = getDomains(url);
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const domain of domains) {
|
2017-08-14 18:15:29 +00:00
|
|
|
const numParts = domain.length - domain.replace(/\./g, '').length + 1;
|
2017-03-21 01:32:38 +00:00
|
|
|
// Don't include TLD
|
2017-08-14 18:15:29 +00:00
|
|
|
if (domains.length > 1 && numParts === 1) {
|
2017-03-21 01:32:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-11-18 11:17:15 +00:00
|
|
|
const domainLink = t.template.writeStyle.cloneNode(true);
|
2017-03-21 01:32:38 +00:00
|
|
|
Object.assign(domainLink, {
|
|
|
|
href: 'edit.html?domain=' + encodeURIComponent(domain),
|
2017-08-14 18:15:29 +00:00
|
|
|
textContent: numParts > 2 ? domain.split('.')[0] : domain,
|
2017-03-21 01:32:38 +00:00
|
|
|
title: `domain("${domain}")`,
|
2020-02-02 04:36:54 +00:00
|
|
|
onclick: e => handleEvent.openEditor(e, {domain}),
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
2017-08-14 18:15:29 +00:00
|
|
|
domainLink.setAttribute('subdomain', numParts > 1 ? 'true' : '');
|
2020-02-24 23:16:45 +00:00
|
|
|
targets.appendChild(domainLink);
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prefs.get('popup.breadcrumbs')) {
|
2020-02-24 23:16:45 +00:00
|
|
|
targets.classList.add('breadcrumbs');
|
|
|
|
targets.appendChild(urlLink); // making it the last element
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
const root = $('#write-style');
|
|
|
|
const parent = $(`[data-frame-id="${parentFrameId}"]`, root) || root;
|
|
|
|
const child = $create({
|
|
|
|
tag: 'span',
|
|
|
|
className: `match${isDupe ? ' dupe' : ''}${isAboutBlank ? ' about-blank' : ''}`,
|
|
|
|
dataset: {frameId},
|
|
|
|
appendChild: targets,
|
|
|
|
});
|
|
|
|
parent.appendChild(child);
|
|
|
|
parent.dataset.children = (Number(parent.dataset.children) || 0) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDomains(url) {
|
|
|
|
let d = url.split(/[/:]+/, 2)[1];
|
|
|
|
if (!d || url.startsWith('file:')) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const domains = [d];
|
|
|
|
while (d.includes('.')) {
|
|
|
|
d = d.substring(d.indexOf('.') + 1);
|
|
|
|
domains.push(d);
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
2020-02-24 23:16:45 +00:00
|
|
|
return domains;
|
2015-03-23 18:56:11 +00:00
|
|
|
}
|
2012-08-20 01:14:33 +00:00
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
/** @param {chrome.webNavigation.GetAllFrameResultDetails[]} frames */
|
|
|
|
function sortTabFrames(frames) {
|
|
|
|
const unknown = new Map(frames.map(f => [f.frameId, f]));
|
|
|
|
const known = new Map([[0, unknown.get(0) || {frameId: 0, url: ''}]]);
|
|
|
|
unknown.delete(0);
|
|
|
|
let lastSize = 0;
|
|
|
|
while (unknown.size !== lastSize) {
|
|
|
|
for (const [frameId, f] of unknown) {
|
|
|
|
if (known.has(f.parentFrameId)) {
|
|
|
|
unknown.delete(frameId);
|
|
|
|
if (!f.errorOccurred) known.set(frameId, f);
|
|
|
|
if (f.url === ABOUT_BLANK) f.url = known.get(f.parentFrameId).url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastSize = unknown.size; // guard against an infinite loop due to a weird frame structure
|
|
|
|
}
|
|
|
|
const sortedFrames = [...known.values(), ...unknown.values()];
|
|
|
|
const urls = new Set([ABOUT_BLANK]);
|
|
|
|
for (const f of sortedFrames) {
|
|
|
|
if (!f.url) f.url = '';
|
|
|
|
f.isDupe = urls.has(f.url);
|
|
|
|
urls.add(f.url);
|
|
|
|
}
|
|
|
|
return sortedFrames;
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2019-01-01 05:11:45 +00:00
|
|
|
function sortStyles(entries) {
|
|
|
|
const enabledFirst = prefs.get('popup.enabledFirst');
|
2020-02-24 23:16:45 +00:00
|
|
|
return entries.sort(({styleMeta: a}, {styleMeta: b}) =>
|
|
|
|
Boolean(a.frameUrl) - Boolean(b.frameUrl) ||
|
|
|
|
enabledFirst && Boolean(b.enabled) - Boolean(a.enabled) ||
|
2020-10-11 15:12:06 +00:00
|
|
|
(a.customName || a.name).localeCompare(b.customName || b.name));
|
2019-01-01 05:11:45 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
function showStyles(frameResults) {
|
|
|
|
const entries = new Map();
|
|
|
|
frameResults.forEach(({styles = [], url}, index) => {
|
|
|
|
styles.forEach(style => {
|
2020-10-04 05:30:02 +00:00
|
|
|
const {id} = style;
|
2020-02-24 23:16:45 +00:00
|
|
|
if (!entries.has(id)) {
|
|
|
|
style.frameUrl = index === 0 ? '' : url;
|
2020-10-04 05:30:02 +00:00
|
|
|
entries.set(id, createStyleElement(style));
|
2020-02-24 23:16:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
if (entries.size) {
|
2020-10-04 05:30:02 +00:00
|
|
|
resortEntries([...entries.values()]);
|
2020-02-24 23:16:45 +00:00
|
|
|
} else {
|
2020-11-18 11:17:15 +00:00
|
|
|
installed.appendChild(t.template.noStyles);
|
2017-03-30 23:18:41 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
window.dispatchEvent(new Event('showStyles:done'));
|
2012-08-20 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
function resortEntries(entries) {
|
|
|
|
// `entries` is specified only at startup, after that we respect the prefs
|
|
|
|
if (entries || prefs.get('popup.autoResort')) {
|
|
|
|
installed.append(...sortStyles(entries || $$('.entry', installed)));
|
|
|
|
}
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2019-01-01 05:11:45 +00:00
|
|
|
function createStyleElement(style) {
|
2020-10-04 05:30:02 +00:00
|
|
|
let entry = $.entry(style);
|
2018-11-07 06:09:29 +00:00
|
|
|
if (!entry) {
|
2020-11-18 11:17:15 +00:00
|
|
|
entry = t.template.style.cloneNode(true);
|
2018-11-07 06:09:29 +00:00
|
|
|
Object.assign(entry, {
|
|
|
|
id: ENTRY_ID_PREFIX_RAW + style.id,
|
|
|
|
styleId: style.id,
|
|
|
|
styleIsUsercss: Boolean(style.usercssData),
|
|
|
|
onmousedown: handleEvent.maybeEdit,
|
2020-11-18 11:17:15 +00:00
|
|
|
styleMeta: style,
|
2018-11-07 06:09:29 +00:00
|
|
|
});
|
2020-11-20 14:00:20 +00:00
|
|
|
Object.assign($('input', entry), {
|
2018-11-07 06:09:29 +00:00
|
|
|
onclick: handleEvent.toggle,
|
|
|
|
});
|
|
|
|
const editLink = $('.style-edit-link', entry);
|
|
|
|
Object.assign(editLink, {
|
|
|
|
href: editLink.getAttribute('href') + style.id,
|
2020-02-02 04:36:54 +00:00
|
|
|
onclick: e => handleEvent.openEditor(e, {id: style.id}),
|
2018-11-07 06:09:29 +00:00
|
|
|
});
|
|
|
|
const styleName = $('.style-name', entry);
|
|
|
|
Object.assign(styleName, {
|
|
|
|
htmlFor: ENTRY_ID_PREFIX_RAW + style.id,
|
|
|
|
onclick: handleEvent.name,
|
|
|
|
});
|
|
|
|
styleName.appendChild(document.createTextNode(' '));
|
2018-12-10 13:57:44 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
const config = $('.configure', entry);
|
2018-12-10 13:57:44 +00:00
|
|
|
config.onclick = handleEvent.configure;
|
|
|
|
if (!style.usercssData) {
|
|
|
|
if (style.updateUrl && style.updateUrl.includes('?') && style.url) {
|
|
|
|
config.href = style.url;
|
|
|
|
config.target = '_blank';
|
|
|
|
config.title = t('configureStyleOnHomepage');
|
|
|
|
config.dataset.sendMessage = JSON.stringify({method: 'openSettings'});
|
|
|
|
$('use', config).attributes['xlink:href'].nodeValue = '#svg-icon-config-uso';
|
|
|
|
} else {
|
|
|
|
config.classList.add('hidden');
|
|
|
|
}
|
|
|
|
} else if (Object.keys(style.usercssData.vars || {}).length === 0) {
|
|
|
|
config.classList.add('hidden');
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2018-12-10 13:57:44 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
$('.delete', entry).onclick = handleEvent.delete;
|
|
|
|
|
2020-11-18 11:17:15 +00:00
|
|
|
const indicator = t.template.regexpProblemIndicator.cloneNode(true);
|
2018-11-07 06:09:29 +00:00
|
|
|
indicator.appendChild(document.createTextNode('!'));
|
|
|
|
indicator.onclick = handleEvent.indicator;
|
|
|
|
$('.main-controls', entry).appendChild(indicator);
|
2019-03-03 22:54:37 +00:00
|
|
|
|
|
|
|
$('.menu-button', entry).onclick = handleEvent.toggleMenu;
|
2019-06-20 19:37:40 +00:00
|
|
|
$('.menu-close', entry).onclick = handleEvent.toggleMenu;
|
2019-03-03 22:54:37 +00:00
|
|
|
|
|
|
|
$('.exclude-by-domain-checkbox', entry).onchange = e => handleEvent.toggleExclude(e, 'domain');
|
|
|
|
$('.exclude-by-url-checkbox', entry).onchange = e => handleEvent.toggleExclude(e, 'url');
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
style = Object.assign(entry.styleMeta, style);
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
entry.classList.toggle('disabled', !style.enabled);
|
|
|
|
entry.classList.toggle('enabled', style.enabled);
|
2020-11-20 14:00:20 +00:00
|
|
|
$('input', entry).checked = style.enabled;
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
const styleName = $('.style-name', entry);
|
2020-10-11 15:12:06 +00:00
|
|
|
styleName.lastChild.textContent = style.customName || style.name;
|
2018-11-07 06:09:29 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
styleName.title = entry.styleMeta.sloppy ?
|
|
|
|
t('styleNotAppliedRegexpProblemTooltip') :
|
|
|
|
styleName.scrollWidth > styleName.clientWidth + 1 ?
|
|
|
|
styleName.textContent : '';
|
2017-11-26 12:31:00 +00:00
|
|
|
});
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
entry.classList.toggle('not-applied', style.excluded || style.sloppy);
|
|
|
|
entry.classList.toggle('regexp-partial', style.sloppy);
|
|
|
|
|
2019-03-03 22:54:37 +00:00
|
|
|
$('.exclude-by-domain-checkbox', entry).checked = styleExcluded(style, 'domain');
|
2019-06-11 14:44:32 +00:00
|
|
|
$('.exclude-by-url-checkbox', entry).checked = styleExcluded(style, 'url');
|
2019-03-03 22:54:37 +00:00
|
|
|
|
2019-06-11 14:44:32 +00:00
|
|
|
$('.exclude-by-domain', entry).title = getExcludeRule('domain');
|
|
|
|
$('.exclude-by-url', entry).title = getExcludeRule('url');
|
2019-03-03 22:54:37 +00:00
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
const {frameUrl} = style;
|
|
|
|
if (frameUrl) {
|
|
|
|
const sel = 'span.frame-url';
|
|
|
|
const frameEl = $(sel, entry) || styleName.insertBefore($create(sel), styleName.lastChild);
|
|
|
|
frameEl.title = frameUrl;
|
2020-11-22 11:09:59 +00:00
|
|
|
frameEl.onmousedown = handleEvent.maybeEdit;
|
2020-02-24 23:16:45 +00:00
|
|
|
}
|
|
|
|
entry.classList.toggle('frame', Boolean(frameUrl));
|
|
|
|
|
2019-01-01 05:11:45 +00:00
|
|
|
return entry;
|
2012-08-20 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 22:54:37 +00:00
|
|
|
function styleExcluded({exclusions}, type) {
|
|
|
|
if (!exclusions) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const rule = getExcludeRule(type);
|
|
|
|
return exclusions.includes(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getExcludeRule(type) {
|
2019-06-11 14:44:32 +00:00
|
|
|
const u = new URL(tabURL);
|
2019-03-03 22:54:37 +00:00
|
|
|
if (type === 'domain') {
|
2019-06-11 14:44:32 +00:00
|
|
|
return u.origin + '/*';
|
2019-03-03 22:54:37 +00:00
|
|
|
}
|
2019-06-11 14:44:32 +00:00
|
|
|
// current page
|
|
|
|
return escapeGlob(u.origin + u.pathname);
|
|
|
|
}
|
|
|
|
|
|
|
|
function escapeGlob(text) {
|
|
|
|
return text.replace(/\*/g, '\\*');
|
2019-03-03 22:54:37 +00:00
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
Object.assign(handleEvent, {
|
2017-03-25 03:18:14 +00:00
|
|
|
|
2017-04-09 18:33:17 +00:00
|
|
|
getClickedStyleId(event) {
|
|
|
|
return (handleEvent.getClickedStyleElement(event) || {}).styleId;
|
|
|
|
},
|
|
|
|
|
|
|
|
getClickedStyleElement(event) {
|
|
|
|
return event.target.closest('.entry');
|
|
|
|
},
|
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
name(event) {
|
2020-11-20 14:00:20 +00:00
|
|
|
$('input', this).dispatchEvent(new MouseEvent('click'));
|
2017-03-25 03:18:14 +00:00
|
|
|
event.preventDefault();
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-25 03:18:14 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
toggle(event) {
|
2018-09-13 05:36:19 +00:00
|
|
|
// when fired on checkbox, prevent the parent label from seeing the event, see #501
|
|
|
|
event.stopPropagation();
|
2019-01-01 05:11:45 +00:00
|
|
|
API
|
|
|
|
.toggleStyle(handleEvent.getClickedStyleId(event), this.checked)
|
2020-10-04 05:30:02 +00:00
|
|
|
.then(() => resortEntries());
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-25 03:18:14 +00:00
|
|
|
|
2019-03-03 22:54:37 +00:00
|
|
|
toggleExclude(event, type) {
|
|
|
|
const entry = handleEvent.getClickedStyleElement(event);
|
|
|
|
if (event.target.checked) {
|
|
|
|
API.addExclusion(entry.styleMeta.id, getExcludeRule(type));
|
|
|
|
} else {
|
|
|
|
API.removeExclusion(entry.styleMeta.id, getExcludeRule(type));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleMenu(event) {
|
|
|
|
const entry = handleEvent.getClickedStyleElement(event);
|
2019-06-20 19:37:40 +00:00
|
|
|
const menu = $('.menu', entry);
|
|
|
|
const menuActive = $('.menu[data-display=true]');
|
|
|
|
if (menuActive) {
|
|
|
|
// fade-out style menu
|
2020-11-06 18:04:10 +00:00
|
|
|
animateElement(menu, 'lights-on')
|
|
|
|
.then(() => (menu.dataset.display = false));
|
2019-06-20 19:37:40 +00:00
|
|
|
window.onkeydown = null;
|
|
|
|
} else {
|
|
|
|
$('.menu-title', entry).textContent = $('.style-name', entry).textContent;
|
|
|
|
menu.dataset.display = true;
|
|
|
|
menu.style.cssText = '';
|
|
|
|
window.onkeydown = event => {
|
|
|
|
const close = $('.menu-close', entry);
|
|
|
|
const checkbox = $('.exclude-by-domain-checkbox', entry);
|
2020-10-13 18:14:54 +00:00
|
|
|
if (document.activeElement === close && (event.key === 'Tab') && !event.shiftKey) {
|
2019-06-20 19:37:40 +00:00
|
|
|
event.preventDefault();
|
|
|
|
checkbox.focus();
|
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
if (document.activeElement === checkbox && (event.key === 'Tab') && event.shiftKey) {
|
2019-06-20 19:37:40 +00:00
|
|
|
event.preventDefault();
|
|
|
|
close.focus();
|
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
if (event.key === 'Escape') {
|
2019-06-20 19:37:40 +00:00
|
|
|
event.preventDefault();
|
|
|
|
close.click();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-03-03 22:54:37 +00:00
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
delete(event) {
|
2018-01-01 17:02:49 +00:00
|
|
|
const entry = handleEvent.getClickedStyleElement(event);
|
|
|
|
const id = entry.styleId;
|
2017-03-25 19:50:37 +00:00
|
|
|
const box = $('#confirm');
|
2019-06-20 19:37:40 +00:00
|
|
|
const menu = $('.menu', entry);
|
|
|
|
const cancel = $('[data-cmd="cancel"]', box);
|
|
|
|
const affirm = $('[data-cmd="ok"]', box);
|
2017-03-25 19:50:37 +00:00
|
|
|
box.dataset.display = true;
|
|
|
|
box.style.cssText = '';
|
2018-01-01 17:02:49 +00:00
|
|
|
$('b', box).textContent = $('.style-name', entry).textContent;
|
2019-06-20 19:37:40 +00:00
|
|
|
affirm.focus();
|
|
|
|
affirm.onclick = () => confirm(true);
|
|
|
|
cancel.onclick = () => confirm(false);
|
2017-03-25 19:50:37 +00:00
|
|
|
window.onkeydown = event => {
|
2019-06-20 19:37:40 +00:00
|
|
|
const close = $('.menu-close', entry);
|
|
|
|
const checkbox = $('.exclude-by-domain-checkbox', entry);
|
|
|
|
const confirmActive = $('#confirm[data-display="true"]');
|
2020-10-13 18:14:54 +00:00
|
|
|
const {key} = event;
|
|
|
|
if (document.activeElement === cancel && (key === 'Tab')) {
|
2019-06-20 19:37:40 +00:00
|
|
|
event.preventDefault();
|
|
|
|
affirm.focus();
|
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
if (document.activeElement === close && (key === 'Tab') && !event.shiftKey) {
|
2017-03-25 19:50:37 +00:00
|
|
|
event.preventDefault();
|
2019-06-20 19:37:40 +00:00
|
|
|
checkbox.focus();
|
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
if (document.activeElement === checkbox && (key === 'Tab') && event.shiftKey) {
|
2019-06-20 19:37:40 +00:00
|
|
|
event.preventDefault();
|
|
|
|
close.focus();
|
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
if (key === 'Escape') {
|
2019-06-20 19:37:40 +00:00
|
|
|
event.preventDefault();
|
|
|
|
if (confirmActive) {
|
|
|
|
box.dataset.display = false;
|
|
|
|
menu.focus();
|
|
|
|
} else {
|
|
|
|
close.click();
|
|
|
|
}
|
2017-03-25 03:18:14 +00:00
|
|
|
}
|
2017-03-25 19:50:37 +00:00
|
|
|
};
|
|
|
|
function confirm(ok) {
|
2019-06-20 19:37:40 +00:00
|
|
|
if (ok) {
|
|
|
|
// fade-out deletion confirmation dialog
|
2020-11-06 18:04:10 +00:00
|
|
|
animateElement(box, 'lights-on')
|
|
|
|
.then(() => (box.dataset.display = false));
|
2019-06-20 19:37:40 +00:00
|
|
|
window.onkeydown = null;
|
|
|
|
API.deleteStyle(id);
|
|
|
|
} else {
|
|
|
|
box.dataset.display = false;
|
|
|
|
menu.focus();
|
|
|
|
}
|
2017-03-25 19:50:37 +00:00
|
|
|
}
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-25 03:18:14 +00:00
|
|
|
|
2017-12-05 21:14:21 +00:00
|
|
|
configure(event) {
|
|
|
|
const {styleId, styleIsUsercss} = handleEvent.getClickedStyleElement(event);
|
|
|
|
if (styleIsUsercss) {
|
2018-11-07 06:09:29 +00:00
|
|
|
API.getStyle(styleId, true).then(style => {
|
2017-12-05 21:14:21 +00:00
|
|
|
hotkeys.setState(false);
|
2018-01-05 10:26:11 +00:00
|
|
|
configDialog(style).then(() => {
|
2017-12-05 21:14:21 +00:00
|
|
|
hotkeys.setState(true);
|
|
|
|
});
|
|
|
|
});
|
2017-12-10 04:56:41 +00:00
|
|
|
} else {
|
|
|
|
handleEvent.openURLandHide.call(this, event);
|
2017-12-05 21:14:21 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
indicator(event) {
|
2017-04-09 18:33:17 +00:00
|
|
|
const entry = handleEvent.getClickedStyleElement(event);
|
2020-11-18 11:17:15 +00:00
|
|
|
const info = t.template.regexpProblemExplanation.cloneNode(true);
|
2017-12-02 16:54:54 +00:00
|
|
|
$.remove('#' + info.id);
|
2017-04-03 04:13:10 +00:00
|
|
|
$$('a', info).forEach(el => (el.onclick = handleEvent.openURLandHide));
|
|
|
|
$$('button', info).forEach(el => (el.onclick = handleEvent.closeExplanation));
|
2017-03-30 23:18:41 +00:00
|
|
|
entry.appendChild(info);
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-30 23:18:41 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
closeExplanation() {
|
2017-03-30 23:18:41 +00:00
|
|
|
$('#regexp-explanation').remove();
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-25 03:18:14 +00:00
|
|
|
|
2020-02-02 04:36:54 +00:00
|
|
|
openEditor(event, options) {
|
2017-03-21 01:32:38 +00:00
|
|
|
event.preventDefault();
|
2020-02-02 04:36:54 +00:00
|
|
|
API.openEditor(options);
|
|
|
|
window.close();
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
|
|
|
|
2017-04-17 16:48:39 +00:00
|
|
|
maybeEdit(event) {
|
|
|
|
if (!(
|
2017-07-16 18:02:00 +00:00
|
|
|
event.button === 0 && (event.ctrlKey || event.metaKey) ||
|
|
|
|
event.button === 1 ||
|
|
|
|
event.button === 2)) {
|
2017-04-03 04:13:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// open an editor on middleclick
|
2020-11-22 11:09:59 +00:00
|
|
|
const el = event.target;
|
|
|
|
if (el.matches('.entry, .style-edit-link') || el.closest('.style-name')) {
|
2017-04-17 16:48:39 +00:00
|
|
|
this.onmouseup = () => $('.style-edit-link', this).click();
|
2017-04-26 15:44:23 +00:00
|
|
|
this.oncontextmenu = event => event.preventDefault();
|
2017-04-03 04:13:10 +00:00
|
|
|
event.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// prevent the popup being opened in a background tab
|
|
|
|
// when an irrelevant link was accidentally clicked
|
2020-11-22 11:09:59 +00:00
|
|
|
if (el.closest('a')) {
|
2017-04-03 04:13:10 +00:00
|
|
|
event.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
openURLandHide(event) {
|
|
|
|
event.preventDefault();
|
2017-12-10 04:56:41 +00:00
|
|
|
getActiveTab()
|
2018-03-13 15:35:28 +00:00
|
|
|
.then(activeTab => API.openURL({
|
2017-12-10 04:56:41 +00:00
|
|
|
url: this.href || this.dataset.href,
|
2020-02-24 23:16:45 +00:00
|
|
|
index: activeTab.index + 1,
|
|
|
|
message: tryJSONparse(this.dataset.sendMessage),
|
2017-12-10 04:56:41 +00:00
|
|
|
}))
|
2017-04-09 06:43:51 +00:00
|
|
|
.then(window.close);
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-08-22 16:29:26 +00:00
|
|
|
|
|
|
|
openManager(event) {
|
|
|
|
event.preventDefault();
|
2020-11-18 11:17:15 +00:00
|
|
|
const isSearch = tabURL && (event.shiftKey || event.button === 2);
|
|
|
|
API.openManage(isSearch ? {search: tabURL, searchMode: 'url'} : {});
|
|
|
|
window.close();
|
2017-08-22 16:29:26 +00:00
|
|
|
},
|
2019-11-18 03:23:12 +00:00
|
|
|
|
|
|
|
copyContent(event) {
|
2020-01-07 00:13:49 +00:00
|
|
|
event.preventDefault();
|
|
|
|
const target = document.activeElement;
|
2019-11-18 03:23:12 +00:00
|
|
|
const message = $('.copy-message');
|
|
|
|
navigator.clipboard.writeText(target.textContent);
|
|
|
|
target.classList.add('copied');
|
|
|
|
message.classList.add('show-message');
|
|
|
|
setTimeout(() => {
|
|
|
|
target.classList.remove('copied');
|
|
|
|
message.classList.remove('show-message');
|
|
|
|
}, 1000);
|
|
|
|
},
|
2017-04-03 04:13:10 +00:00
|
|
|
});
|
2012-08-20 01:14:33 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
async function handleUpdate({style, reason}) {
|
|
|
|
if (reason !== 'toggle' || !$.entry(style)) {
|
|
|
|
style = await getStyleDataMerged(tabURL, style.id);
|
|
|
|
if (!style) return;
|
|
|
|
}
|
|
|
|
const el = createStyleElement(style);
|
|
|
|
if (!el.parentNode) {
|
|
|
|
installed.appendChild(el);
|
|
|
|
blockPopup(false);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2020-10-04 05:30:02 +00:00
|
|
|
resortEntries();
|
2012-08-20 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2012-08-20 01:14:33 +00:00
|
|
|
function handleDelete(id) {
|
2020-10-04 05:30:02 +00:00
|
|
|
const el = $.entry(id);
|
|
|
|
if (el) {
|
|
|
|
el.remove();
|
2020-11-18 11:17:15 +00:00
|
|
|
if (!$('.entry')) installed.appendChild(t.template.noStyles);
|
2017-12-12 02:21:06 +00:00
|
|
|
}
|
2017-03-30 23:18:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
function waitForTabUrlFF(tab) {
|
2017-08-18 11:41:27 +00:00
|
|
|
return new Promise(resolve => {
|
2020-02-24 23:16:45 +00:00
|
|
|
browser.tabs.onUpdated.addListener(...[
|
|
|
|
function onUpdated(tabId, info, updatedTab) {
|
|
|
|
if (info.url && tabId === tab.id) {
|
|
|
|
chrome.tabs.onUpdated.removeListener(onUpdated);
|
|
|
|
resolve(updatedTab);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
...'UpdateFilter' in browser.tabs ? [{tabId: tab.id}] : [],
|
|
|
|
// TODO: remove both spreads and tabId check when strict_min_version >= 61
|
|
|
|
]);
|
2017-08-18 11:41:27 +00:00
|
|
|
});
|
|
|
|
}
|
2020-10-04 05:30:02 +00:00
|
|
|
|
|
|
|
/* Merges the extra props from API into style data.
|
|
|
|
* When `id` is specified returns a single object otherwise an array */
|
|
|
|
async function getStyleDataMerged(url, id) {
|
|
|
|
const styles = (await API.getStylesByUrl(url, id))
|
|
|
|
.map(r => Object.assign(r.data, r));
|
|
|
|
return id ? styles[0] : styles;
|
|
|
|
}
|
|
|
|
|
|
|
|
function blockPopup(isBlocked = true) {
|
|
|
|
document.body.classList.toggle('blocked', isBlocked);
|
|
|
|
if (isBlocked) {
|
2020-11-18 11:17:15 +00:00
|
|
|
document.body.prepend(t.template.unavailableInfo);
|
2020-10-04 05:30:02 +00:00
|
|
|
} else {
|
2020-11-18 11:17:15 +00:00
|
|
|
t.template.unavailableInfo.remove();
|
|
|
|
t.template.noStyles.remove();
|
2020-10-04 05:30:02 +00:00
|
|
|
}
|
|
|
|
}
|