stylus/install-usercss/install-usercss.js

432 lines
14 KiB
JavaScript
Raw Normal View History

2018-10-10 16:54:38 +00:00
/* global CodeMirror semverCompare closeCurrentTab messageBox download
$ $$ $create $createLink t prefs API getTab */
2017-09-24 03:39:04 +00:00
'use strict';
2017-10-29 17:22:10 +00:00
(() => {
const DUMMY_URL = 'foo:';
// TODO: remove .replace(/^\?/, '') when minimum_chrome_version >= 52 (https://crbug.com/601425)
const params = new URLSearchParams(location.search.replace(/^\?/, ''));
2017-09-25 12:01:50 +00:00
let liveReload = false;
2018-01-04 10:36:27 +00:00
let installed = null;
let installedDup = null;
2017-09-24 03:39:04 +00:00
const tabId = Number(params.get('tabId'));
let tabUrl;
let port;
2017-11-25 17:24:15 +00:00
if (params.has('direct')) {
$('.live-reload').textContent = t('liveReloadUnavailable');
$('.live-reload').classList.add('unavailable');
getCodeDirectly();
} else {
port = chrome.tabs.connect(tabId);
port.postMessage({method: 'getSourceCode'});
port.onMessage.addListener(msg => {
switch (msg.method) {
case 'getSourceCodeResponse':
if (msg.error) {
messageBox.alert(msg.error, 'pre');
} else {
initSourceCode(msg.sourceCode);
}
break;
case 'sourceCodeChanged':
if (msg.error) {
messageBox.alert(msg.error, 'pre');
} else {
liveReloadUpdate(msg.sourceCode);
}
break;
}
});
port.onDisconnect.addListener(onPortDisconnected);
}
2017-09-24 03:39:04 +00:00
const theme = prefs.get('editor.theme');
const cm = CodeMirror($('.main'), {
readOnly: true,
colorpicker: true,
theme,
});
if (theme !== 'default') {
document.head.appendChild($create('link', {
rel: 'stylesheet',
href: `vendor/codemirror/theme/${theme}.css`
}));
}
2017-09-25 12:01:50 +00:00
let liveReloadPending = Promise.resolve();
window.addEventListener('resize', adjustCodeHeight);
2017-09-25 12:01:50 +00:00
setTimeout(() => {
if (!installed) {
2018-07-16 16:37:01 +00:00
$('#header').appendChild($create('.lds-spinner',
new Array(12).fill($create('div')).map(e => e.cloneNode())));
}
}, 200);
getTab(tabId).then(tab => (tabUrl = tab.url));
2018-02-15 13:48:00 +00:00
chrome.tabs.onUpdated.addListener((id, {url}) => {
if (id === tabId && url && url !== tabUrl) {
closeCurrentTab();
}
});
// close the tab in case the port didn't report onDisconnect
chrome.tabs.onRemoved.addListener(id => {
if (id === tabId) {
closeCurrentTab();
}
});
2017-09-25 12:01:50 +00:00
function liveReloadUpdate(sourceCode) {
liveReloadPending = liveReloadPending.then(() => {
const scrollInfo = cm.getScrollInfo();
const cursor = cm.getCursor();
cm.setValue(sourceCode);
cm.setCursor(cursor);
cm.scrollTo(scrollInfo.left, scrollInfo.top);
2018-10-07 14:59:31 +00:00
API.installUsercss({
2018-01-04 10:36:27 +00:00
id: (installed || installedDup).id,
2017-09-25 12:01:50 +00:00
sourceCode
2018-10-07 14:59:31 +00:00
}).then(style => {
updateMeta(style);
}).catch(showError);
2017-09-25 12:01:50 +00:00
});
}
2018-01-04 10:36:27 +00:00
function updateMeta(style, dup = installedDup) {
installedDup = dup;
2017-09-25 12:01:50 +00:00
const data = style.usercssData;
const dupData = dup && dup.usercssData;
const versionTest = dup && semverCompare(data.version, dupData.version);
cm.setPreprocessor(data.preprocessor);
2018-01-04 10:36:27 +00:00
const installButtonLabel = t(
installed ? 'installButtonInstalled' :
!dup ? 'installButton' :
versionTest > 0 ? 'installButtonUpdate' : 'installButtonReinstall'
);
document.title = `${installButtonLabel} ${data.name}`;
$('.install').textContent = installButtonLabel;
$('.install').classList.add(
installed ? 'installed' :
!dup ? 'install' :
versionTest > 0 ? 'update' :
'reinstall');
2017-09-25 12:01:50 +00:00
$('.set-update-url').title = dup && dup.updateUrl && t('installUpdateFrom', dup.updateUrl) || '';
$('.meta-name').textContent = data.name;
$('.meta-version').textContent = data.version;
$('.meta-description').textContent = data.description;
if (data.author) {
$('.meta-author').parentNode.style.display = '';
$('.meta-author').textContent = '';
$('.meta-author').appendChild(makeAuthor(data.author));
} else {
$('.meta-author').parentNode.style.display = 'none';
}
2017-09-25 12:01:50 +00:00
$('.meta-license').parentNode.style.display = data.license ? '' : 'none';
$('.meta-license').textContent = data.license;
$('.applies-to').textContent = '';
getAppliesTo(style).forEach(pattern =>
$('.applies-to').appendChild($create('li', pattern)));
2017-09-25 12:01:50 +00:00
$('.external-link').textContent = '';
const externalLink = makeExternalLink();
if (externalLink) {
$('.external-link').appendChild(externalLink);
}
2018-07-16 16:37:01 +00:00
$('#header').classList.add('meta-init');
$('#header').classList.remove('meta-init-error');
setTimeout(() => $.remove('.lds-spinner'), 1000);
showError('');
requestAnimationFrame(adjustCodeHeight);
function makeAuthor(text) {
2018-07-12 21:25:47 +00:00
const match = text.match(/^(.+?)(?:\s+<(.+?)>)?(?:\s+\((.+?)\))?$/);
if (!match) {
return document.createTextNode(text);
}
const [, name, email, url] = match;
const frag = document.createDocumentFragment();
if (email) {
frag.appendChild($createLink(`mailto:${email}`, name));
} else {
frag.appendChild($create('span', name));
}
if (url) {
frag.appendChild($createLink(url,
$create('SVG:svg.svg-icon', {viewBox: '0 0 20 20'},
$create('SVG:path', {
2017-11-09 00:01:45 +00:00
d: 'M4,4h5v2H6v8h8v-3h2v5H4V4z M11,3h6v6l-2-2l-4,4L9,9l4-4L11,3z'
}))
2017-10-12 08:15:45 +00:00
));
}
return frag;
}
2017-09-25 12:01:50 +00:00
function makeExternalLink() {
const urls = [
data.homepageURL && [data.homepageURL, t('externalHomepage')],
data.supportURL && [data.supportURL, t('externalSupport')],
];
return (data.homepageURL || data.supportURL) && (
$create('div', [
$create('h3', t('externalLink')),
$create('ul', urls.map(args => args &&
$create('li',
$createLink(...args)
)
))
]));
2017-09-25 12:01:50 +00:00
}
}
function showError(err) {
$('.warnings').textContent = '';
if (err) {
$('.warnings').appendChild(buildWarning(err));
}
$('.warnings').classList.toggle('visible', Boolean(err));
$('.container').classList.toggle('has-warnings', Boolean(err));
adjustCodeHeight();
2017-09-25 12:01:50 +00:00
}
2017-09-24 03:39:04 +00:00
function install(style) {
installed = style;
2017-09-25 12:01:50 +00:00
$$.remove('.warning');
$('button.install').disabled = true;
$('button.install').classList.add('installed');
$('#live-reload-install-hint').classList.toggle('hidden', !liveReload);
$('h2.installed').classList.add('active');
$('.set-update-url input[type=checkbox]').disabled = true;
$('.set-update-url').title = style.updateUrl ?
t('installUpdateFrom', style.updateUrl) : '';
2017-09-25 12:01:50 +00:00
updateMeta(style);
2017-09-25 12:01:50 +00:00
if (!liveReload && !prefs.get('openEditInWindow')) {
chrome.tabs.update({url: '/edit.html?id=' + style.id});
} else {
2018-01-01 17:02:49 +00:00
API.openEditor({id: style.id});
if (!liveReload) {
closeCurrentTab();
}
}
2017-10-06 08:29:06 +00:00
window.dispatchEvent(new CustomEvent('installed'));
2017-09-24 03:39:04 +00:00
}
function initSourceCode(sourceCode) {
cm.setValue(sourceCode);
cm.refresh();
2018-01-01 17:02:49 +00:00
API.buildUsercss({sourceCode, checkDup: true})
2018-01-04 10:36:27 +00:00
.then(init)
.catch(err => {
2018-07-16 16:37:01 +00:00
$('#header').classList.add('meta-init-error');
showError(err);
});
2017-09-24 03:39:04 +00:00
}
function buildWarning(err) {
const contents = Array.isArray(err) ?
2018-01-01 17:02:49 +00:00
[$create('pre', err.join('\n'))] :
[err && err.message && $create('pre', err.message) || err || 'Unknown error'];
2018-09-25 12:45:07 +00:00
if (Number.isInteger(err.index) && typeof contents[0] === 'string') {
const pos = cm.posFromIndex(err.index);
contents[0] = `${pos.line + 1}:${pos.ch + 1} ` + contents[0];
contents.push($create('pre', drawLinePointer(pos)));
setTimeout(() => {
cm.scrollIntoView({line: pos.line + 1, ch: pos.ch}, window.innerHeight / 4);
cm.setCursor(pos.line, pos.ch + 1);
cm.focus();
});
}
return $create('.warning', [
2017-09-24 03:39:04 +00:00
t('parseUsercssError'),
'\n',
...contents,
]);
2017-09-24 03:39:04 +00:00
}
function drawLinePointer(pos) {
const SIZE = 60;
const line = cm.getLine(pos.line);
const numTabs = pos.ch + 1 - line.slice(0, pos.ch + 1).replace(/\t/g, '').length;
const pointer = ' '.repeat(pos.ch) + '^';
const start = Math.max(Math.min(pos.ch - SIZE / 2, line.length - SIZE), 0);
const end = Math.min(Math.max(pos.ch + SIZE / 2, SIZE), line.length);
const leftPad = start !== 0 ? '...' : '';
const rightPad = end !== line.length ? '...' : '';
return (
leftPad +
line.slice(start, end).replace(/\t/g, ' '.repeat(cm.options.tabSize)) +
rightPad +
'\n' +
' '.repeat(leftPad.length + numTabs * cm.options.tabSize) +
pointer.slice(start, end)
);
}
2017-09-24 03:39:04 +00:00
function init({style, dup}) {
const data = style.usercssData;
const dupData = dup && dup.usercssData;
const versionTest = dup && semverCompare(data.version, dupData.version);
2017-09-25 12:01:50 +00:00
updateMeta(style, dup);
2017-09-24 08:54:21 +00:00
// update UI
2017-09-24 03:39:04 +00:00
if (versionTest < 0) {
$('.actions').parentNode.insertBefore(
$create('.warning', t('versionInvalidOlder')),
2017-09-24 03:39:04 +00:00
$('.actions')
);
}
$('button.install').onclick = () => {
(!dup ?
Promise.resolve(true) :
messageBox.confirm(t('styleInstallOverwrite', [
data.name,
dupData.version,
data.version,
]))
).then(ok => ok &&
2018-10-07 14:59:31 +00:00
API.installUsercss(style)
2018-01-05 10:26:11 +00:00
.then(install)
.catch(err => messageBox.alert(t('styleInstallFailed', err), 'pre'))
);
2017-09-24 03:39:04 +00:00
};
2017-09-24 08:54:21 +00:00
// set updateUrl
const checker = $('.set-update-url input[type=checkbox]');
// prefer the installation URL unless drag'n'dropped on the manage page
const installationUrl = (params.get('updateUrl') || '').replace(/^blob.+/, '');
const updateUrl = new URL(installationUrl || style.updateUrl || DUMMY_URL);
$('.set-update-url > span').textContent = t('installUpdateFromLabel');
2017-09-24 03:39:04 +00:00
if (dup && dup.updateUrl === updateUrl.href) {
checker.checked = true;
2017-09-24 03:39:04 +00:00
// there is no way to "unset" updateUrl, you can only overwrite it.
checker.disabled = true;
} else if (updateUrl.href === DUMMY_URL) {
// drag'n'dropped on the manage page and the style doesn't have @updateURL
$('.set-update-url').textContent = t('installUpdateUnavailable');
$('.set-update-url').classList.add('unavailable');
return;
2017-10-11 13:45:17 +00:00
} else if (updateUrl.protocol !== 'file:') {
checker.checked = true;
2017-09-24 03:39:04 +00:00
style.updateUrl = updateUrl.href;
}
checker.onchange = () => {
style.updateUrl = checker.checked ? updateUrl.href : null;
2017-09-24 03:39:04 +00:00
};
checker.onchange();
$('.set-update-url p').textContent = updateUrl.href.length < 300 ? updateUrl.href :
updateUrl.href.slice(0, 300) + '...';
2017-09-24 03:39:04 +00:00
if (!port) {
return;
}
2017-09-25 12:01:50 +00:00
// live reload
const setLiveReload = $('.live-reload input[type=checkbox]');
if (updateUrl.protocol !== 'file:') {
setLiveReload.parentNode.remove();
2017-09-24 03:39:04 +00:00
} else {
2017-09-25 12:01:50 +00:00
setLiveReload.addEventListener('change', () => {
liveReload = setLiveReload.checked;
2018-01-04 10:36:27 +00:00
if (installed || installedDup) {
const method = 'liveReload' + (liveReload ? 'Start' : 'Stop');
port.postMessage({method});
2018-01-04 10:36:27 +00:00
$('.install').disabled = liveReload;
$('#live-reload-install-hint').classList.toggle('hidden', !liveReload);
}
});
window.addEventListener('installed', () => {
if (liveReload) {
port.postMessage({method: 'liveReloadStart'});
}
2017-09-25 12:01:50 +00:00
});
2017-09-24 03:39:04 +00:00
}
}
function getAppliesTo(style) {
function *_gen() {
for (const section of style.sections) {
for (const type of ['urls', 'urlPrefixes', 'domains', 'regexps']) {
if (section[type]) {
yield *section[type];
}
}
}
}
const result = [..._gen()];
if (!result.length) {
result.push(chrome.i18n.getMessage('appliesToEverything'));
}
return result;
}
function adjustCodeHeight() {
// Chrome-only bug (apparently): it doesn't limit the scroller element height
const scroller = cm.display.scroller;
const prevWindowHeight = adjustCodeHeight.prevWindowHeight;
if (scroller.scrollHeight === scroller.clientHeight ||
prevWindowHeight && window.innerHeight !== prevWindowHeight) {
adjustCodeHeight.prevWindowHeight = window.innerHeight;
cm.setSize(null, $('.main').offsetHeight - $('.warnings').offsetHeight);
}
}
function getCodeDirectly() {
// FF applies page CSP even to content scripts, https://bugzil.la/1267027
// To circumvent that, the bg process downloads the code directly
2017-11-25 17:24:15 +00:00
const key = 'tempUsercssCode' + tabId;
chrome.storage.local.get(key, data => {
const code = data && data[key];
// bg already downloaded the code
if (typeof code === 'string') {
initSourceCode(code);
chrome.storage.local.remove(key);
return;
}
// bg still downloads the code
if (code && code.loading) {
const waitForCodeInStorage = (changes, area) => {
if (area === 'local' && key in changes) {
initSourceCode(changes[key].newValue);
chrome.storage.onChanged.removeListener(waitForCodeInStorage);
chrome.storage.local.remove(key);
}
};
chrome.storage.onChanged.addListener(waitForCodeInStorage);
return;
}
// on the off-chance dbExecChromeStorage.getAll ran right after bg download was saved
download(params.get('updateUrl'))
.then(initSourceCode)
.catch(err => messageBox.alert(t('styleInstallFailed', String(err)), 'pre'));
});
}
function onPortDisconnected() {
chrome.tabs.get(tabId, tab => {
if (chrome.runtime.lastError) {
closeCurrentTab();
} else if (tab.url === tabUrl) {
location.reload();
}
});
}
2017-09-24 03:39:04 +00:00
})();