Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Structure:
- h3h3-swap/
- ├─ manifest.json
- ├─ content.js
- 1. Open chrome://extensions.
- 2. Toggle Developer mode (top-right).
- 3. Click Load unpacked and select your h3h3-swap/ folder.
- manifest.json
- {
- "manifest_version": 3,
- "name": "H3H3 Name Swap",
- "description": "On r/h3h3productions, replace 'Ethan' / 'Ethan Klein' with 'Pedo_Troll'.",
- "version": "1.0.0",
- "content_scripts": [
- {
- "matches": [
- "https://www.reddit.com/r/h3h3productions/*",
- "https://old.reddit.com/r/h3h3productions/*",
- "https://www.reddit.com/r/H3H3Productions/*",
- "https://old.reddit.com/r/H3H3Productions/*"
- ],
- "js": ["content.js"],
- "run_at": "document_idle"
- }
- ]
- }
- content.js
- const RE_FULL = /\bEthan Klein\b/g;
- const RE_FIRST = /\bEthan\b/g;
- const REPLACEMENT = "Pedo_Troll";
- const SKIP_TAGS = new Set(["SCRIPT", "STYLE", "NOSCRIPT", "CODE", "PRE", "TEXTAREA"]);
- function isEditable(el) {
- if (!el || el === document.body) return false;
- if (el.nodeType !== 1) return isEditable(el.parentNode);
- const tag = el.tagName;
- if (SKIP_TAGS.has(tag)) return true;
- const ce = el.getAttribute("contenteditable");
- if (ce === "" || ce === "true") return true;
- if (tag === "INPUT") return true;
- return isEditable(el.parentNode);
- }
- function replaceInTextNode(node) {
- const original = node.nodeValue;
- if (!original || original.indexOf("Ethan") === -1) return;
- let updated = original.replace(RE_FULL, REPLACEMENT).replace(RE_FIRST, REPLACEMENT);
- if (updated !== original) {
- node.nodeValue = updated;
- }
- }
- function walkAndReplace(root) {
- const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
- acceptNode(node) {
- if (isEditable(node.parentNode)) return NodeFilter.FILTER_REJECT;
- if (!node.nodeValue || !node.nodeValue.trim()) return NodeFilter.FILTER_REJECT;
- return NodeFilter.FILTER_ACCEPT;
- }
- });
- const toProcess = [];
- let n;
- while ((n = walker.nextNode())) toProcess.push(n);
- toProcess.forEach(replaceInTextNode);
- }
- function init() {
- walkAndReplace(document.body);
- const mo = new MutationObserver((mutations) => {
- for (const m of mutations) {
- if (m.type === "childList") {
- m.addedNodes.forEach((node) => {
- if (node.nodeType === 1) {
- if (!isEditable(node)) walkAndReplace(node);
- } else if (node.nodeType === 3) {
- replaceInTextNode(node);
- }
- });
- } else if (m.type === "characterData" && m.target.nodeType === 3) {
- replaceInTextNode(m.target);
- }
- }
- });
- mo.observe(document.body, {
- subtree: true,
- childList: true,
- characterData: true
- });
- }
- if (document.readyState === "loading") {
- document.addEventListener("DOMContentLoaded", init);
- } else {
- init();
- }
Advertisement
Add Comment
Please, Sign In to add comment