Skip to content

Instantly share code, notes, and snippets.

@marktaiwan
Last active August 20, 2020 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marktaiwan/8921df87c4276b213b80ba0eb12047dd to your computer and use it in GitHub Desktop.
Save marktaiwan/8921df87c4276b213b80ba0eb12047dd to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name The Pony Archive - Derpi searcher
// @namespace Violentmonkey Scripts
// @version 1.1
// @description Search the Derpi archive on TPA
// @author Marker
// @match *://*.theponyarchive.com/archive/derpibooru/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
const getDir = num => 1000 + num - (num % 1000);
const $ = (selector, parent = document) => parent.querySelector(selector);
const $$ = (selector, parent = document) => parent.querySelectorAll(selector);
const create = ele => document.createElement(ele);
function initCSS() {
const CSS = '.hidden {display: none;}';
const styleElement = document.createElement('style');
styleElement.setAttribute('type', 'text/css');
styleElement.innerHTML = CSS;
document.body.insertAdjacentElement('afterend', styleElement);
}
function insertSearch() {
const container = create('div');
container.style.margin = '20px';
container.style.textAlign = 'center';
const input = create('input');
input.style.marginRight = '5px';
input.setAttribute('required', '');
input.placeholder = 'Search image ID';
input.addEventListener('click', e => e.target.select());
input.addEventListener('keyup', e => {
const input = e.target;
const key = e.key;
if (key == 'Enter') {
doSearch(input);
return;
}
if (key == 'Escape') {
input.value = '';
}
doFilter(input);
});
const button = create('button');
button.innerText = 'Search';
button.addEventListener('click', () => {
doSearch(input);
});
[input, button].forEach(ele => container.appendChild(ele));
$('.container')?.before(container);
const searchVal = getQueryVariable('search');
if (searchVal) {
input.value = searchVal;
doFilter(input);
input.focus();
}
}
function doSearch(input) {
if (!input.validity.valid) return;
const id = Number(input.value, 10);
if (inSubDir(id)) return;
const dir = getDir(id);
window.location.href = window.origin + '/archive/derpibooru/' + dir + '/?search=' + id;
}
function doFilter(input) {
if (window.location.pathname === '/archive/derpibooru/') return;
const id = input.value;
for (const tr of $$('.table-responsive tr')) {
if (!input.validity.valid) {
tr.classList.remove('hidden');
continue;
}
const filename = $('a', tr)?.href;
if (filename?.match(id)) {
tr.classList.remove('hidden');
} else {
tr.classList.add('hidden');
}
}
}
function inSubDir(id) {
const dir = getDir(id);
const re = new RegExp(`/archive/derpibooru/${dir}/$`, 'i');
return re.test(window.location.pathname);
}
function getQueryVariable(key) {
const search = window.location.search;
const arr = search.substring(1).split('&').map(string => string.split('='));
for (const list of arr) {
if (list[0] === key) return list[1]
}
return null;
}
initCSS();
insertSearch();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment