// ==UserScript== // @name 4chan pixeldrain URL parser // @namespace 4chanpixeldrainparse // @match https://boards.4chan.org/*/thread/* // @grant none // @version 1.0.3 // @author Anonymous // @description Parse pixeldrain URLs on 4chan // @updateURL https://gist.github.com/raw/6fedeaacb169bc89bc5186bd00741b4a/4chan-pixeldrain.user.js // @downloadURL https://gist.github.com/raw/6fedeaacb169bc89bc5186bd00741b4a/4chan-pixeldrain.user.js // ==/UserScript== (async function () { const RE_PIXELDRAIN_URL_SUFFIX = /com\/\s?([a-z])\s?\/\s?([a-zA-Z0-9]{8})/; const RE_PIXELDRAIN_URL_PLAINTEXT = new RegExp(`(?:https?://)?pixeldrain\\s+\\.?${RE_PIXELDRAIN_URL_SUFFIX.source}`, 'gim'); const RE_PIXELDRAIN_URL_A = new RegExp(`href="https?://pixeldrain">https://pixeldrain\\s*${RE_PIXELDRAIN_URL_SUFFIX.source}`, 'gim'); const ids = {}; function processPost(post) { if (post.textContent.includes('pixeldrain') && (post.innerHTML.match(RE_PIXELDRAIN_URL_PLAINTEXT) || post.innerHTML.match(RE_PIXELDRAIN_URL_A))) { post.innerHTML = post.innerHTML.replaceAll(RE_PIXELDRAIN_URL_PLAINTEXT, `https://pixeldrain.com/$1/$2`); post.innerHTML = post.innerHTML.replaceAll(RE_PIXELDRAIN_URL_A, `href="https://pixeldrain.com/$1/$2">https://pixeldrain.com/$1/$2`); } } function* getPosts(root) { for (const post of Array.from(root.querySelectorAll('.postMessage')).reverse()) { if (post.id in ids) { continue; } ids[post.id] = true; yield post; } } function processAllPosts(root) { for (const post of getPosts(root)) { processPost(post); } } window.addEventListener('load', async () => { setTimeout(async () => { processAllPosts(document.body); }, 5000); }); document.addEventListener('PostsInserted', async (evt) => { processAllPosts(evt.target); }); })();