2017-12-18 06:57:00 +00:00
|
|
|
/* global cloneInto */
|
2017-07-12 18:17:04 +00:00
|
|
|
'use strict';
|
|
|
|
|
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-01-10 13:44:29 +00:00
|
|
|
chrome.runtime.onMessage.addListener(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-02-27 18:30:29 +00:00
|
|
|
let gotBody = false;
|
2018-03-02 15:08:25 +00:00
|
|
|
new MutationObserver(observeDOM).observe(document.documentElement, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true,
|
|
|
|
});
|
|
|
|
observeDOM();
|
|
|
|
|
|
|
|
function observeDOM() {
|
2018-02-27 18:30:29 +00:00
|
|
|
if (!gotBody) {
|
|
|
|
if (!document.body) return;
|
|
|
|
gotBody = true;
|
2017-11-25 21:56:57 +00:00
|
|
|
// TODO: remove the following statement when USO pagination title is fixed
|
|
|
|
document.title = document.title.replace(/^(\d+)&\w+=/, '#$1: ');
|
|
|
|
chrome.runtime.sendMessage({
|
|
|
|
method: 'getStyles',
|
2017-12-09 01:43:02 +00:00
|
|
|
md5Url: getMeta('stylish-md5-url') || location.href
|
2017-11-25 21:56:57 +00:00
|
|
|
}, checkUpdatability);
|
|
|
|
}
|
2018-02-27 18:30:29 +00:00
|
|
|
if (document.getElementById('install_button')) {
|
|
|
|
onDOMready().then(() => {
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
sendEvent(sendEvent.lastEvent);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-03-02 15:08:25 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2018-01-10 13:44:29 +00:00
|
|
|
function onMessage(msg, sender, sendResponse) {
|
|
|
|
switch (msg.method) {
|
|
|
|
case 'ping':
|
|
|
|
// orphaned content script check
|
|
|
|
sendResponse(true);
|
|
|
|
break;
|
|
|
|
case 'openSettings':
|
|
|
|
openSettings();
|
|
|
|
sendResponse(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(/^[^?]+/, '') : '');
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkUpdatability([installedStyle]) {
|
|
|
|
// TODO: remove the following statement when USO is fixed
|
|
|
|
document.dispatchEvent(new CustomEvent('stylusFixBuggyUSOsettings', {
|
|
|
|
detail: installedStyle && installedStyle.updateUrl,
|
|
|
|
}));
|
|
|
|
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) {
|
|
|
|
getResource(md5Url).then(md5 => {
|
2018-04-30 16:31:43 +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));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function reportUpdatable(isUpdatable) {
|
2018-02-27 18:30:29 +00:00
|
|
|
sendEvent({
|
|
|
|
type: isUpdatable
|
2017-11-25 21:56:57 +00:00
|
|
|
? 'styleCanBeUpdatedChrome'
|
|
|
|
: 'styleAlreadyInstalledChrome',
|
2018-02-27 18:30:29 +00:00
|
|
|
detail: {
|
2017-11-25 21:56:57 +00:00
|
|
|
updateUrl: installedStyle.updateUrl
|
2018-02-27 18:30:29 +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;
|
|
|
|
(event.type.includes('Update') ? onUpdate() : onInstall())
|
|
|
|
.then(done, done);
|
|
|
|
function done() {
|
|
|
|
setTimeout(() => {
|
|
|
|
onClick.processing = false;
|
|
|
|
});
|
|
|
|
}
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
function onInstall() {
|
|
|
|
return getResource(getMeta('stylish-description'))
|
|
|
|
.then(name => saveStyleCode('styleInstall', name))
|
|
|
|
.then(() => getResource(getMeta('stylish-install-ping-url-chrome')));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onUpdate() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
chrome.runtime.sendMessage({
|
|
|
|
method: 'getStyles',
|
2017-12-09 01:43:02 +00:00
|
|
|
md5Url: getMeta('stylish-md5-url') || location.href,
|
2017-11-25 21:56:57 +00:00
|
|
|
}, ([style]) => {
|
|
|
|
saveStyleCode('styleUpdate', style.name, {id: style.id})
|
|
|
|
.then(resolve, reject);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function saveStyleCode(message, name, addProps) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-02 04:07:12 +00:00
|
|
|
const isNew = message === 'styleInstall';
|
|
|
|
const needsConfirmation = isNew || !saveStyleCode.confirmed;
|
2017-11-25 21:56:57 +00:00
|
|
|
if (needsConfirmation && !confirm(chrome.i18n.getMessage(message, [name]))) {
|
|
|
|
reject();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
saveStyleCode.confirmed = true;
|
|
|
|
enableUpdateButton(false);
|
|
|
|
getStyleJson().then(json => {
|
|
|
|
if (!json) {
|
|
|
|
prompt(chrome.i18n.getMessage('styleInstallFailed', ''),
|
|
|
|
'https://github.com/openstyles/stylus/issues/195');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chrome.runtime.sendMessage(
|
|
|
|
Object.assign(json, addProps, {
|
|
|
|
method: 'saveStyle',
|
2017-12-02 04:07:12 +00:00
|
|
|
reason: isNew ? 'install' : 'update',
|
2017-11-25 21:56:57 +00:00
|
|
|
}),
|
|
|
|
style => {
|
2017-12-02 04:07:12 +00:00
|
|
|
if (!isNew && style.updateUrl.includes('?')) {
|
2017-11-25 21:56:57 +00:00
|
|
|
enableUpdateButton(true);
|
|
|
|
} else {
|
2018-02-27 18:30:29 +00:00
|
|
|
sendEvent({type: 'styleInstalledChrome'});
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
2017-11-25 21:56:57 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
if (url.startsWith('#')) {
|
|
|
|
resolve(document.getElementById(url.slice(1)).textContent);
|
|
|
|
} else {
|
2018-04-12 06:13:31 +00:00
|
|
|
chrome.runtime.sendMessage(Object.assign({
|
2018-03-13 14:23:47 +00:00
|
|
|
url,
|
|
|
|
method: 'download',
|
2018-04-11 09:55:06 +00:00
|
|
|
timeout: 60e3,
|
2018-04-12 04:37:51 +00:00
|
|
|
// USO can't handle POST requests for style json
|
|
|
|
body: null,
|
2018-04-12 06:13:31 +00:00
|
|
|
}, options), result => {
|
2018-01-01 17:02:49 +00:00
|
|
|
const error = result && result.__ERROR__;
|
|
|
|
if (error) {
|
|
|
|
alert('Error' + (error ? '\n' + error : ''));
|
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
});
|
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;
|
|
|
|
}
|
|
|
|
return getResource(getMeta('stylish-update-url')).then(code => new Promise(resolve => {
|
|
|
|
chrome.runtime.sendMessage({method: 'parseCss', code}, ({sections}) => {
|
|
|
|
style.sections = sections;
|
|
|
|
resolve(style);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
})
|
2018-04-12 06:13:31 +00:00
|
|
|
.catch(() => null);
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function styleSectionsEqual({sections: a}, {sections: b}) {
|
|
|
|
if (!a || !b) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (a.length !== b.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// order of sections should be identical to account for the case of multiple
|
|
|
|
// sections matching the same URL because the order of rules is part of cascading
|
|
|
|
return a.every((sectionA, index) => propertiesEqual(sectionA, b[index]));
|
|
|
|
|
|
|
|
function propertiesEqual(secA, secB) {
|
|
|
|
for (const name of ['urlPrefixes', 'urls', 'domains', 'regexps']) {
|
|
|
|
if (!equalOrEmpty(secA[name], secB[name], 'every', arrayMirrors)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return equalOrEmpty(secA.code, secB.code, 'substr', (a, b) => a === b);
|
|
|
|
}
|
|
|
|
|
|
|
|
function equalOrEmpty(a, b, telltale, comparator) {
|
|
|
|
const typeA = a && typeof a[telltale] === 'function';
|
|
|
|
const typeB = b && typeof b[telltale] === 'function';
|
|
|
|
return (
|
|
|
|
(a === null || a === undefined || (typeA && !a.length)) &&
|
|
|
|
(b === null || b === undefined || (typeB && !b.length))
|
|
|
|
) || typeA && typeB && a.length === b.length && comparator(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
function arrayMirrors(array1, array2) {
|
|
|
|
return (
|
|
|
|
array1.every(el => array2.includes(el)) &&
|
|
|
|
array2.every(el => array1.includes(el))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onDOMready() {
|
|
|
|
if (document.readyState !== 'loading') {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return new Promise(resolve => {
|
|
|
|
document.addEventListener('DOMContentLoaded', function _() {
|
|
|
|
document.removeEventListener('DOMContentLoaded', _);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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() {
|
2018-02-03 03:31:43 +00:00
|
|
|
// TODO: switch to install-hook-usercss.js impl, and remove explicit orphanCheck() calls
|
2017-11-25 21:56:57 +00:00
|
|
|
if (chrome.i18n && chrome.i18n.getUILanguage()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// 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-02-03 03:31:43 +00:00
|
|
|
chrome.runtime.onMessage.removeListener(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
|
|
|
|
2018-07-04 12:03:54 +00:00
|
|
|
// run in page context
|
|
|
|
document.documentElement.appendChild(document.createElement('script')).text = `(${
|
|
|
|
EXTENSION_ORIGIN => {
|
2018-07-03 22:14:29 +00:00
|
|
|
document.currentScript.remove();
|
|
|
|
|
|
|
|
// spoof Stylish extension presence in Chrome
|
2018-07-04 12:03:54 +00:00
|
|
|
if (window.chrome && chrome.app) {
|
2018-07-03 22:14:29 +00:00
|
|
|
const realImage = window.Image;
|
|
|
|
window.Image = function Image(...args) {
|
|
|
|
return new Proxy(new realImage(...args), {
|
|
|
|
get(obj, key) {
|
|
|
|
return obj[key];
|
|
|
|
},
|
|
|
|
set(obj, key, value) {
|
|
|
|
if (key === 'src' && /^chrome-extension:/i.test(value)) {
|
|
|
|
setTimeout(() => typeof obj.onload === 'function' && obj.onload());
|
|
|
|
} else {
|
|
|
|
obj[key] = value;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:03:54 +00:00
|
|
|
// spoof USO referrer for style search in the popup
|
|
|
|
if (window !== top && location.pathname === '/') {
|
|
|
|
window.addEventListener('message', ({data, origin}) => {
|
|
|
|
if (!data ||
|
|
|
|
!data.xhr ||
|
|
|
|
origin !== EXTENSION_ORIGIN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.onloadend = xhr.onerror = () => {
|
|
|
|
top.postMessage({
|
|
|
|
id: data.xhr.id,
|
|
|
|
status: xhr.status,
|
2018-07-05 08:40:23 +00:00
|
|
|
// [being overcautious] a string response is used instead of relying on responseType=json
|
|
|
|
// because it was invoked in a web page context so another extension may have incorrectly spoofed it
|
2018-07-04 12:03:54 +00:00
|
|
|
response: xhr.response,
|
|
|
|
}, EXTENSION_ORIGIN);
|
|
|
|
};
|
|
|
|
xhr.open('GET', data.xhr.url);
|
|
|
|
xhr.send();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-03 22:14:29 +00:00
|
|
|
// USO bug workaround: use the actual style settings in API response
|
2017-07-12 18:17:04 +00:00
|
|
|
let settings;
|
2017-11-21 10:42:51 +00:00
|
|
|
const originalResponseJson = Response.prototype.json;
|
2017-07-12 18:17:04 +00:00
|
|
|
document.addEventListener('stylusFixBuggyUSOsettings', function _({detail}) {
|
|
|
|
document.removeEventListener('stylusFixBuggyUSOsettings', _);
|
2017-11-28 19:19:00 +00:00
|
|
|
// TODO: remove .replace(/^\?/, '') when minimum_chrome_version >= 52 (https://crbug.com/601425)
|
|
|
|
settings = /\?/.test(detail) && new URLSearchParams(new URL(detail).search.replace(/^\?/, ''));
|
2017-11-21 10:42:51 +00:00
|
|
|
if (!settings) {
|
|
|
|
Response.prototype.json = originalResponseJson;
|
|
|
|
}
|
2017-07-12 18:17:04 +00:00
|
|
|
});
|
2017-07-16 16:49:31 +00:00
|
|
|
Response.prototype.json = function (...args) {
|
2017-07-12 18:17:04 +00:00
|
|
|
return originalResponseJson.call(this, ...args).then(json => {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (!settings || typeof ((json || {}).style_settings || {}).every !== 'function') {
|
2017-07-12 18:17:04 +00:00
|
|
|
return json;
|
|
|
|
}
|
2017-11-21 10:42:51 +00:00
|
|
|
Response.prototype.json = originalResponseJson;
|
2017-07-12 18:17:04 +00:00
|
|
|
const images = new Map();
|
|
|
|
for (const jsonSetting of json.style_settings) {
|
|
|
|
let value = settings.get('ik-' + jsonSetting.install_key);
|
|
|
|
if (!value
|
|
|
|
|| !jsonSetting.style_setting_options
|
|
|
|
|| !jsonSetting.style_setting_options[0]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (value.startsWith('ik-')) {
|
|
|
|
value = value.replace(/^ik-/, '');
|
|
|
|
const defaultItem = jsonSetting.style_setting_options.find(item => item.default);
|
2017-07-16 18:02:00 +00:00
|
|
|
if (!defaultItem || defaultItem.install_key !== value) {
|
2017-07-12 18:17:04 +00:00
|
|
|
if (defaultItem) {
|
|
|
|
defaultItem.default = false;
|
|
|
|
}
|
|
|
|
jsonSetting.style_setting_options.some(item => {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (item.install_key === value) {
|
2017-07-12 18:17:04 +00:00
|
|
|
item.default = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-07-16 18:02:00 +00:00
|
|
|
} else if (jsonSetting.setting_type === 'image') {
|
2017-07-12 18:17:04 +00:00
|
|
|
jsonSetting.style_setting_options.some(item => {
|
|
|
|
if (item.default) {
|
|
|
|
item.default = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
images.set(jsonSetting.install_key, value);
|
|
|
|
} else {
|
|
|
|
const item = jsonSetting.style_setting_options[0];
|
2017-07-16 18:02:00 +00:00
|
|
|
if (item.value !== value && item.install_key === 'placeholder') {
|
2017-07-12 18:17:04 +00:00
|
|
|
item.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (images.size) {
|
|
|
|
new MutationObserver((_, observer) => {
|
|
|
|
if (!document.getElementById('style-settings')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
observer.disconnect();
|
|
|
|
for (const [name, url] of images.entries()) {
|
|
|
|
const elRadio = document.querySelector(`input[name="ik-${name}"][value="user-url"]`);
|
|
|
|
const elUrl = elRadio && document.getElementById(elRadio.id.replace('url-choice', 'user-url'));
|
|
|
|
if (elUrl) {
|
|
|
|
elUrl.value = url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).observe(document, {childList: true, subtree: true});
|
|
|
|
}
|
|
|
|
return json;
|
|
|
|
});
|
|
|
|
};
|
2018-07-04 12:03:54 +00:00
|
|
|
}
|
|
|
|
})('${chrome.runtime.getURL('').slice(0, -1)}')`;
|
2017-07-12 18:17:04 +00:00
|
|
|
|
|
|
|
// TODO: remove the following statement when USO pagination is fixed
|
|
|
|
if (location.search.includes('category=')) {
|
|
|
|
document.addEventListener('DOMContentLoaded', function _() {
|
|
|
|
document.removeEventListener('DOMContentLoaded', _);
|
|
|
|
new MutationObserver((_, observer) => {
|
|
|
|
if (!document.getElementById('pagination')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
observer.disconnect();
|
|
|
|
const category = '&' + location.search.match(/category=[^&]+/)[0];
|
|
|
|
const links = document.querySelectorAll('#pagination a[href*="page="]:not([href*="category="])');
|
|
|
|
for (let i = 0; i < links.length; i++) {
|
|
|
|
links[i].href += category;
|
|
|
|
}
|
|
|
|
}).observe(document, {childList: true, subtree: true});
|
|
|
|
});
|
|
|
|
}
|