2018-11-07 06:09:29 +00:00
|
|
|
/* global CodeMirror semverCompare closeCurrentTab messageBox download
|
2020-02-23 15:43:26 +00:00
|
|
|
$ $$ $create $createLink t prefs API */
|
2017-09-24 03:39:04 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-10-29 17:22:10 +00:00
|
|
|
(() => {
|
2020-10-13 18:14:54 +00:00
|
|
|
const params = new URLSearchParams(location.search);
|
2020-02-23 15:43:26 +00:00
|
|
|
const tabId = params.has('tabId') ? Number(params.get('tabId')) : -1;
|
|
|
|
const initialUrl = params.get('updateUrl');
|
|
|
|
|
2018-01-04 10:36:27 +00:00
|
|
|
let installed = null;
|
|
|
|
let installedDup = null;
|
2017-09-24 03:39:04 +00:00
|
|
|
|
2020-02-23 15:43:26 +00:00
|
|
|
const liveReload = initLiveReload();
|
|
|
|
liveReload.ready.then(initSourceCode, error => messageBox.alert(error, 'pre'));
|
2017-09-24 03:39:04 +00:00
|
|
|
|
2018-07-22 14:46:42 +00:00
|
|
|
const theme = prefs.get('editor.theme');
|
2018-01-04 10:36:56 +00:00
|
|
|
const cm = CodeMirror($('.main'), {
|
|
|
|
readOnly: true,
|
|
|
|
colorpicker: true,
|
2018-07-22 14:46:42 +00:00
|
|
|
theme,
|
2018-01-04 10:36:56 +00:00
|
|
|
});
|
2018-07-22 14:46:42 +00:00
|
|
|
if (theme !== 'default') {
|
|
|
|
document.head.appendChild($create('link', {
|
|
|
|
rel: 'stylesheet',
|
|
|
|
href: `vendor/codemirror/theme/${theme}.css`
|
|
|
|
}));
|
|
|
|
}
|
2017-11-22 10:19:27 +00:00
|
|
|
window.addEventListener('resize', adjustCodeHeight);
|
2020-02-23 15:43:26 +00:00
|
|
|
// "History back" in Firefox (for now) restores the old DOM including the messagebox,
|
|
|
|
// which stays after installing since we don't want to wait for the fadeout animation before resolving.
|
|
|
|
document.addEventListener('visibilitychange', () => {
|
|
|
|
if (messageBox.element) messageBox.element.remove();
|
|
|
|
if (installed) liveReload.onToggled();
|
|
|
|
});
|
2017-09-25 12:01:50 +00:00
|
|
|
|
2017-11-24 17:27:09 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
if (!installed) {
|
2018-07-16 16:37:01 +00:00
|
|
|
$('#header').appendChild($create('.lds-spinner',
|
2017-12-03 21:12:09 +00:00
|
|
|
new Array(12).fill($create('div')).map(e => e.cloneNode())));
|
2017-11-24 17:27:09 +00:00
|
|
|
}
|
|
|
|
}, 200);
|
|
|
|
|
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;
|
|
|
|
|
2017-10-07 10:04:37 +00:00
|
|
|
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 =>
|
2017-12-03 21:12:09 +00:00
|
|
|
$('.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');
|
2017-12-02 16:54:54 +00:00
|
|
|
setTimeout(() => $.remove('.lds-spinner'), 1000);
|
2017-11-24 17:27:09 +00:00
|
|
|
|
2017-11-22 10:19:27 +00:00
|
|
|
showError('');
|
|
|
|
requestAnimationFrame(adjustCodeHeight);
|
|
|
|
|
2017-10-12 08:10:17 +00:00
|
|
|
function makeAuthor(text) {
|
2018-07-12 21:25:47 +00:00
|
|
|
const match = text.match(/^(.+?)(?:\s+<(.+?)>)?(?:\s+\((.+?)\))?$/);
|
2017-10-12 08:10:17 +00:00
|
|
|
if (!match) {
|
|
|
|
return document.createTextNode(text);
|
|
|
|
}
|
|
|
|
const [, name, email, url] = match;
|
|
|
|
const frag = document.createDocumentFragment();
|
|
|
|
if (email) {
|
2017-12-03 21:12:09 +00:00
|
|
|
frag.appendChild($createLink(`mailto:${email}`, name));
|
2017-10-12 08:10:17 +00:00
|
|
|
} else {
|
2017-12-03 21:12:09 +00:00
|
|
|
frag.appendChild($create('span', name));
|
2017-10-12 08:10:17 +00:00
|
|
|
}
|
|
|
|
if (url) {
|
2017-12-03 21:12:09 +00:00
|
|
|
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-12-03 21:12:09 +00:00
|
|
|
}))
|
2017-10-12 08:15:45 +00:00
|
|
|
));
|
2017-10-12 08:10:17 +00:00
|
|
|
}
|
|
|
|
return frag;
|
|
|
|
}
|
|
|
|
|
2017-09-25 12:01:50 +00:00
|
|
|
function makeExternalLink() {
|
2017-12-03 21:12:09 +00:00
|
|
|
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) {
|
2017-11-22 10:19:27 +00:00
|
|
|
$('.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) {
|
2017-11-24 15:26:20 +00:00
|
|
|
installed = style;
|
2017-09-25 12:01:50 +00:00
|
|
|
|
2017-12-02 16:54:54 +00:00
|
|
|
$$.remove('.warning');
|
2017-12-01 23:43:01 +00:00
|
|
|
$('button.install').disabled = true;
|
|
|
|
$('button.install').classList.add('installed');
|
2020-02-23 15:43:26 +00:00
|
|
|
$('#live-reload-install-hint').classList.toggle('hidden', !liveReload.enabled);
|
2017-12-01 23:43:01 +00:00
|
|
|
$('h2.installed').classList.add('active');
|
2017-11-24 15:26:20 +00:00
|
|
|
$('.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
|
|
|
|
2017-11-24 15:26:20 +00:00
|
|
|
updateMeta(style);
|
2017-09-25 12:01:50 +00:00
|
|
|
|
2020-02-23 15:43:26 +00:00
|
|
|
if (!liveReload.enabled && !prefs.get('openEditInWindow')) {
|
|
|
|
location.href = '/edit.html?id=' + style.id;
|
2017-11-28 21:05:52 +00:00
|
|
|
} else {
|
2018-01-01 17:02:49 +00:00
|
|
|
API.openEditor({id: style.id});
|
2020-02-23 15:43:26 +00:00
|
|
|
if (!liveReload.enabled) {
|
|
|
|
if (tabId < 0 && history.length > 1) {
|
|
|
|
history.back();
|
|
|
|
} else {
|
|
|
|
closeCurrentTab();
|
|
|
|
}
|
2017-11-28 21:05:52 +00:00
|
|
|
}
|
2017-11-24 15:26:20 +00:00
|
|
|
}
|
2017-09-24 03:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function initSourceCode(sourceCode) {
|
|
|
|
cm.setValue(sourceCode);
|
2017-11-22 10:19:27 +00:00
|
|
|
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)
|
2017-11-24 15:26:20 +00:00
|
|
|
.catch(err => {
|
2018-07-16 16:37:01 +00:00
|
|
|
$('#header').classList.add('meta-init-error');
|
2018-11-30 01:35:21 +00:00
|
|
|
console.error(err);
|
2017-11-24 15:26:20 +00:00
|
|
|
showError(err);
|
|
|
|
});
|
2017-09-24 03:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function buildWarning(err) {
|
2017-12-03 20:29:36 +00:00
|
|
|
const contents = Array.isArray(err) ?
|
2018-01-01 17:02:49 +00:00
|
|
|
[$create('pre', err.join('\n'))] :
|
2018-07-04 16:25:03 +00:00
|
|
|
[err && err.message && $create('pre', err.message) || err || 'Unknown error'];
|
2018-11-07 06:09:29 +00:00
|
|
|
if (Number.isInteger(err.index) && typeof contents[0] === 'string') {
|
2017-12-03 20:29:36 +00:00
|
|
|
const pos = cm.posFromIndex(err.index);
|
|
|
|
contents[0] = `${pos.line + 1}:${pos.ch + 1} ` + contents[0];
|
2017-12-03 21:12:09 +00:00
|
|
|
contents.push($create('pre', drawLinePointer(pos)));
|
2017-12-03 20:29:36 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
cm.scrollIntoView({line: pos.line + 1, ch: pos.ch}, window.innerHeight / 4);
|
|
|
|
cm.setCursor(pos.line, pos.ch + 1);
|
|
|
|
cm.focus();
|
|
|
|
});
|
|
|
|
}
|
2017-12-03 21:12:09 +00:00
|
|
|
return $create('.warning', [
|
2017-09-24 03:39:04 +00:00
|
|
|
t('parseUsercssError'),
|
2017-12-03 20:29:36 +00:00
|
|
|
'\n',
|
|
|
|
...contents,
|
2017-12-03 21:12:09 +00:00
|
|
|
]);
|
2017-09-24 03:39:04 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 20:29:36 +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(
|
2017-12-03 21:12:09 +00:00
|
|
|
$create('.warning', t('versionInvalidOlder')),
|
2017-09-24 03:39:04 +00:00
|
|
|
$('.actions')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$('button.install').onclick = () => {
|
2017-11-24 16:40:18 +00:00
|
|
|
(!dup ?
|
|
|
|
Promise.resolve(true) :
|
|
|
|
messageBox.confirm(t('styleInstallOverwrite', [
|
|
|
|
data.name,
|
|
|
|
dupData.version,
|
|
|
|
data.version,
|
|
|
|
]))
|
2017-11-24 15:26:20 +00:00
|
|
|
).then(ok => ok &&
|
2018-11-07 06:09:29 +00:00
|
|
|
API.installUsercss(style)
|
2018-01-05 10:26:11 +00:00
|
|
|
.then(install)
|
2018-03-11 13:31:25 +00:00
|
|
|
.catch(err => messageBox.alert(t('styleInstallFailed', err), 'pre'))
|
2017-12-03 20:29:36 +00:00
|
|
|
);
|
2017-09-24 03:39:04 +00:00
|
|
|
};
|
|
|
|
|
2017-09-24 08:54:21 +00:00
|
|
|
// set updateUrl
|
2017-12-06 19:35:19 +00:00
|
|
|
const checker = $('.set-update-url input[type=checkbox]');
|
2020-02-23 15:43:26 +00:00
|
|
|
const updateUrl = new URL(style.updateUrl || initialUrl);
|
2017-09-24 03:39:04 +00:00
|
|
|
if (dup && dup.updateUrl === updateUrl.href) {
|
2017-12-06 19:35:19 +00:00
|
|
|
checker.checked = true;
|
2017-09-24 03:39:04 +00:00
|
|
|
// there is no way to "unset" updateUrl, you can only overwrite it.
|
2017-12-06 19:35:19 +00:00
|
|
|
checker.disabled = true;
|
2017-10-11 13:45:17 +00:00
|
|
|
} else if (updateUrl.protocol !== 'file:') {
|
2017-12-06 19:35:19 +00:00
|
|
|
checker.checked = true;
|
2017-09-24 03:39:04 +00:00
|
|
|
style.updateUrl = updateUrl.href;
|
|
|
|
}
|
2017-12-06 19:35:19 +00:00
|
|
|
checker.onchange = () => {
|
|
|
|
style.updateUrl = checker.checked ? updateUrl.href : null;
|
2017-09-24 03:39:04 +00:00
|
|
|
};
|
2017-12-06 19:35:19 +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
|
|
|
|
2020-02-23 15:43:26 +00:00
|
|
|
if (initialUrl.startsWith('file:')) {
|
|
|
|
$('.live-reload input').onchange = liveReload.onToggled;
|
2017-09-24 03:39:04 +00:00
|
|
|
} else {
|
2020-02-23 15:43:26 +00:00
|
|
|
$('.live-reload').remove();
|
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;
|
|
|
|
}
|
2017-11-22 10:19:27 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2017-11-24 16:33:50 +00:00
|
|
|
|
2020-02-23 15:43:26 +00:00
|
|
|
function initLiveReload() {
|
|
|
|
const DELAY = 500;
|
|
|
|
let isEnabled = false;
|
|
|
|
let timer = 0;
|
|
|
|
/** @type function(?options):Promise<string|null> */
|
|
|
|
let getData = null;
|
|
|
|
/** @type Promise */
|
|
|
|
let sequence = null;
|
|
|
|
if (tabId < 0) {
|
|
|
|
getData = DirectDownloader();
|
2020-10-06 10:49:43 +00:00
|
|
|
sequence = API.getUsercssInstallCode(initialUrl)
|
|
|
|
.then(code => code || getData())
|
|
|
|
.catch(getData);
|
2020-02-23 15:43:26 +00:00
|
|
|
} else {
|
|
|
|
getData = PortDownloader();
|
|
|
|
sequence = getData({timer: false});
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
get enabled() {
|
|
|
|
return isEnabled;
|
|
|
|
},
|
|
|
|
ready: sequence,
|
|
|
|
onToggled(e) {
|
|
|
|
if (e) isEnabled = e.target.checked;
|
|
|
|
if (installed || installedDup) {
|
2020-10-09 11:37:29 +00:00
|
|
|
if (isEnabled) {
|
|
|
|
check({force: true});
|
|
|
|
} else {
|
|
|
|
stop();
|
|
|
|
}
|
2020-02-23 15:43:26 +00:00
|
|
|
$('.install').disabled = isEnabled;
|
|
|
|
Object.assign($('#live-reload-install-hint'), {
|
|
|
|
hidden: !isEnabled,
|
|
|
|
textContent: t(`liveReloadInstallHint${tabId >= 0 ? 'FF' : ''}`),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2020-10-09 11:37:29 +00:00
|
|
|
function check(opts) {
|
|
|
|
getData(opts)
|
2020-02-23 15:43:26 +00:00
|
|
|
.then(update, logError)
|
|
|
|
.then(() => {
|
|
|
|
timer = 0;
|
|
|
|
start();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function logError(error) {
|
|
|
|
console.warn(t('liveReloadError', error));
|
|
|
|
}
|
|
|
|
function start() {
|
|
|
|
timer = timer || setTimeout(check, DELAY);
|
|
|
|
}
|
|
|
|
function stop() {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = 0;
|
|
|
|
}
|
|
|
|
function update(code) {
|
|
|
|
if (code == null) return;
|
|
|
|
sequence = sequence.catch(console.error).then(() => {
|
|
|
|
const {id} = installed || installedDup;
|
|
|
|
const scrollInfo = cm.getScrollInfo();
|
|
|
|
const cursor = cm.getCursor();
|
|
|
|
cm.setValue(code);
|
|
|
|
cm.setCursor(cursor);
|
|
|
|
cm.scrollTo(scrollInfo.left, scrollInfo.top);
|
|
|
|
return API.installUsercss({id, sourceCode: code})
|
|
|
|
.then(updateMeta)
|
|
|
|
.catch(showError);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function DirectDownloader() {
|
|
|
|
let oldCode = null;
|
|
|
|
const passChangedCode = code => {
|
|
|
|
const isSame = code === oldCode;
|
|
|
|
oldCode = code;
|
|
|
|
return isSame ? null : code;
|
|
|
|
};
|
|
|
|
return () => download(initialUrl).then(passChangedCode);
|
|
|
|
}
|
|
|
|
function PortDownloader() {
|
|
|
|
const resolvers = new Map();
|
|
|
|
const port = chrome.tabs.connect(tabId, {name: 'downloadSelf'});
|
|
|
|
port.onMessage.addListener(({id, code, error}) => {
|
|
|
|
const r = resolvers.get(id);
|
|
|
|
resolvers.delete(id);
|
|
|
|
if (error) {
|
|
|
|
r.reject(error);
|
|
|
|
} else {
|
|
|
|
r.resolve(code);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
port.onDisconnect.addListener(() => {
|
2020-10-11 11:37:55 +00:00
|
|
|
chrome.tabs.get(tabId, tab =>
|
|
|
|
!chrome.runtime.lastError && tab.url === initialUrl
|
|
|
|
? location.reload()
|
|
|
|
: closeCurrentTab());
|
2020-02-23 15:43:26 +00:00
|
|
|
});
|
2020-10-09 11:37:29 +00:00
|
|
|
return (opts = {}) => new Promise((resolve, reject) => {
|
2020-02-23 15:43:26 +00:00
|
|
|
const id = performance.now();
|
|
|
|
resolvers.set(id, {resolve, reject});
|
2020-10-09 11:37:29 +00:00
|
|
|
opts.id = id;
|
|
|
|
port.postMessage(opts);
|
2020-02-23 15:43:26 +00:00
|
|
|
});
|
|
|
|
}
|
2017-12-31 14:58:35 +00:00
|
|
|
}
|
2017-09-24 03:39:04 +00:00
|
|
|
})();
|