2017-12-05 21:14:21 +00:00
|
|
|
/* global configDialog hotkeys */
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
2017-03-16 13:36:33 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
let installed;
|
2017-03-30 23:18:41 +00:00
|
|
|
let tabURL;
|
2017-04-03 04:13:10 +00:00
|
|
|
const handleEvent = {};
|
2018-10-13 22:07:52 +00:00
|
|
|
const doc = document.documentElement;
|
2015-01-30 17:28:05 +00:00
|
|
|
|
2017-06-27 20:13:11 +00:00
|
|
|
const ENTRY_ID_PREFIX_RAW = 'style-';
|
|
|
|
const ENTRY_ID_PREFIX = '#' + ENTRY_ID_PREFIX_RAW;
|
|
|
|
|
2017-11-14 08:13:03 +00:00
|
|
|
toggleSideBorders();
|
|
|
|
|
2018-10-13 22:07:52 +00:00
|
|
|
doc.classList.add(prefs.get('popupui') === 1 ? 'classicUI' : 'iconUI');
|
2018-10-12 20:31:59 +00:00
|
|
|
|
2018-10-13 22:07:52 +00:00
|
|
|
doc.classList.add(prefs.get('popupclick') === 1 ? 'toggleUI' : 'directLinkUI');
|
2018-10-12 20:31:59 +00:00
|
|
|
|
|
|
|
if (!prefs.get('popup.stylesFirst')) {
|
2018-10-13 22:07:52 +00:00
|
|
|
doc.classList.add('actions-top');
|
2018-10-12 20:31:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 11:41:27 +00:00
|
|
|
getActiveTab().then(tab =>
|
|
|
|
FIREFOX && tab.url === 'about:blank' && tab.status === 'loading'
|
|
|
|
? getTabRealURLFirefox(tab)
|
|
|
|
: getTabRealURL(tab)
|
2018-01-01 17:02:49 +00:00
|
|
|
).then(url => Promise.all([
|
|
|
|
(tabURL = URLS.supported(url) ? url : '') &&
|
|
|
|
API.getStyles({
|
|
|
|
matchUrl: tabURL,
|
|
|
|
omitCode: !BG,
|
|
|
|
}),
|
|
|
|
onDOMready().then(initPopup),
|
|
|
|
])).then(([styles]) => {
|
|
|
|
showStyles(styles);
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
chrome.runtime.onMessage.addListener(onRuntimeMessage);
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function onRuntimeMessage(msg) {
|
2017-03-31 08:46:18 +00:00
|
|
|
switch (msg.method) {
|
|
|
|
case 'styleAdded':
|
|
|
|
case 'styleUpdated':
|
2018-01-10 18:56:14 +00:00
|
|
|
if (msg.reason === 'editPreview') return;
|
2018-01-01 17:02:49 +00:00
|
|
|
handleUpdate(msg.style);
|
2017-03-31 08:46:18 +00:00
|
|
|
break;
|
|
|
|
case 'styleDeleted':
|
|
|
|
handleDelete(msg.id);
|
|
|
|
break;
|
|
|
|
case 'prefChanged':
|
|
|
|
if ('popup.stylesFirst' in msg.prefs) {
|
|
|
|
const stylesFirst = msg.prefs['popup.stylesFirst'];
|
|
|
|
const actions = $('body > .actions');
|
|
|
|
const before = stylesFirst ? actions : actions.nextSibling;
|
|
|
|
document.body.insertBefore(installed, before);
|
2017-04-04 17:21:03 +00:00
|
|
|
} else if ('popupWidth' in msg.prefs) {
|
|
|
|
setPopupWidth(msg.prefs.popupWidth);
|
2017-11-14 08:13:03 +00:00
|
|
|
} else if ('popup.borders' in msg.prefs) {
|
|
|
|
toggleSideBorders(msg.prefs['popup.borders']);
|
2017-03-31 08:46:18 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2017-12-10 01:03:04 +00:00
|
|
|
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;
|
|
|
|
if (CHROME >= 3167 && state) {
|
|
|
|
style.cssText +=
|
|
|
|
'border-left: 2px solid white !important;' +
|
|
|
|
'border-right: 2px solid white !important;';
|
|
|
|
} else if (style.cssText) {
|
|
|
|
style.borderLeft = style.borderRight = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
function initPopup() {
|
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 () {
|
2018-10-12 20:31:59 +00:00
|
|
|
document.body.classList.toggle('disabled', this.checked);
|
|
|
|
};
|
|
|
|
|
2018-10-18 20:35:48 +00:00
|
|
|
$('#find-styles-icon').onclick = () => {
|
|
|
|
$('#find-styles-link').click();
|
|
|
|
};
|
|
|
|
|
2018-10-14 14:34:13 +00:00
|
|
|
$('#disable-all-icon').onclick = () => {
|
|
|
|
$('#disableAll').click();
|
|
|
|
};
|
|
|
|
|
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,
|
|
|
|
onmouseup: handleEvent.openManager,
|
|
|
|
oncontextmenu: handleEvent.openManager,
|
|
|
|
});
|
2017-04-09 06:43:51 +00:00
|
|
|
|
2018-10-13 22:07:52 +00:00
|
|
|
$('#find-external-icon').onclick = event => {
|
2018-10-12 20:31:59 +00:00
|
|
|
event.preventDefault();
|
|
|
|
$('input.toggle-inline').click()
|
|
|
|
};
|
|
|
|
|
2018-10-13 22:07:52 +00:00
|
|
|
$('#find-inline-icon').onclick = event => {
|
2018-10-12 20:31:59 +00:00
|
|
|
event.preventDefault();
|
|
|
|
$('input.toggle-inline').click()
|
|
|
|
};
|
|
|
|
|
2017-04-09 06:43:51 +00:00
|
|
|
$('#popup-options-button').onclick = () => {
|
|
|
|
chrome.runtime.openOptionsPage();
|
|
|
|
window.close();
|
|
|
|
};
|
|
|
|
|
2018-07-02 15:28:38 +00:00
|
|
|
$('#popup-wiki-button').onclick = handleEvent.openURLandHide;
|
2018-04-12 18:02:34 +00:00
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
if (!tabURL) {
|
2017-03-21 01:32:38 +00:00
|
|
|
document.body.classList.add('blocked');
|
2017-08-27 09:56:09 +00:00
|
|
|
document.body.insertBefore(template.unavailableInfo, document.body.firstChild);
|
2017-03-21 01:32:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-18 11:41:27 +00:00
|
|
|
getActiveTab().then(function ping(tab, retryCountdown = 10) {
|
2017-11-24 15:26:20 +00:00
|
|
|
sendMessage({tabId: tab.id, method: 'ping', frameId: 0}, pong => {
|
2017-08-18 11:41:27 +00:00
|
|
|
if (pong) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ignoreChromeError();
|
|
|
|
// 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
|
2017-12-22 05:36:10 +00:00
|
|
|
if (retryCountdown > 0 && (
|
2017-08-23 06:46:36 +00:00
|
|
|
tab.status !== 'complete' ||
|
2017-12-22 05:36:10 +00:00
|
|
|
FIREFOX && tab.url === 'about:blank')) {
|
2017-08-18 11:41:27 +00:00
|
|
|
setTimeout(ping, 100, tab, --retryCountdown);
|
2017-12-22 05:36:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const info = template.unreachableInfo;
|
|
|
|
if (FIREFOX && tabURL.startsWith(URLS.browserWebStore)) {
|
|
|
|
$('label', info).textContent = t('unreachableAMO');
|
2018-03-22 22:44:40 +00:00
|
|
|
const note = (FIREFOX < 59 ? t('unreachableAMOHintOldFF') : t('unreachableAMOHint')) +
|
|
|
|
(FIREFOX < 60 ? '' : '\n' + t('unreachableAMOHintNewFF'));
|
2018-10-12 23:27:57 +00:00
|
|
|
const renderToken = s => s[0] === '<' ? $create('b', tWordBreak(s.slice(1, -1))) : s;
|
2018-03-22 22:44:40 +00:00
|
|
|
const renderLine = line => $create('p', line.split(/(<.*?>)/).map(renderToken));
|
|
|
|
const noteNode = $create('fragment', note.split('\n').map(renderLine));
|
|
|
|
const target = $('p', info);
|
|
|
|
target.parentNode.insertBefore(noteNode, target);
|
|
|
|
target.remove();
|
2017-04-12 09:57:40 +00:00
|
|
|
}
|
2017-12-22 05:36:10 +00:00
|
|
|
document.body.classList.add('unreachable');
|
|
|
|
document.body.insertBefore(info, document.body.firstChild);
|
2017-04-12 09:57:40 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
// Write new style links
|
|
|
|
const writeStyle = $('#write-style');
|
|
|
|
const matchTargets = document.createElement('span');
|
2017-04-01 04:37:25 +00:00
|
|
|
const matchWrapper = document.createElement('span');
|
|
|
|
matchWrapper.id = 'match';
|
|
|
|
matchWrapper.appendChild(matchTargets);
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
// For this URL
|
|
|
|
const urlLink = template.writeStyle.cloneNode(true);
|
|
|
|
Object.assign(urlLink, {
|
2018-01-01 17:02:49 +00:00
|
|
|
href: 'edit.html?url-prefix=' + encodeURIComponent(tabURL),
|
|
|
|
title: `url-prefix("${tabURL}")`,
|
2017-03-21 01:32:38 +00:00
|
|
|
textContent: prefs.get('popup.breadcrumbs.usePath')
|
2018-01-01 17:02:49 +00:00
|
|
|
? new URL(tabURL).pathname.slice(1)
|
2017-08-20 20:14:12 +00:00
|
|
|
// this URL
|
|
|
|
: t('writeStyleForURL').replace(/ /g, '\u00a0'),
|
2017-04-03 04:13:10 +00:00
|
|
|
onclick: handleEvent.openLink,
|
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()');
|
|
|
|
}
|
|
|
|
matchTargets.appendChild(urlLink);
|
|
|
|
|
|
|
|
// For domain
|
2018-01-01 17:02:49 +00:00
|
|
|
const domains = getDomains(tabURL);
|
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;
|
|
|
|
}
|
|
|
|
const domainLink = template.writeStyle.cloneNode(true);
|
|
|
|
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}")`,
|
2017-04-03 04:13:10 +00:00
|
|
|
onclick: handleEvent.openLink,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
2017-08-14 18:15:29 +00:00
|
|
|
domainLink.setAttribute('subdomain', numParts > 1 ? 'true' : '');
|
2017-03-21 01:32:38 +00:00
|
|
|
matchTargets.appendChild(domainLink);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefs.get('popup.breadcrumbs')) {
|
|
|
|
matchTargets.classList.add('breadcrumbs');
|
|
|
|
matchTargets.appendChild(matchTargets.removeChild(matchTargets.firstElementChild));
|
|
|
|
}
|
2017-04-01 04:37:25 +00:00
|
|
|
writeStyle.appendChild(matchWrapper);
|
2018-01-01 17:02:49 +00:00
|
|
|
|
|
|
|
function getDomains(url) {
|
|
|
|
let d = /.*?:\/*([^/:]+)|$/.exec(url)[1];
|
|
|
|
if (!d || url.startsWith('file:')) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const domains = [d];
|
|
|
|
while (d.indexOf('.') !== -1) {
|
|
|
|
d = d.substring(d.indexOf('.') + 1);
|
|
|
|
domains.push(d);
|
|
|
|
}
|
|
|
|
return domains;
|
|
|
|
}
|
2015-03-23 18:56:11 +00:00
|
|
|
}
|
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 showStyles(styles) {
|
2017-03-30 23:18:41 +00:00
|
|
|
if (!styles) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
if (!styles.length) {
|
2017-07-19 08:22:16 +00:00
|
|
|
installed.appendChild(template.noStyles.cloneNode(true));
|
2017-12-10 01:03:04 +00:00
|
|
|
window.dispatchEvent(new Event('showStyles:done'));
|
2017-03-30 23:18:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const enabledFirst = prefs.get('popup.enabledFirst');
|
|
|
|
styles.sort((a, b) => (
|
|
|
|
enabledFirst && a.enabled !== b.enabled
|
|
|
|
? !(a.enabled < b.enabled) ? -1 : 1
|
|
|
|
: a.name.localeCompare(b.name)
|
|
|
|
));
|
|
|
|
|
|
|
|
const container = document.createDocumentFragment();
|
2018-01-01 17:02:49 +00:00
|
|
|
styles.forEach(style => createStyleElement({style, container}));
|
2017-03-30 23:18:41 +00:00
|
|
|
installed.appendChild(container);
|
2018-01-01 17:02:49 +00:00
|
|
|
setTimeout(detectSloppyRegexps, 100, styles);
|
|
|
|
|
|
|
|
API.getStyles({
|
|
|
|
matchUrl: tabURL,
|
|
|
|
strictRegexp: false,
|
|
|
|
omitCode: true,
|
|
|
|
}).then(unscreenedStyles => {
|
|
|
|
for (const style of unscreenedStyles) {
|
|
|
|
if (!styles.find(({id}) => id === style.id)) {
|
|
|
|
createStyleElement({style, check: true});
|
2017-03-30 23:18:41 +00:00
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
|
|
|
window.dispatchEvent(new Event('showStyles:done'));
|
|
|
|
});
|
2018-10-12 20:31:59 +00:00
|
|
|
|
|
|
|
var reverseZebra = $('.entry:last-child:nth-of-type(odd)') !== null;
|
2018-10-13 22:07:52 +00:00
|
|
|
installed.classList.toggle('reverse-stripe', reverseZebra);
|
2012-08-20 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-03-30 23:18:41 +00:00
|
|
|
function createStyleElement({
|
|
|
|
style,
|
2018-01-01 17:02:49 +00:00
|
|
|
check = false,
|
2017-03-30 23:18:41 +00:00
|
|
|
container = installed,
|
|
|
|
}) {
|
2017-03-21 01:32:38 +00:00
|
|
|
const entry = template.style.cloneNode(true);
|
|
|
|
entry.setAttribute('style-id', style.id);
|
|
|
|
Object.assign(entry, {
|
2017-06-27 20:13:11 +00:00
|
|
|
id: ENTRY_ID_PREFIX_RAW + style.id,
|
2017-03-21 01:32:38 +00:00
|
|
|
styleId: style.id,
|
2017-12-05 21:14:21 +00:00
|
|
|
styleIsUsercss: Boolean(style.usercssData),
|
2017-03-25 03:04:24 +00:00
|
|
|
className: entry.className + ' ' + (style.enabled ? 'enabled' : 'disabled'),
|
2017-04-17 16:48:39 +00:00
|
|
|
onmousedown: handleEvent.maybeEdit,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const checkbox = $('.checker', entry);
|
|
|
|
Object.assign(checkbox, {
|
2017-06-27 20:13:11 +00:00
|
|
|
id: ENTRY_ID_PREFIX_RAW + style.id,
|
2017-03-21 01:32:38 +00:00
|
|
|
checked: style.enabled,
|
2017-04-03 04:13:10 +00:00
|
|
|
onclick: handleEvent.toggle,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const editLink = $('.style-edit-link', entry);
|
|
|
|
Object.assign(editLink, {
|
|
|
|
href: editLink.getAttribute('href') + style.id,
|
2017-04-03 04:13:10 +00:00
|
|
|
onclick: handleEvent.openLink,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
|
2018-10-12 20:31:59 +00:00
|
|
|
const editLinkAccess = $('.style-edit-link-accessibility', entry);
|
|
|
|
Object.assign(editLinkAccess, {
|
|
|
|
href: editLinkAccess.getAttribute('href') + style.id,
|
|
|
|
onclick: handleEvent.openLink,
|
|
|
|
});
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
const styleName = $('.style-name', entry);
|
|
|
|
Object.assign(styleName, {
|
2017-06-27 20:13:11 +00:00
|
|
|
htmlFor: ENTRY_ID_PREFIX_RAW + style.id,
|
2017-04-03 04:13:10 +00:00
|
|
|
onclick: handleEvent.name,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
styleName.checkbox = checkbox;
|
|
|
|
styleName.appendChild(document.createTextNode(style.name));
|
2017-11-26 12:31:00 +00:00
|
|
|
setTimeout((el = styleName) => {
|
|
|
|
if (el.scrollWidth > el.clientWidth + 1) {
|
|
|
|
el.title = el.textContent;
|
|
|
|
}
|
|
|
|
});
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-12-05 21:14:21 +00:00
|
|
|
const config = $('.configure', entry);
|
|
|
|
if (!style.usercssData && style.updateUrl && style.updateUrl.includes('?') && style.url) {
|
|
|
|
config.href = style.url;
|
|
|
|
config.target = '_blank';
|
2017-12-07 17:58:02 +00:00
|
|
|
config.title = t('configureStyleOnHomepage');
|
2017-12-10 04:56:41 +00:00
|
|
|
config.dataset.sendMessage = JSON.stringify({method: 'openSettings'});
|
2017-12-06 21:11:30 +00:00
|
|
|
$('use', config).attributes['xlink:href'].nodeValue = '#svg-icon-config-uso';
|
2017-12-05 21:14:21 +00:00
|
|
|
} else if (!style.usercssData || !Object.keys(style.usercssData.vars || {}).length) {
|
|
|
|
config.style.display = 'none';
|
|
|
|
}
|
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
$('.enable', entry).onclick = handleEvent.toggle;
|
|
|
|
$('.disable', entry).onclick = handleEvent.toggle;
|
|
|
|
$('.delete', entry).onclick = handleEvent.delete;
|
2017-12-05 21:14:21 +00:00
|
|
|
$('.configure', entry).onclick = handleEvent.configure;
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
if (check) detectSloppyRegexps([style]);
|
2017-03-30 23:18:41 +00:00
|
|
|
|
2017-06-27 20:13:11 +00:00
|
|
|
const oldElement = $(ENTRY_ID_PREFIX + style.id);
|
2018-10-12 21:47:08 +00:00
|
|
|
if (oldElement && oldElement.contains(document.activeElement)) {
|
|
|
|
// preserve the focused element inside
|
|
|
|
const {className} = document.activeElement;
|
|
|
|
oldElement.parentNode.replaceChild(entry, oldElement);
|
|
|
|
// we're not using $() since className may contain multiple tokens
|
|
|
|
const el = entry.getElementsByClassName(className)[0];
|
|
|
|
if (el) el.focus();
|
|
|
|
} else if (oldElement) {
|
2017-03-30 23:18:41 +00:00
|
|
|
oldElement.parentNode.replaceChild(entry, oldElement);
|
|
|
|
} else {
|
|
|
|
container.appendChild(entry);
|
|
|
|
}
|
2012-08-20 01:14:33 +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) {
|
2018-10-12 20:31:59 +00:00
|
|
|
if (prefs.get('popupclick') === 1) {
|
|
|
|
this.checkbox.dispatchEvent(new MouseEvent('click'));
|
|
|
|
} else {
|
|
|
|
const entry = handleEvent.getClickedStyleElement(event);
|
|
|
|
$('.style-edit-link', entry).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-01-01 17:02:49 +00:00
|
|
|
API.saveStyle({
|
2017-04-09 18:33:17 +00:00
|
|
|
id: handleEvent.getClickedStyleId(event),
|
2018-01-01 17:02:49 +00:00
|
|
|
enabled: this.matches('.enable') || this.checked,
|
2017-03-25 03:18:14 +00:00
|
|
|
});
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-25 03:18:14 +00:00
|
|
|
|
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');
|
|
|
|
box.dataset.display = true;
|
|
|
|
box.style.cssText = '';
|
2018-01-01 17:02:49 +00:00
|
|
|
$('b', box).textContent = $('.style-name', entry).textContent;
|
2018-10-12 20:31:59 +00:00
|
|
|
$('button[data-cmd="ok"]', box).focus();
|
|
|
|
$('button[data-cmd="ok"]', box).onclick = () => confirm(true);
|
|
|
|
$('button[data-cmd="cancel"]', box).onclick = () => confirm(false);
|
2018-10-18 20:35:48 +00:00
|
|
|
$('a[data-cmd="ok"]', box).focus();
|
2018-10-13 22:07:52 +00:00
|
|
|
$('a[data-cmd="ok"]', box).onclick = () => confirm(true);
|
|
|
|
$('a[data-cmd="cancel"]', box).onclick = () => confirm(false);
|
2017-03-25 19:50:37 +00:00
|
|
|
window.onkeydown = event => {
|
2017-04-18 18:56:32 +00:00
|
|
|
const keyCode = event.keyCode || event.which;
|
2017-03-25 19:50:37 +00:00
|
|
|
if (!event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey
|
2017-07-16 18:02:00 +00:00
|
|
|
&& (keyCode === 13 || keyCode === 27)) {
|
2017-03-25 19:50:37 +00:00
|
|
|
event.preventDefault();
|
2017-07-16 18:02:00 +00:00
|
|
|
confirm(keyCode === 13);
|
2017-03-25 03:18:14 +00:00
|
|
|
}
|
2017-03-25 19:50:37 +00:00
|
|
|
};
|
|
|
|
function confirm(ok) {
|
|
|
|
window.onkeydown = null;
|
2017-08-31 10:23:12 +00:00
|
|
|
animateElement(box, {
|
|
|
|
className: 'lights-on',
|
|
|
|
onComplete: () => (box.dataset.display = false),
|
|
|
|
});
|
2018-01-01 17:02:49 +00:00
|
|
|
if (ok) API.deleteStyle({id});
|
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-01-01 17:02:49 +00:00
|
|
|
API.getStyles({id: styleId}).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);
|
2017-03-30 23:18:41 +00:00
|
|
|
const info = 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
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
openLink(event) {
|
2017-11-25 13:24:07 +00:00
|
|
|
if (!chrome.windows || !prefs.get('openEditInWindow', false)) {
|
2017-04-03 04:13:10 +00:00
|
|
|
handleEvent.openURLandHide.call(this, event);
|
|
|
|
return;
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
event.preventDefault();
|
2017-04-03 04:13:10 +00:00
|
|
|
chrome.windows.create(
|
|
|
|
Object.assign({
|
|
|
|
url: this.href
|
|
|
|
}, prefs.get('windowPosition', {}))
|
|
|
|
);
|
|
|
|
close();
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
if (event.target.matches('.entry, .style-name, .style-edit-link')) {
|
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
|
|
|
|
if (event.target.closest('a')) {
|
|
|
|
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({
|
2018-10-12 20:31:59 +00:00
|
|
|
url: this.hasAttribute('data-href') ? this.dataset.href : this.href,
|
2017-12-10 04:56:41 +00:00
|
|
|
index: activeTab.index + 1,
|
2018-03-13 15:35:28 +00:00
|
|
|
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();
|
|
|
|
if (!this.eventHandled) {
|
|
|
|
this.eventHandled = true;
|
|
|
|
this.dataset.href += event.shiftKey || event.button === 2 ?
|
|
|
|
'?url=' + encodeURIComponent(tabURL) : '';
|
|
|
|
handleEvent.openURLandHide.call(this, event);
|
|
|
|
}
|
|
|
|
},
|
2017-04-03 04:13:10 +00:00
|
|
|
});
|
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 handleUpdate(style) {
|
2017-06-27 20:13:11 +00:00
|
|
|
if ($(ENTRY_ID_PREFIX + style.id)) {
|
2018-01-01 17:02:49 +00:00
|
|
|
createStyleElement({style, check: true});
|
2017-03-30 23:18:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
if (!tabURL) return;
|
2017-03-30 23:18:41 +00:00
|
|
|
// Add an entry when a new style for the current url is installed
|
2018-01-01 17:02:49 +00:00
|
|
|
API.getStyles({
|
|
|
|
matchUrl: tabURL,
|
|
|
|
stopOnFirst: true,
|
|
|
|
omitCode: true,
|
|
|
|
}).then(([style]) => {
|
|
|
|
if (style) {
|
|
|
|
document.body.classList.remove('blocked');
|
|
|
|
$$.remove('.blocked-info, #no-styles');
|
|
|
|
createStyleElement({style, check: true});
|
|
|
|
}
|
|
|
|
});
|
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) {
|
2017-12-02 16:54:54 +00:00
|
|
|
$.remove(ENTRY_ID_PREFIX + id);
|
2017-12-12 14:09:42 +00:00
|
|
|
if (!$('.entry')) {
|
2017-12-12 02:21:06 +00:00
|
|
|
installed.appendChild(template.noStyles.cloneNode(true));
|
|
|
|
}
|
2018-10-12 20:31:59 +00:00
|
|
|
|
|
|
|
var reverseZebra = $('.entry:last-child:nth-of-type(odd)') !== null;
|
2018-10-13 22:07:52 +00:00
|
|
|
installed.classList.toggle('reverse-stripe', reverseZebra);
|
2017-03-30 23:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
function detectSloppyRegexps(styles) {
|
|
|
|
API.detectSloppyRegexps({
|
|
|
|
matchUrl: tabURL,
|
|
|
|
ids: styles.map(({id}) => id),
|
|
|
|
}).then(results => {
|
|
|
|
for (const {id, applied, skipped, hasInvalidRegexps} of results) {
|
|
|
|
const entry = $(ENTRY_ID_PREFIX + id);
|
|
|
|
if (!entry) continue;
|
|
|
|
if (!applied) {
|
|
|
|
entry.classList.add('not-applied');
|
|
|
|
$('.style-name', entry).title = t('styleNotAppliedRegexpProblemTooltip');
|
|
|
|
}
|
|
|
|
if (skipped || hasInvalidRegexps) {
|
|
|
|
entry.classList.toggle('regexp-partial', Boolean(skipped));
|
|
|
|
entry.classList.toggle('regexp-invalid', Boolean(hasInvalidRegexps));
|
|
|
|
const indicator = template.regexpProblemIndicator.cloneNode(true);
|
|
|
|
indicator.appendChild(document.createTextNode(entry.skipped || '!'));
|
|
|
|
indicator.onclick = handleEvent.indicator;
|
|
|
|
$('.main-controls', entry).appendChild(indicator);
|
2017-06-25 10:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
});
|
2015-03-24 14:07:59 +00:00
|
|
|
}
|
2017-08-18 11:41:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
function getTabRealURLFirefox(tab) {
|
|
|
|
// wait for FF tab-on-demand to get a real URL (initially about:blank), 5 sec max
|
|
|
|
return new Promise(resolve => {
|
|
|
|
function onNavigation({tabId, url, frameId}) {
|
|
|
|
if (tabId === tab.id && frameId === 0) {
|
|
|
|
detach();
|
|
|
|
resolve(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function detach(timedOut) {
|
|
|
|
if (timedOut) {
|
|
|
|
resolve(tab.url);
|
|
|
|
} else {
|
|
|
|
debounce.unregister(detach);
|
|
|
|
}
|
|
|
|
chrome.webNavigation.onBeforeNavigate.removeListener(onNavigation);
|
|
|
|
chrome.webNavigation.onCommitted.removeListener(onNavigation);
|
|
|
|
chrome.tabs.onRemoved.removeListener(detach);
|
|
|
|
chrome.tabs.onReplaced.removeListener(detach);
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.webNavigation.onBeforeNavigate.addListener(onNavigation);
|
|
|
|
chrome.webNavigation.onCommitted.addListener(onNavigation);
|
|
|
|
chrome.tabs.onRemoved.addListener(detach);
|
|
|
|
chrome.tabs.onReplaced.addListener(detach);
|
|
|
|
debounce(detach, 5000, {timedOut: true});
|
|
|
|
});
|
|
|
|
}
|