2018-11-07 06:09:29 +00:00
|
|
|
/* global cloneInto msg API */
|
2017-07-12 18:17:04 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-10-13 18:14:54 +00:00
|
|
|
// eslint-disable-next-line no-unused-expressions
|
|
|
|
/^\/styles\/(\d+)(\/([^/]*))?([?#].*)?$/.test(location.pathname) && (() => {
|
|
|
|
const styleId = RegExp.$1;
|
|
|
|
const pageEventId = `${performance.now()}${Math.random()}`;
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
window.dispatchEvent(new CustomEvent(chrome.runtime.id + '-install'));
|
|
|
|
window.addEventListener(chrome.runtime.id + '-install', orphanCheck, true);
|
|
|
|
|
2018-04-30 16:31:43 +00:00
|
|
|
document.addEventListener('stylishInstallChrome', onClick);
|
|
|
|
document.addEventListener('stylishUpdateChrome', onClick);
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
msg.on(onMessage);
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2018-07-03 22:16:18 +00:00
|
|
|
onDOMready().then(() => {
|
2018-04-30 16:31:43 +00:00
|
|
|
window.postMessage({
|
|
|
|
direction: 'from-content-script',
|
|
|
|
message: 'StylishInstalled',
|
|
|
|
}, '*');
|
|
|
|
});
|
|
|
|
|
2018-11-18 13:30:47 +00:00
|
|
|
let currentMd5;
|
2020-10-13 18:14:54 +00:00
|
|
|
const md5Url = getMeta('stylish-md5-url') || `https://update.userstyles.org/${styleId}.md5`;
|
|
|
|
Promise.all([
|
|
|
|
API.findStyle({md5Url}),
|
|
|
|
getResource(md5Url),
|
|
|
|
onDOMready(),
|
|
|
|
]).then(checkUpdatability);
|
|
|
|
|
|
|
|
document.documentElement.appendChild(
|
|
|
|
Object.assign(document.createElement('script'), {
|
|
|
|
textContent: `(${inPageContext})('${pageEventId}')`,
|
|
|
|
}));
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function onMessage(msg) {
|
2018-01-10 13:44:29 +00:00
|
|
|
switch (msg.method) {
|
|
|
|
case 'ping':
|
|
|
|
// orphaned content script check
|
2018-11-07 06:09:29 +00:00
|
|
|
return true;
|
2018-01-10 13:44:29 +00:00
|
|
|
case 'openSettings':
|
|
|
|
openSettings();
|
2018-11-07 06:09:29 +00:00
|
|
|
return true;
|
2018-01-10 13:44:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
/* since we are using "stylish-code-chrome" meta key on all browsers and
|
|
|
|
US.o does not provide "advanced settings" on this url if browser is not Chrome,
|
|
|
|
we need to fix this URL using "stylish-update-url" meta key
|
|
|
|
*/
|
|
|
|
function getStyleURL() {
|
|
|
|
const textUrl = getMeta('stylish-update-url') || '';
|
|
|
|
const jsonUrl = getMeta('stylish-code-chrome') ||
|
|
|
|
textUrl.replace(/styles\/(\d+)\/[^?]*/, 'styles/chrome/$1.json');
|
|
|
|
const paramsMissing = !jsonUrl.includes('?') && textUrl.includes('?');
|
|
|
|
return jsonUrl + (paramsMissing ? textUrl.replace(/^[^?]+/, '') : '');
|
|
|
|
}
|
|
|
|
|
2018-11-18 13:30:47 +00:00
|
|
|
function checkUpdatability([installedStyle, md5]) {
|
2017-11-25 21:56:57 +00:00
|
|
|
// TODO: remove the following statement when USO is fixed
|
2020-10-13 18:14:54 +00:00
|
|
|
document.dispatchEvent(new CustomEvent(pageEventId, {
|
2017-11-25 21:56:57 +00:00
|
|
|
detail: installedStyle && installedStyle.updateUrl,
|
|
|
|
}));
|
2018-11-18 13:30:47 +00:00
|
|
|
currentMd5 = md5;
|
2017-11-25 21:56:57 +00:00
|
|
|
if (!installedStyle) {
|
2018-02-27 18:30:29 +00:00
|
|
|
sendEvent({type: 'styleCanBeInstalledChrome'});
|
2017-11-25 21:56:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-30 16:31:43 +00:00
|
|
|
const isCustomizable = /\?/.test(installedStyle.updateUrl);
|
2017-11-25 21:56:57 +00:00
|
|
|
const md5Url = getMeta('stylish-md5-url');
|
|
|
|
if (md5Url && installedStyle.md5Url && installedStyle.originalMd5) {
|
2018-11-18 13:30:47 +00:00
|
|
|
reportUpdatable(isCustomizable || md5 !== installedStyle.originalMd5);
|
2017-11-25 21:56:57 +00:00
|
|
|
} else {
|
|
|
|
getStyleJson().then(json => {
|
2018-04-30 16:31:43 +00:00
|
|
|
reportUpdatable(
|
|
|
|
isCustomizable ||
|
|
|
|
!json ||
|
2017-11-25 21:56:57 +00:00
|
|
|
!styleSectionsEqual(json, installedStyle));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-01 09:12:06 +00:00
|
|
|
function prepareInstallButton() {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const observer = new MutationObserver(check);
|
|
|
|
observer.observe(document.documentElement, {
|
|
|
|
childList: true,
|
2020-11-18 11:17:15 +00:00
|
|
|
subtree: true,
|
2019-06-01 09:12:06 +00:00
|
|
|
});
|
|
|
|
check();
|
|
|
|
|
|
|
|
function check() {
|
|
|
|
if (document.querySelector('#install_style_button')) {
|
|
|
|
resolve();
|
|
|
|
observer.disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function reportUpdatable(isUpdatable) {
|
2019-06-01 09:12:06 +00:00
|
|
|
prepareInstallButton().then(() => {
|
2018-11-18 13:30:47 +00:00
|
|
|
sendEvent({
|
|
|
|
type: isUpdatable
|
|
|
|
? 'styleCanBeUpdatedChrome'
|
|
|
|
: 'styleAlreadyInstalledChrome',
|
|
|
|
detail: {
|
2020-11-18 11:17:15 +00:00
|
|
|
updateUrl: installedStyle.updateUrl,
|
2018-11-18 13:30:47 +00:00
|
|
|
},
|
|
|
|
});
|
2019-06-01 09:12:06 +00:00
|
|
|
});
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-12 18:17:04 +00:00
|
|
|
|
|
|
|
|
2018-02-27 18:30:29 +00:00
|
|
|
function sendEvent(event) {
|
|
|
|
sendEvent.lastEvent = event;
|
|
|
|
let {type, detail = null} = event;
|
2017-11-25 21:56:57 +00:00
|
|
|
if (typeof cloneInto !== 'undefined') {
|
|
|
|
// Firefox requires explicit cloning, however USO can't process our messages anyway
|
|
|
|
// because USO tries to use a global "event" variable deprecated in Firefox
|
2018-02-27 18:30:29 +00:00
|
|
|
detail = cloneInto({detail}, document);
|
|
|
|
} else {
|
|
|
|
detail = {detail};
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
onDOMready().then(() => {
|
|
|
|
document.dispatchEvent(new CustomEvent(type, detail));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onClick(event) {
|
|
|
|
if (onClick.processing || !orphanCheck()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onClick.processing = true;
|
2019-06-01 09:12:06 +00:00
|
|
|
doInstall()
|
|
|
|
.then(() => {
|
|
|
|
if (!event.type.includes('Update')) {
|
|
|
|
// FIXME: sometimes the button is broken i.e. the button sends
|
|
|
|
// 'install' instead of 'update' event while the style is already
|
|
|
|
// install.
|
|
|
|
// This triggers an incorrect install count but we don't really care.
|
|
|
|
return getResource(getMeta('stylish-install-ping-url-chrome'));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(console.error)
|
|
|
|
.then(done);
|
2017-11-25 21:56:57 +00:00
|
|
|
function done() {
|
|
|
|
setTimeout(() => {
|
|
|
|
onClick.processing = false;
|
|
|
|
});
|
|
|
|
}
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2019-06-01 09:12:06 +00:00
|
|
|
function doInstall() {
|
|
|
|
let oldStyle;
|
|
|
|
return API.findStyle({
|
2020-11-18 11:17:15 +00:00
|
|
|
md5Url: getMeta('stylish-md5-url') || location.href,
|
2019-06-01 09:12:06 +00:00
|
|
|
}, true)
|
|
|
|
.then(_oldStyle => {
|
|
|
|
oldStyle = _oldStyle;
|
|
|
|
return oldStyle ?
|
|
|
|
oldStyle.name :
|
|
|
|
getResource(getMeta('stylish-description'));
|
|
|
|
})
|
|
|
|
.then(name => {
|
|
|
|
const props = {};
|
|
|
|
if (oldStyle) {
|
|
|
|
props.id = oldStyle.id;
|
|
|
|
}
|
|
|
|
return saveStyleCode(oldStyle ? 'styleUpdate' : 'styleInstall', name, props);
|
2017-11-25 21:56:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-18 13:30:47 +00:00
|
|
|
function saveStyleCode(message, name, addProps = {}) {
|
2018-11-07 06:09:29 +00:00
|
|
|
const isNew = message === 'styleInstall';
|
|
|
|
const needsConfirmation = isNew || !saveStyleCode.confirmed;
|
|
|
|
if (needsConfirmation && !confirm(chrome.i18n.getMessage(message, [name]))) {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
saveStyleCode.confirmed = true;
|
|
|
|
enableUpdateButton(false);
|
|
|
|
return getStyleJson().then(json => {
|
|
|
|
if (!json) {
|
|
|
|
prompt(chrome.i18n.getMessage('styleInstallFailed', ''),
|
|
|
|
'https://github.com/openstyles/stylus/issues/195');
|
2017-11-25 21:56:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-11-18 13:30:47 +00:00
|
|
|
// Update originalMd5 since USO changed it (2018-11-11) to NOT match the current md5
|
|
|
|
return API.installStyle(Object.assign(json, addProps, {originalMd5: currentMd5}))
|
2018-11-07 06:09:29 +00:00
|
|
|
.then(style => {
|
|
|
|
if (!isNew && style.updateUrl.includes('?')) {
|
|
|
|
enableUpdateButton(true);
|
|
|
|
} else {
|
|
|
|
sendEvent({type: 'styleInstalledChrome'});
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
});
|
2017-11-25 21:56:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function enableUpdateButton(state) {
|
|
|
|
const important = s => s.replace(/;/g, '!important;');
|
|
|
|
const button = document.getElementById('update_style_button');
|
|
|
|
if (button) {
|
|
|
|
button.style.cssText = state ? '' : important('pointer-events: none; opacity: .35;');
|
|
|
|
const icon = button.querySelector('img[src*=".svg"]');
|
|
|
|
if (icon) {
|
|
|
|
icon.style.cssText = state ? '' : important('transition: transform 5s; transform: rotate(0);');
|
|
|
|
if (state) {
|
|
|
|
setTimeout(() => (icon.style.cssText += important('transform: rotate(10turn);')));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMeta(name) {
|
|
|
|
const e = document.querySelector(`link[rel="${name}"]`);
|
|
|
|
return e ? e.getAttribute('href') : null;
|
|
|
|
}
|
|
|
|
|
2018-04-12 06:13:31 +00:00
|
|
|
function getResource(url, options) {
|
2018-11-07 06:09:29 +00:00
|
|
|
if (url.startsWith('#')) {
|
|
|
|
return Promise.resolve(document.getElementById(url.slice(1)).textContent);
|
|
|
|
}
|
|
|
|
return API.download(Object.assign({
|
|
|
|
url,
|
|
|
|
timeout: 60e3,
|
|
|
|
// USO can't handle POST requests for style json
|
|
|
|
body: null,
|
|
|
|
}, options))
|
|
|
|
.catch(error => {
|
|
|
|
alert('Error' + (error ? '\n' + error : ''));
|
|
|
|
throw error;
|
|
|
|
});
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 13:48:58 +00:00
|
|
|
// USO providing md5Url as "https://update.update.userstyles.org/#####.md5"
|
|
|
|
// instead of "https://update.userstyles.org/#####.md5"
|
|
|
|
function tryFixMd5(style) {
|
|
|
|
if (style && style.md5Url && style.md5Url.includes('update.update')) {
|
|
|
|
style.md5Url = style.md5Url.replace('update.update', 'update');
|
|
|
|
}
|
|
|
|
return style;
|
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
|
|
|
function getStyleJson() {
|
2018-04-12 06:13:31 +00:00
|
|
|
return getResource(getStyleURL(), {responseType: 'json'})
|
2018-07-05 12:42:42 +00:00
|
|
|
.then(style => {
|
|
|
|
if (!style || !Array.isArray(style.sections) || style.sections.length) {
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
const codeElement = document.getElementById('stylish-code');
|
|
|
|
if (codeElement && !codeElement.textContent.trim()) {
|
|
|
|
return style;
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
return getResource(getMeta('stylish-update-url'))
|
|
|
|
.then(code => API.parseCss({code}))
|
|
|
|
.then(result => {
|
|
|
|
style.sections = result.sections;
|
|
|
|
return style;
|
2018-07-05 12:42:42 +00:00
|
|
|
});
|
|
|
|
})
|
2018-10-26 13:48:58 +00:00
|
|
|
.then(tryFixMd5)
|
2018-04-12 06:13:31 +00:00
|
|
|
.catch(() => null);
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 10:27:42 +00:00
|
|
|
/**
|
|
|
|
* The sections are checked in successive order because it matters when many sections
|
|
|
|
* match the same URL and they have rules with the same CSS specificity
|
|
|
|
* @param {Object} a - first style object
|
|
|
|
* @param {Object} b - second style object
|
|
|
|
* @returns {?boolean}
|
|
|
|
*/
|
2017-11-25 21:56:57 +00:00
|
|
|
function styleSectionsEqual({sections: a}, {sections: b}) {
|
2020-11-08 10:27:42 +00:00
|
|
|
const targets = ['urls', 'urlPrefixes', 'domains', 'regexps'];
|
|
|
|
return a && b && a.length === b.length && a.every(sameSection);
|
|
|
|
function sameSection(secA, i) {
|
|
|
|
return equalOrEmpty(secA.code, b[i].code, 'string', (a, b) => a === b) &&
|
|
|
|
targets.every(target => equalOrEmpty(secA[target], b[i][target], 'array', arrayMirrors));
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2020-11-08 10:27:42 +00:00
|
|
|
function equalOrEmpty(a, b, type, comparator) {
|
|
|
|
const typeA = type === 'array' ? Array.isArray(a) : typeof a === type;
|
|
|
|
const typeB = type === 'array' ? Array.isArray(b) : typeof b === type;
|
|
|
|
return typeA && typeB && comparator(a, b) ||
|
|
|
|
(a == null || typeA && !a.length) &&
|
|
|
|
(b == null || typeB && !b.length);
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2020-11-08 10:27:42 +00:00
|
|
|
function arrayMirrors(a, b) {
|
|
|
|
return a.length === b.length &&
|
|
|
|
a.every(el => b.includes(el)) &&
|
|
|
|
b.every(el => a.includes(el));
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onDOMready() {
|
2020-10-13 18:14:54 +00:00
|
|
|
return document.readyState !== 'loading'
|
|
|
|
? Promise.resolve()
|
|
|
|
: new Promise(resolve => document.addEventListener('DOMContentLoaded', resolve, {once: true}));
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-10 04:56:41 +00:00
|
|
|
function openSettings(countdown = 10e3) {
|
2018-02-23 12:14:10 +00:00
|
|
|
const button = document.querySelector('.customize_button');
|
2017-12-10 04:56:41 +00:00
|
|
|
if (button) {
|
|
|
|
button.dispatchEvent(new MouseEvent('click', {bubbles: true}));
|
|
|
|
setTimeout(function pollArea(countdown = 2000) {
|
|
|
|
const area = document.getElementById('advancedsettings_area');
|
|
|
|
if (area || countdown < 0) {
|
|
|
|
(area || button).scrollIntoView({behavior: 'smooth', block: area ? 'end' : 'center'});
|
|
|
|
} else {
|
|
|
|
setTimeout(pollArea, 100, countdown - 100);
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
} else if (countdown > 0) {
|
|
|
|
setTimeout(openSettings, 100, countdown - 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function orphanCheck() {
|
2020-10-13 18:14:54 +00:00
|
|
|
try {
|
|
|
|
if (chrome.i18n.getUILanguage()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
2017-11-25 21:56:57 +00:00
|
|
|
// In Chrome content script is orphaned on an extension update/reload
|
|
|
|
// so we need to detach event listeners
|
|
|
|
window.removeEventListener(chrome.runtime.id + '-install', orphanCheck, true);
|
2018-07-03 22:16:18 +00:00
|
|
|
document.removeEventListener('stylishInstallChrome', onClick);
|
|
|
|
document.removeEventListener('stylishUpdateChrome', onClick);
|
2018-01-10 13:44:29 +00:00
|
|
|
try {
|
2018-11-07 06:09:29 +00:00
|
|
|
msg.off(onMessage);
|
2018-01-10 13:44:29 +00:00
|
|
|
} catch (e) {}
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
})();
|
2017-07-12 18:17:04 +00:00
|
|
|
|
2020-10-13 18:14:54 +00:00
|
|
|
function inPageContext(eventId) {
|
|
|
|
document.currentScript.remove();
|
|
|
|
const origMethods = {
|
|
|
|
json: Response.prototype.json,
|
|
|
|
byId: document.getElementById,
|
|
|
|
};
|
|
|
|
let vars;
|
|
|
|
// USO bug workaround: prevent errors in console after install and busy cursor
|
|
|
|
document.getElementById = id =>
|
|
|
|
origMethods.byId.call(document, id) ||
|
|
|
|
(/^(stylish-code|stylish-installed-style-installed-\w+|post-install-ad|style-install-unknown)$/.test(id)
|
|
|
|
? Object.assign(document.createElement('p'), {className: 'afterdownload-ad'})
|
|
|
|
: null);
|
|
|
|
// USO bug workaround: use the actual image data in customized settings
|
|
|
|
document.addEventListener(eventId, ({detail}) => {
|
|
|
|
vars = /\?/.test(detail) && new URL(detail).searchParams;
|
|
|
|
if (!vars) Response.prototype.json = origMethods.json;
|
|
|
|
}, {once: true});
|
|
|
|
Response.prototype.json = async function () {
|
|
|
|
const json = await origMethods.json.apply(this, arguments);
|
|
|
|
if (vars && json && Array.isArray(json.style_settings)) {
|
|
|
|
Response.prototype.json = origMethods.json;
|
|
|
|
const images = new Map();
|
|
|
|
for (const ss of json.style_settings) {
|
|
|
|
const value = vars.get('ik-' + ss.install_key);
|
|
|
|
if (value && ss.setting_type === 'image' && ss.style_setting_options) {
|
|
|
|
let isListed;
|
|
|
|
for (const opt of ss.style_setting_options) {
|
|
|
|
isListed |= opt.default = (opt.value === value);
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
images.set(ss.install_key, {url: value, isListed});
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
}
|
|
|
|
if (images.size) {
|
|
|
|
new MutationObserver((_, observer) => {
|
|
|
|
if (document.getElementById('style-settings')) {
|
2017-07-12 18:17:04 +00:00
|
|
|
observer.disconnect();
|
2020-10-13 18:14:54 +00:00
|
|
|
for (const [name, {url, isListed}] of images) {
|
2017-07-12 18:17:04 +00:00
|
|
|
const elRadio = document.querySelector(`input[name="ik-${name}"][value="user-url"]`);
|
2020-10-13 18:14:54 +00:00
|
|
|
const elUrl = elRadio &&
|
|
|
|
document.getElementById(elRadio.id.replace('url-choice', 'user-url'));
|
2017-07-12 18:17:04 +00:00
|
|
|
if (elUrl) {
|
2020-10-13 18:14:54 +00:00
|
|
|
elRadio.checked = !isListed;
|
2017-07-12 18:17:04 +00:00
|
|
|
elUrl.value = url;
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
}
|
|
|
|
}).observe(document, {childList: true, subtree: true});
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
2019-03-24 23:08:01 +00:00
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
return json;
|
|
|
|
};
|
2019-03-24 23:08:01 +00:00
|
|
|
}
|