Skip to content

Instantly share code, notes, and snippets.

@catboxanon
Last active March 13, 2024 20:33
Show Gist options
  • Save catboxanon/6fedeaacb169bc89bc5186bd00741b4a to your computer and use it in GitHub Desktop.
Save catboxanon/6fedeaacb169bc89bc5186bd00741b4a to your computer and use it in GitHub Desktop.
Parse pixeldrain URLs on 4chan
// ==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</a>\\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, `<a class="linkify" rel="noreferrer noopener" target="_blank" href="https://pixeldrain.com/$1/$2">https://pixeldrain.com/$1/$2</a>`);
post.innerHTML = post.innerHTML.replaceAll(RE_PIXELDRAIN_URL_A, `href="https://pixeldrain.com/$1/$2">https://pixeldrain.com/$1/$2</a>`);
}
}
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);
});
})();

4chan pixeldrain URL parser

This userscript automatically parses pixeldrain links into valid (clickable) <a> elements.

Prerequisites

You will need both a userscript extension and 4chanX.

The Chrome extension for 4chanX does not work with this userscript. This is likely due to the Chrome extension not firing the required Custom Events for the script to function, either due to a bug, the extension not being up-to-date, or some other unknown factor (I haven't bothered testing since I do not use Chrome). However, you can easily migrate to the userscript version of 4chanX by using the Export and Import feature in the top right of the 4chanX settings panel to migrate your configuration.

Installation

Open this URL: https://gist.github.com/raw/6fedeaacb169bc89bc5186bd00741b4a/4chan-pixeldrain.user.js

If you have an open tab on 4chan, refresh it.

If you do not recieve an install dialog, you do not have a userscript extension, or it is not functioning properly.

Notes

Currently, posts that this userscript are applied to cause the ability to hover over/click replies (to see them inline) no longer function. This is due to how the script currently functions, by editing the HTML directly, which removes all event listeners within the DOM for that given HTML.

This is personally not a huge dealbreaker for me and I don't intend to fix it currently, so just be aware this is a known issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment