Created
February 25, 2021 18:20
-
-
Save LenAnderson/bbbaaf2195071582d3139dfd97a7e3f9 to your computer and use it in GitHub Desktop.
Userscript for reddit: Search reddit's user page with regular expressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Reddit - Search User Page | |
// @namespace https://github.com/LenAnderson/ | |
// @version 0.1 | |
// @description Search reddit's user page with regular expressions | |
// @author LenAnderson | |
// @match https://www.reddit.com/user/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const log = (...msgs)=>console.log.call(console.log, '[RSUP]', ...msgs); | |
const $ = (query,root)=>(root?query:document).querySelector(root?root:query); | |
const $$ = (query,root)=>Array.from((root?query:document).querySelectorAll(root?root:query)); | |
const get = (url) => { | |
return new Promise((resolve,reject)=>{ | |
const xhr = new XMLHttpRequest(); | |
xhr.open('GET', url, true); | |
xhr.addEventListener('load', ()=>{ | |
resolve(xhr.responseText); | |
}); | |
xhr.addEventListener('error', ()=>{ | |
reject(xhr); | |
}); | |
xhr.send(); | |
}); | |
}; | |
const getHtml = (url) => { | |
return get(url).then(txt=>{ | |
const html = document.createElement('div'); | |
html.innerHTML = txt; | |
return html; | |
}); | |
}; | |
const getJson = (url) => { | |
return get(url).then(JSON.parse); | |
} | |
const wait = async(millis)=>new Promise(resolve=>setTimeout(resolve, millis)); | |
let things = []; | |
const processPage = async (html, re) => { | |
things.push(...$$(html, '#siteTable > .thing').filter(it=>it.textContent.search(re) != -1)); | |
const next = $(html, '.nav-buttons .next-button a'); | |
if (next) { | |
// wait a moment to not overwhelm server with requests | |
await wait(100); | |
return getHtml(next.href); | |
} | |
}; | |
const init = async()=>{ | |
const siteTable = $('#siteTable'); | |
const container = $('.content[role="main"] > .menuarea'); | |
const inp = document.createElement('input'); { | |
inp.type = 'text'; | |
inp.style.width = '300px'; | |
inp.placeholder = 'Search using regular expressions'; | |
inp.addEventListener('keyup', async(evt)=>{ | |
if (evt.keyCode == 13) { | |
evt.preventDefault(); | |
evt.stopPropagation(); | |
things = []; | |
const blocker = document.createElement('div'); { | |
blocker.style.position = 'fixed'; | |
blocker.style.top = 0; | |
blocker.style.bottom = 0; | |
blocker.style.left = 0; | |
blocker.style.right = 0; | |
blocker.style.zIndex = 99999; | |
blocker.style.fontSize = '3em'; | |
blocker.style.textAlign = 'center'; | |
blocker.style.fontWeight = 'bold'; | |
blocker.style.paddingTop = '40vh'; | |
blocker.style.backgroundColor = 'rgba(0,0,0,0.5)'; | |
blocker.textContent = 'searching... [0]'; | |
document.body.appendChild(blocker); | |
} | |
const re = new RegExp(inp.value, 'i'); | |
let next = document.body; | |
do { | |
next = await processPage(next, re); | |
blocker.textContent = `searching... [${things.length}]`; | |
} while (next); | |
log(things); | |
siteTable.innerHTML = ''; | |
things.forEach(thing=>{ | |
siteTable.appendChild(thing); | |
}); | |
blocker.remove(); | |
} | |
}); | |
container.appendChild(inp); | |
}; | |
}; | |
init(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment