2021-08-13 20:03:01 +00:00
|
|
|
/* global $ $$ $create $remove showSpinner toggleDataset */// dom.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global $entry tabURL */// popup.js
|
|
|
|
/* global API */// msg.js
|
|
|
|
/* global Events */
|
2022-02-20 21:02:42 +00:00
|
|
|
/* global FIREFOX URLS debounce download tryURL */// toolbox.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global prefs */
|
|
|
|
/* global t */// localization.js
|
2017-12-09 21:03:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
(() => {
|
2021-08-13 20:03:01 +00:00
|
|
|
const RESULT_ID_PREFIX = t.template.searchResult.className + '-';
|
|
|
|
const RESULT_SEL = '.' + t.template.searchResult.className;
|
2021-08-12 17:35:56 +00:00
|
|
|
const INDEX_URL = URLS.usoArchiveRaw[0] + 'search-index.json';
|
2021-04-20 17:40:04 +00:00
|
|
|
const USW_INDEX_URL = URLS.usw + 'api/index/uso-format';
|
|
|
|
const USW_ICON = $create('img', {
|
|
|
|
src: `${URLS.usw}favicon.ico`,
|
|
|
|
title: URLS.usw,
|
|
|
|
});
|
2020-01-28 02:36:43 +00:00
|
|
|
const STYLUS_CATEGORY = 'chrome-extension';
|
2020-10-04 05:30:02 +00:00
|
|
|
const PAGE_LENGTH = 10;
|
|
|
|
// update USO style install counter if the style isn't uninstalled immediately
|
|
|
|
const PINGBACK_DELAY = 5e3;
|
|
|
|
const BUSY_DELAY = .5e3;
|
|
|
|
const USO_AUTO_PIC_SUFFIX = '-after.png';
|
|
|
|
const BLANK_PIXEL = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
|
|
|
|
const dom = {};
|
|
|
|
/**
|
|
|
|
* @typedef IndexEntry
|
|
|
|
* @prop {'uso' | 'uso-android'} f - format
|
|
|
|
* @prop {Number} i - id
|
|
|
|
* @prop {string} n - name
|
|
|
|
* @prop {string} c - category
|
|
|
|
* @prop {Number} u - updatedTime
|
|
|
|
* @prop {Number} t - totalInstalls
|
|
|
|
* @prop {Number} w - weeklyInstalls
|
|
|
|
* @prop {Number} r - rating
|
|
|
|
* @prop {Number} ai - authorId
|
|
|
|
* @prop {string} an - authorName
|
|
|
|
* @prop {string} sn - screenshotName
|
|
|
|
* @prop {boolean} sa - screenshotArchived
|
2021-08-13 20:03:01 +00:00
|
|
|
* --------------------- Stylus' internally added extras
|
|
|
|
* @prop {boolean} installed
|
|
|
|
* @prop {number} installedStyleId
|
|
|
|
* @prop {number} pingbackTimer
|
2020-10-04 05:30:02 +00:00
|
|
|
*/
|
|
|
|
/** @type IndexEntry[] */
|
|
|
|
let results;
|
|
|
|
/** @type IndexEntry[] */
|
|
|
|
let index;
|
|
|
|
let category = '';
|
2020-10-10 11:25:43 +00:00
|
|
|
let searchGlobals = $('#search-globals').checked;
|
2020-10-04 05:30:02 +00:00
|
|
|
/** @type string[] */
|
|
|
|
let query = [];
|
2021-04-20 17:40:04 +00:00
|
|
|
let order = prefs.get('popup.findSort');
|
2017-12-11 02:20:59 +00:00
|
|
|
let scrollToFirstResult = true;
|
|
|
|
let displayedPage = 1;
|
|
|
|
let totalPages = 1;
|
2020-10-04 05:30:02 +00:00
|
|
|
let ready;
|
2017-12-11 02:20:59 +00:00
|
|
|
|
2021-08-12 13:02:48 +00:00
|
|
|
let imgType = '.jpg';
|
|
|
|
// detect WebP support
|
|
|
|
$create('img', {
|
|
|
|
src: 'data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAAAAAAfQ//73v/+BiOh/AAA=',
|
|
|
|
onload: () => (imgType = '.webp'),
|
|
|
|
});
|
|
|
|
|
2021-08-13 20:03:01 +00:00
|
|
|
/** @returns {{result: IndexEntry, entry: HTMLElement}} */
|
|
|
|
const $resultEntry = el => {
|
|
|
|
const entry = el.closest(RESULT_SEL);
|
|
|
|
return {entry, result: entry && entry._result};
|
|
|
|
};
|
2022-04-05 10:07:24 +00:00
|
|
|
Events.searchInline = () => {
|
|
|
|
calcCategory();
|
|
|
|
ready = start();
|
|
|
|
};
|
2022-02-20 21:02:42 +00:00
|
|
|
Events.searchSite = event => {
|
|
|
|
// use a less specific category if the inline search wasn't used yet
|
|
|
|
if (!category) calcCategory({retry: 1});
|
|
|
|
const add = (prefix, str) => str ? prefix + str : '';
|
|
|
|
const where = event.detail;
|
|
|
|
const q = encodeURIComponent($('#search-query').value.trim());
|
|
|
|
const catQ = category + add('+', q);
|
|
|
|
const href =
|
|
|
|
where === 'uso' &&
|
|
|
|
`${URLS.uso}styles/browse${q ? `?search_terms=${catQ}` : `/${category}`}` ||
|
|
|
|
where === 'usoa' &&
|
|
|
|
`${URLS.usoArchive}browse/styles?search=%23${catQ}` ||
|
|
|
|
where === 'usw' &&
|
|
|
|
`${URLS.usw}search?q=${catQ}` ||
|
|
|
|
where === 'gf' &&
|
|
|
|
'https://greasyfork.org/' + ($.root.lang.split('-')[0] || 'en') +
|
2022-03-06 00:11:48 +00:00
|
|
|
`/scripts/by-site/${tryURL(tabURL).hostname.replace(/^www\./, '')}?language=css${add('&q=', q)}`;
|
2022-02-20 21:02:42 +00:00
|
|
|
Events.openURLandHide.call({href}, event);
|
|
|
|
};
|
|
|
|
$('#search-globals').onchange = function () {
|
|
|
|
searchGlobals = this.checked;
|
|
|
|
ready = ready.then(start);
|
|
|
|
};
|
|
|
|
$('#search-query').oninput = function () {
|
|
|
|
query = [];
|
|
|
|
const text = this.value.trim().toLocaleLowerCase();
|
|
|
|
const thisYear = new Date().getFullYear();
|
|
|
|
for (let re = /"(.+?)"|(\S+)/g, m; (m = re.exec(text));) {
|
|
|
|
const n = Number(m[2]);
|
|
|
|
query.push(n >= 2000 && n <= thisYear ? n : m[1] || m[2]);
|
2017-12-09 21:03:17 +00:00
|
|
|
}
|
2022-02-20 21:02:42 +00:00
|
|
|
if (category === STYLUS_CATEGORY && !query.includes('stylus')) {
|
|
|
|
query.push('stylus');
|
|
|
|
}
|
|
|
|
ready = ready.then(start);
|
|
|
|
};
|
|
|
|
$('#search-order').value = order;
|
|
|
|
$('#search-order').onchange = function () {
|
|
|
|
order = this.value;
|
|
|
|
prefs.set('popup.findSort', order);
|
|
|
|
results.sort(comparator);
|
|
|
|
render();
|
|
|
|
};
|
|
|
|
dom.list = $('#search-results-list');
|
|
|
|
dom.container = $('#search-results');
|
|
|
|
dom.container.dataset.empty = '';
|
|
|
|
dom.error = $('#search-results-error');
|
|
|
|
dom.nav = {};
|
|
|
|
const navOnClick = {prev, next};
|
|
|
|
for (const place of ['top', 'bottom']) {
|
|
|
|
const nav = $(`.search-results-nav[data-type="${place}"]`);
|
|
|
|
nav.appendChild(t.template.searchNav.cloneNode(true));
|
|
|
|
dom.nav[place] = nav;
|
|
|
|
for (const child of $$('[data-type]', nav)) {
|
|
|
|
const type = child.dataset.type;
|
|
|
|
child.onclick = navOnClick[type];
|
|
|
|
nav['_' + type] = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FIREFOX) {
|
|
|
|
let lastShift;
|
|
|
|
window.on('resize', () => {
|
|
|
|
const scrollbarWidth = window.innerWidth - document.scrollingElement.clientWidth;
|
|
|
|
const shift = document.body.getBoundingClientRect().left;
|
|
|
|
if (!scrollbarWidth || shift === lastShift) return;
|
|
|
|
lastShift = shift;
|
|
|
|
document.body.style.setProperty('padding',
|
|
|
|
`0 ${scrollbarWidth + shift}px 0 ${-shift}px`, 'important');
|
|
|
|
}, {passive: true});
|
|
|
|
}
|
2017-12-09 21:03:17 +00:00
|
|
|
|
2022-02-20 21:02:42 +00:00
|
|
|
window.on('styleDeleted', ({detail: {style: {id}}}) => {
|
|
|
|
restoreScrollPosition();
|
|
|
|
const result = results.find(r => r.installedStyleId === id);
|
|
|
|
if (result) {
|
|
|
|
clearTimeout(result.pingbackTimer);
|
|
|
|
renderActionButtons(result.i, -1);
|
2017-12-13 20:13:35 +00:00
|
|
|
}
|
2022-02-20 21:02:42 +00:00
|
|
|
});
|
2017-12-11 20:25:41 +00:00
|
|
|
|
2022-02-20 21:02:42 +00:00
|
|
|
window.on('styleAdded', async ({detail: {style}}) => {
|
|
|
|
restoreScrollPosition();
|
|
|
|
const id = calcId(style) || calcId(await API.styles.get(style.id));
|
|
|
|
if (id && results.find(r => r.i === id)) {
|
|
|
|
renderActionButtons(id, style.id);
|
|
|
|
}
|
|
|
|
});
|
2017-12-10 01:03:04 +00:00
|
|
|
|
2017-12-11 02:20:59 +00:00
|
|
|
function next() {
|
2020-10-04 05:30:02 +00:00
|
|
|
displayedPage = Math.min(totalPages, displayedPage + 1);
|
2017-12-11 02:20:59 +00:00
|
|
|
scrollToFirstResult = true;
|
|
|
|
render();
|
|
|
|
}
|
2017-12-09 21:03:17 +00:00
|
|
|
|
2017-12-11 02:20:59 +00:00
|
|
|
function prev() {
|
|
|
|
displayedPage = Math.max(1, displayedPage - 1);
|
|
|
|
scrollToFirstResult = true;
|
|
|
|
render();
|
|
|
|
}
|
2017-12-09 21:03:17 +00:00
|
|
|
|
2017-12-11 02:20:59 +00:00
|
|
|
function error(reason) {
|
2020-10-04 05:30:02 +00:00
|
|
|
dom.error.textContent = reason;
|
2022-04-05 10:07:24 +00:00
|
|
|
dom.error.hidden = false;
|
|
|
|
dom.list.hidden = true;
|
2017-12-19 02:29:21 +00:00
|
|
|
if (dom.error.getBoundingClientRect().bottom < 0) {
|
|
|
|
dom.error.scrollIntoView({behavior: 'smooth', block: 'start'});
|
2017-12-11 19:26:33 +00:00
|
|
|
}
|
2017-12-11 02:20:59 +00:00
|
|
|
}
|
2017-12-09 21:03:17 +00:00
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
async function start() {
|
2022-04-05 10:07:24 +00:00
|
|
|
resetUI.timer = resetUI.timer || setTimeout(resetUI, 500);
|
2020-10-04 05:30:02 +00:00
|
|
|
try {
|
2021-04-20 17:40:04 +00:00
|
|
|
results = [];
|
2020-10-04 05:30:02 +00:00
|
|
|
for (let retry = 0; !results.length && retry <= 2; retry++) {
|
|
|
|
results = await search({retry});
|
2017-12-11 02:20:59 +00:00
|
|
|
}
|
2020-10-04 05:30:02 +00:00
|
|
|
if (results.length) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const installedStyles = await API.styles.getAll();
|
2021-04-20 17:40:04 +00:00
|
|
|
const allSupportedIds = new Set(installedStyles.map(calcId));
|
|
|
|
results = results.filter(r => !allSupportedIds.has(r.i));
|
2017-12-11 20:25:41 +00:00
|
|
|
}
|
2020-10-10 11:25:43 +00:00
|
|
|
render();
|
2022-04-05 10:07:24 +00:00
|
|
|
dom.list.hidden = !results.length;
|
2020-10-10 11:25:43 +00:00
|
|
|
if (!results.length && !$('#search-query').value) {
|
2022-07-07 13:49:43 +00:00
|
|
|
if (index._ready) error(t('searchResultNoneFound'));
|
2022-04-05 10:07:24 +00:00
|
|
|
} else {
|
|
|
|
resetUI();
|
2017-12-09 21:03:17 +00:00
|
|
|
}
|
2020-10-04 05:30:02 +00:00
|
|
|
} catch (reason) {
|
|
|
|
error(reason);
|
|
|
|
}
|
2022-02-20 21:02:42 +00:00
|
|
|
clearTimeout(resetUI.timer);
|
2022-04-05 10:07:24 +00:00
|
|
|
resetUI.timer = 0;
|
2022-02-20 21:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function resetUI() {
|
|
|
|
document.body.classList.add('search-results-shown');
|
2022-04-05 10:07:24 +00:00
|
|
|
dom.container.hidden = false;
|
|
|
|
dom.list.hidden = false;
|
|
|
|
dom.error.hidden = true;
|
2017-12-11 02:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function render() {
|
2020-10-04 05:30:02 +00:00
|
|
|
totalPages = Math.ceil(results.length / PAGE_LENGTH);
|
|
|
|
displayedPage = Math.min(displayedPage, totalPages) || 1;
|
|
|
|
let start = (displayedPage - 1) * PAGE_LENGTH;
|
|
|
|
const end = displayedPage * PAGE_LENGTH;
|
2017-12-11 02:20:59 +00:00
|
|
|
let plantAt = 0;
|
|
|
|
let slot = dom.list.children[0];
|
|
|
|
// keep rendered elements with ids in the range of interest
|
|
|
|
while (
|
2020-10-04 05:30:02 +00:00
|
|
|
plantAt < PAGE_LENGTH &&
|
2021-08-13 20:03:01 +00:00
|
|
|
slot && slot.id === RESULT_ID_PREFIX + (results[start] || {}).i
|
2017-12-11 02:20:59 +00:00
|
|
|
) {
|
|
|
|
slot = slot.nextElementSibling;
|
|
|
|
plantAt++;
|
|
|
|
start++;
|
2017-12-09 21:03:17 +00:00
|
|
|
}
|
2020-10-04 05:30:02 +00:00
|
|
|
// add new elements
|
|
|
|
while (start < Math.min(end, results.length)) {
|
|
|
|
const entry = createSearchResultNode(results[start++]);
|
2017-12-11 02:20:59 +00:00
|
|
|
if (slot) {
|
|
|
|
dom.list.replaceChild(entry, slot);
|
|
|
|
slot = entry.nextElementSibling;
|
2017-12-09 21:03:17 +00:00
|
|
|
} else {
|
2017-12-11 02:20:59 +00:00
|
|
|
dom.list.appendChild(entry);
|
2017-12-09 21:03:17 +00:00
|
|
|
}
|
2017-12-11 02:20:59 +00:00
|
|
|
plantAt++;
|
2017-12-09 21:03:17 +00:00
|
|
|
}
|
2020-10-04 05:30:02 +00:00
|
|
|
// remove extraneous elements
|
|
|
|
const pageLen = end > results.length &&
|
|
|
|
results.length % PAGE_LENGTH ||
|
|
|
|
Math.min(results.length, PAGE_LENGTH);
|
|
|
|
while (dom.list.children.length > pageLen) {
|
2017-12-11 02:20:59 +00:00
|
|
|
dom.list.lastElementChild.remove();
|
|
|
|
}
|
2020-10-04 05:30:02 +00:00
|
|
|
if (results.length && 'empty' in dom.container.dataset) {
|
2017-12-11 20:25:41 +00:00
|
|
|
delete dom.container.dataset.empty;
|
|
|
|
}
|
2022-01-29 15:19:21 +00:00
|
|
|
if (scrollToFirstResult) {
|
2017-12-11 20:25:41 +00:00
|
|
|
debounce(doScrollToFirstResult);
|
|
|
|
}
|
2020-10-04 05:30:02 +00:00
|
|
|
// navigation
|
|
|
|
for (const place in dom.nav) {
|
|
|
|
const nav = dom.nav[place];
|
|
|
|
nav._prev.disabled = displayedPage <= 1;
|
|
|
|
nav._next.disabled = displayedPage >= totalPages;
|
|
|
|
nav._page.textContent = displayedPage;
|
|
|
|
nav._total.textContent = totalPages;
|
|
|
|
}
|
2017-12-11 20:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function doScrollToFirstResult() {
|
|
|
|
if (dom.container.scrollHeight > window.innerHeight * 2) {
|
2017-12-11 02:20:59 +00:00
|
|
|
scrollToFirstResult = false;
|
2017-12-11 20:25:41 +00:00
|
|
|
dom.container.scrollIntoView({behavior: 'smooth', block: 'start'});
|
2017-12-11 02:20:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-10 09:00:40 +00:00
|
|
|
|
2017-12-11 02:20:59 +00:00
|
|
|
/**
|
2020-10-04 05:30:02 +00:00
|
|
|
* @param {IndexEntry} result
|
|
|
|
* @returns {Node}
|
2017-12-11 02:20:59 +00:00
|
|
|
*/
|
|
|
|
function createSearchResultNode(result) {
|
2020-11-18 11:17:15 +00:00
|
|
|
const entry = t.template.searchResult.cloneNode(true);
|
2020-10-04 05:30:02 +00:00
|
|
|
const {
|
|
|
|
i: id,
|
|
|
|
n: name,
|
|
|
|
r: rating,
|
|
|
|
u: updateTime,
|
|
|
|
w: weeklyInstalls,
|
|
|
|
t: totalInstalls,
|
2021-08-12 17:35:56 +00:00
|
|
|
ai: authorId,
|
2020-10-04 05:30:02 +00:00
|
|
|
an: author,
|
|
|
|
sa: shotArchived,
|
2021-08-12 17:35:56 +00:00
|
|
|
sn: shot,
|
2022-01-19 21:41:03 +00:00
|
|
|
f: fmt,
|
2020-10-04 05:30:02 +00:00
|
|
|
} = entry._result = result;
|
|
|
|
entry.id = RESULT_ID_PREFIX + id;
|
|
|
|
// title
|
2017-12-11 02:20:59 +00:00
|
|
|
Object.assign($('.search-result-title', entry), {
|
2021-01-01 14:27:58 +00:00
|
|
|
onclick: Events.openURLandHide,
|
2022-01-19 21:41:03 +00:00
|
|
|
href: `${fmt ? URLS.usoArchive : URLS.usw}style/${id}`,
|
2017-12-11 02:20:59 +00:00
|
|
|
});
|
2022-01-19 21:41:03 +00:00
|
|
|
if (!fmt) $('.search-result-title', entry).prepend(USW_ICON.cloneNode(true));
|
2020-10-04 05:30:02 +00:00
|
|
|
$('.search-result-title span', entry).textContent =
|
2020-11-18 11:17:15 +00:00
|
|
|
t.breakWord(name.length < 300 ? name : name.slice(0, 300) + '...');
|
2020-10-04 05:30:02 +00:00
|
|
|
// screenshot
|
2021-04-20 17:40:04 +00:00
|
|
|
const elShot = $('.search-result-screenshot', entry);
|
2021-08-13 20:03:01 +00:00
|
|
|
let shotSrc;
|
2022-01-19 21:41:03 +00:00
|
|
|
if (!fmt) {
|
2021-08-13 20:03:01 +00:00
|
|
|
shotSrc = /^https?:/i.test(shot) && shot.replace(/\.jpg$/, imgType);
|
2021-04-20 17:40:04 +00:00
|
|
|
} else {
|
2021-08-13 20:03:01 +00:00
|
|
|
elShot._src = URLS.uso + `auto_style_screenshots/${id}${USO_AUTO_PIC_SUFFIX}`;
|
|
|
|
shotSrc = shot && !shot.endsWith(USO_AUTO_PIC_SUFFIX)
|
|
|
|
? `${shotArchived ? URLS.usoArchiveRaw[0] : URLS.uso + 'style_'}screenshots/${shot}`
|
|
|
|
: elShot._src;
|
|
|
|
}
|
|
|
|
if (shotSrc) {
|
|
|
|
elShot._entry = entry;
|
|
|
|
elShot.src = shotSrc;
|
|
|
|
elShot.onerror = fixScreenshot;
|
|
|
|
} else {
|
|
|
|
elShot.src = BLANK_PIXEL;
|
|
|
|
entry.dataset.noImage = '';
|
2021-04-20 17:40:04 +00:00
|
|
|
}
|
2020-10-04 05:30:02 +00:00
|
|
|
// author
|
2017-12-11 10:03:03 +00:00
|
|
|
Object.assign($('[data-type="author"] a', entry), {
|
2020-10-04 05:30:02 +00:00
|
|
|
textContent: author,
|
|
|
|
title: author,
|
2022-01-19 21:41:03 +00:00
|
|
|
href: !fmt ? `${URLS.usw}user/${encodeURIComponent(author)}` :
|
2021-08-12 17:35:56 +00:00
|
|
|
`${URLS.usoArchive}browse/styles?search=%40${authorId}`,
|
2021-01-01 14:27:58 +00:00
|
|
|
onclick: Events.openURLandHide,
|
2017-12-11 02:20:59 +00:00
|
|
|
});
|
2020-10-04 05:30:02 +00:00
|
|
|
// rating
|
|
|
|
$('[data-type="rating"]', entry).dataset.class =
|
|
|
|
!rating ? 'none' :
|
|
|
|
rating >= 2.5 ? 'good' :
|
|
|
|
rating >= 1.5 ? 'okay' :
|
|
|
|
'bad';
|
|
|
|
$('[data-type="rating"] dd', entry).textContent = rating && rating.toFixed(1) || '';
|
|
|
|
// time
|
2017-12-11 10:03:03 +00:00
|
|
|
Object.assign($('[data-type="updated"] time', entry), {
|
2020-10-04 05:30:02 +00:00
|
|
|
dateTime: updateTime * 1000,
|
2020-11-18 11:17:15 +00:00
|
|
|
textContent: t.formatDate(updateTime * 1000),
|
2017-12-11 04:35:23 +00:00
|
|
|
});
|
2020-10-04 05:30:02 +00:00
|
|
|
// totals
|
|
|
|
$('[data-type="weekly"] dd', entry).textContent = formatNumber(weeklyInstalls);
|
|
|
|
$('[data-type="total"] dd', entry).textContent = formatNumber(totalInstalls);
|
2017-12-11 02:20:59 +00:00
|
|
|
renderActionButtons(entry);
|
|
|
|
return entry;
|
|
|
|
}
|
2017-12-10 01:03:04 +00:00
|
|
|
|
2017-12-11 10:03:03 +00:00
|
|
|
function formatNumber(num) {
|
|
|
|
return (
|
|
|
|
num > 1e9 ? (num / 1e9).toFixed(1) + 'B' :
|
|
|
|
num > 10e6 ? (num / 1e6).toFixed(0) + 'M' :
|
|
|
|
num > 1e6 ? (num / 1e6).toFixed(1) + 'M' :
|
|
|
|
num > 10e3 ? (num / 1e3).toFixed(0) + 'k' :
|
|
|
|
num > 1e3 ? (num / 1e3).toFixed(1) + 'k' :
|
|
|
|
num
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
function fixScreenshot() {
|
|
|
|
const {_src} = this;
|
|
|
|
if (_src && _src !== this.src) {
|
|
|
|
this.src = _src;
|
|
|
|
delete this._src;
|
|
|
|
} else {
|
|
|
|
this.onerror = null;
|
2021-08-13 20:03:01 +00:00
|
|
|
this.src = BLANK_PIXEL;
|
|
|
|
this._entry.dataset.noImage = '';
|
|
|
|
renderActionButtons(this._entry);
|
2017-12-13 21:49:11 +00:00
|
|
|
}
|
2020-10-04 05:30:02 +00:00
|
|
|
}
|
2017-12-11 19:26:33 +00:00
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
function renderActionButtons(entry, installedId) {
|
|
|
|
if (Number(entry)) {
|
|
|
|
entry = $('#' + RESULT_ID_PREFIX + entry);
|
|
|
|
}
|
|
|
|
if (!entry) return;
|
|
|
|
const result = entry._result;
|
|
|
|
if (typeof installedId === 'number') {
|
|
|
|
result.installed = installedId > 0;
|
|
|
|
result.installedStyleId = installedId;
|
|
|
|
}
|
|
|
|
const isInstalled = result.installed;
|
2021-08-13 20:03:01 +00:00
|
|
|
const status = $('.search-result-status', entry).textContent =
|
|
|
|
isInstalled ? t('clickToUninstall') :
|
|
|
|
entry.dataset.noImage != null ? t('installButton') :
|
|
|
|
'';
|
2021-01-01 14:27:58 +00:00
|
|
|
const notMatching = installedId > 0 && !$entry(installedId);
|
2020-11-23 12:45:00 +00:00
|
|
|
if (notMatching !== entry.classList.contains('not-matching')) {
|
|
|
|
entry.classList.toggle('not-matching');
|
|
|
|
if (notMatching) {
|
|
|
|
entry.prepend(t.template.searchResultNotMatching.cloneNode(true));
|
|
|
|
} else {
|
|
|
|
entry.firstElementChild.remove();
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 05:30:02 +00:00
|
|
|
Object.assign($('.search-result-screenshot', entry), {
|
|
|
|
onclick: isInstalled ? uninstall : install,
|
2021-08-13 20:03:01 +00:00
|
|
|
title: status ? '' : t('installButton'),
|
2020-10-04 05:30:02 +00:00
|
|
|
});
|
|
|
|
$('.search-result-uninstall', entry).onclick = uninstall;
|
|
|
|
$('.search-result-install', entry).onclick = install;
|
2021-08-13 20:03:01 +00:00
|
|
|
Object.assign($('.search-result-customize', entry), {
|
|
|
|
onclick: configure,
|
|
|
|
disabled: notMatching,
|
|
|
|
});
|
|
|
|
toggleDataset(entry, 'installed', isInstalled);
|
2017-12-11 02:20:59 +00:00
|
|
|
}
|
2017-12-09 21:03:17 +00:00
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
function renderFullInfo(entry, style) {
|
|
|
|
let {description, vars} = style.usercssData;
|
|
|
|
// description
|
|
|
|
description = (description || '')
|
|
|
|
.replace(/<[^>]*>/g, ' ')
|
|
|
|
.replace(/([^.][.。?!]|[\s,].{50,70})\s+/g, '$1\n')
|
|
|
|
.replace(/([\r\n]\s*){3,}/g, '\n\n');
|
|
|
|
Object.assign($('.search-result-description', entry), {
|
|
|
|
textContent: description,
|
|
|
|
title: description,
|
|
|
|
});
|
2021-08-13 20:03:01 +00:00
|
|
|
toggleDataset(entry, 'customizable', vars);
|
|
|
|
}
|
|
|
|
|
|
|
|
function configure() {
|
|
|
|
const styleEntry = $entry($resultEntry(this).result.installedStyleId);
|
|
|
|
Events.configure.call(this, {target: styleEntry});
|
2017-12-11 02:20:59 +00:00
|
|
|
}
|
2017-12-09 21:03:17 +00:00
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
async function install() {
|
2021-08-13 20:03:01 +00:00
|
|
|
const {entry, result} = $resultEntry(this);
|
2022-01-19 21:41:03 +00:00
|
|
|
const {i: id, f: fmt} = result;
|
2017-12-11 02:20:59 +00:00
|
|
|
const installButton = $('.search-result-install', entry);
|
|
|
|
|
|
|
|
showSpinner(entry);
|
2017-12-13 22:13:16 +00:00
|
|
|
saveScrollPosition(entry);
|
2017-12-11 02:20:59 +00:00
|
|
|
installButton.disabled = true;
|
2017-12-11 04:35:23 +00:00
|
|
|
entry.style.setProperty('pointer-events', 'none', 'important');
|
2021-08-12 13:44:02 +00:00
|
|
|
delete entry.dataset.error;
|
2022-01-19 21:41:03 +00:00
|
|
|
if (fmt) {
|
2021-04-20 17:40:04 +00:00
|
|
|
// FIXME: move this to background page and create an API like installUSOStyle
|
|
|
|
result.pingbackTimer = setTimeout(download, PINGBACK_DELAY,
|
|
|
|
`${URLS.uso}styles/install/${id}?source=stylish-ch`);
|
|
|
|
}
|
|
|
|
|
2022-01-19 21:41:03 +00:00
|
|
|
const updateUrl = fmt ? URLS.makeUsoArchiveCodeUrl(id) : URLS.makeUswCodeUrl(id);
|
2017-12-11 02:20:59 +00:00
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
try {
|
|
|
|
const sourceCode = await download(updateUrl);
|
2021-01-01 14:27:58 +00:00
|
|
|
const style = await API.usercss.install({sourceCode, updateUrl});
|
2020-10-04 05:30:02 +00:00
|
|
|
renderFullInfo(entry, style);
|
|
|
|
} catch (reason) {
|
2021-08-12 13:44:02 +00:00
|
|
|
entry.dataset.error = `${t('genericError')}: ${reason}`;
|
|
|
|
entry.scrollIntoView({behavior: 'smooth', block: 'nearest'});
|
2017-12-11 02:20:59 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
$remove('.lds-spinner', entry);
|
2020-10-04 05:30:02 +00:00
|
|
|
installButton.disabled = false;
|
|
|
|
entry.style.pointerEvents = '';
|
2017-12-09 21:03:17 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
function uninstall() {
|
2021-08-13 20:03:01 +00:00
|
|
|
const {entry, result} = $resultEntry(this);
|
2020-10-04 05:30:02 +00:00
|
|
|
saveScrollPosition(entry);
|
2021-08-13 20:03:01 +00:00
|
|
|
API.styles.delete(result.installedStyleId);
|
2017-12-13 22:46:32 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:13:16 +00:00
|
|
|
function saveScrollPosition(entry) {
|
2020-10-04 05:30:02 +00:00
|
|
|
dom.scrollPos = entry.getBoundingClientRect().top;
|
|
|
|
dom.scrollPosElement = entry;
|
2017-12-13 22:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function restoreScrollPosition() {
|
2020-10-04 05:30:02 +00:00
|
|
|
window.scrollBy(0, dom.scrollPosElement.getBoundingClientRect().top - dom.scrollPos);
|
2017-12-13 22:13:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 21:03:17 +00:00
|
|
|
/**
|
|
|
|
* Resolves the Userstyles.org "category" for a given URL.
|
2020-10-04 05:30:02 +00:00
|
|
|
* @returns {boolean} true if the category has actually changed
|
2017-12-09 21:03:17 +00:00
|
|
|
*/
|
2020-10-04 05:30:02 +00:00
|
|
|
function calcCategory({retry} = {}) {
|
2022-02-20 21:02:42 +00:00
|
|
|
const u = tryURL(tabURL);
|
2020-10-04 05:30:02 +00:00
|
|
|
const old = category;
|
2022-02-20 21:02:42 +00:00
|
|
|
if (!u.href) {
|
2017-12-11 02:20:59 +00:00
|
|
|
// Invalid URL
|
2020-10-04 05:30:02 +00:00
|
|
|
category = '';
|
2017-12-09 21:03:17 +00:00
|
|
|
} else if (u.protocol === 'file:') {
|
2020-10-04 05:30:02 +00:00
|
|
|
category = 'file:';
|
2017-12-09 21:03:17 +00:00
|
|
|
} else if (u.protocol === location.protocol) {
|
2020-10-04 05:30:02 +00:00
|
|
|
category = STYLUS_CATEGORY;
|
2017-12-09 21:03:17 +00:00
|
|
|
} else {
|
2020-01-28 02:36:43 +00:00
|
|
|
const parts = u.hostname.replace(/\.(?:com?|org)(\.\w{2,3})$/, '$1').split('.');
|
|
|
|
const [tld, main = u.hostname, third, fourth] = parts.reverse();
|
2020-10-04 05:30:02 +00:00
|
|
|
const keepTld = retry !== 1 && !(
|
2020-01-28 02:36:43 +00:00
|
|
|
tld === 'com' ||
|
|
|
|
tld === 'org' && main !== 'userstyles'
|
|
|
|
);
|
|
|
|
const keepThird = !retry && (
|
|
|
|
fourth ||
|
|
|
|
third && third !== 'www' && third !== 'm'
|
|
|
|
);
|
2020-10-04 05:30:02 +00:00
|
|
|
category = (keepThird && `${third}.` || '') + main + (keepTld || keepThird ? `.${tld}` : '');
|
2017-12-12 14:35:38 +00:00
|
|
|
}
|
2020-10-04 05:30:02 +00:00
|
|
|
return category !== old;
|
2017-12-10 07:08:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
async function fetchIndex() {
|
|
|
|
const timer = setTimeout(showSpinner, BUSY_DELAY, dom.list);
|
2022-07-07 13:49:43 +00:00
|
|
|
const jobs = [
|
2022-01-19 21:41:03 +00:00
|
|
|
[INDEX_URL, json => json.filter(entry => entry.f === 'uso')],
|
|
|
|
[USW_INDEX_URL, json => json.data],
|
2022-07-07 13:49:43 +00:00
|
|
|
].map(async ([url, transform]) => {
|
|
|
|
const res = transform(await download(url, {responseType: 'json'}));
|
|
|
|
index = index ? index.concat(res) : res;
|
|
|
|
if (index !== res) ready = ready.then(start);
|
|
|
|
});
|
|
|
|
// TODO: use Promise.allSettled when "minimum_chrome_version" >= 76 and "strict_min_version" >= 71
|
|
|
|
Promise.all(jobs.map(j => j.catch(e => e))).then(() => {
|
|
|
|
index._ready = true;
|
|
|
|
});
|
|
|
|
await Promise.race(jobs);
|
2020-10-04 05:30:02 +00:00
|
|
|
clearTimeout(timer);
|
2021-01-01 14:27:58 +00:00
|
|
|
$remove(':scope > .lds-spinner', dom.list);
|
2020-10-04 05:30:02 +00:00
|
|
|
return index;
|
2017-12-10 07:08:39 +00:00
|
|
|
}
|
2017-12-11 02:20:59 +00:00
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
async function search({retry} = {}) {
|
|
|
|
return retry && !calcCategory({retry})
|
|
|
|
? []
|
|
|
|
: (index || await fetchIndex()).filter(isResultMatching).sort(comparator);
|
2017-12-11 02:20:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
function isResultMatching(res) {
|
2020-10-13 18:14:54 +00:00
|
|
|
// We're trying to call calcHaystack only when needed, not on all 100K items
|
|
|
|
const {c} = res;
|
2020-10-04 05:30:02 +00:00
|
|
|
return (
|
2020-10-13 18:14:54 +00:00
|
|
|
c === category ||
|
2021-04-20 18:05:49 +00:00
|
|
|
(category === STYLUS_CATEGORY
|
|
|
|
? c === 'stylus' // USW
|
|
|
|
: c === 'global' && searchGlobals &&
|
|
|
|
(query.length || calcHaystack(res)._nLC.includes(category))
|
2020-10-13 18:14:54 +00:00
|
|
|
)
|
2020-10-10 11:25:43 +00:00
|
|
|
) && (
|
2020-10-13 18:14:54 +00:00
|
|
|
!query.length || // to skip calling calcHaystack
|
|
|
|
query.every(isInHaystack, calcHaystack(res))
|
2020-10-04 05:30:02 +00:00
|
|
|
);
|
2018-12-10 13:14:43 +00:00
|
|
|
}
|
2018-07-04 12:03:54 +00:00
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
/** @this {IndexEntry} haystack */
|
|
|
|
function isInHaystack(needle) {
|
2020-10-10 11:25:43 +00:00
|
|
|
return this._year === needle && this.c !== 'global' ||
|
|
|
|
this._nLC.includes(needle);
|
2018-12-10 13:14:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
/**
|
|
|
|
* @param {IndexEntry} a
|
|
|
|
* @param {IndexEntry} b
|
|
|
|
*/
|
|
|
|
function comparator(a, b) {
|
|
|
|
return (
|
|
|
|
order === 'n'
|
|
|
|
? a.n < b.n ? -1 : a.n > b.n
|
|
|
|
: b[order] - a[order]
|
|
|
|
) || b.t - a.t;
|
2018-12-10 13:14:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
function calcUsoId({md5Url: m, updateUrl}) {
|
2021-02-26 10:01:27 +00:00
|
|
|
return Number(m && m.match(/\d+|$/)[0]) ||
|
2020-10-04 05:30:02 +00:00
|
|
|
URLS.extractUsoArchiveId(updateUrl);
|
2018-12-10 13:14:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 17:40:04 +00:00
|
|
|
function calcUswId({updateUrl}) {
|
|
|
|
return URLS.extractUSwId(updateUrl) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function calcId(style) {
|
|
|
|
return calcUsoId(style) || calcUswId(style);
|
|
|
|
}
|
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
function calcHaystack(res) {
|
|
|
|
if (!res._nLC) res._nLC = res.n.toLocaleLowerCase();
|
|
|
|
if (!res._year) res._year = new Date(res.u * 1000).getFullYear();
|
|
|
|
return res;
|
2018-07-04 12:03:54 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
})();
|