Skip to content

Instantly share code, notes, and snippets.

@rf5860
Last active December 28, 2023 13:16
Show Gist options
  • Save rf5860/835274248c984b7fc59ddc166102f41d to your computer and use it in GitHub Desktop.
Save rf5860/835274248c984b7fc59ddc166102f41d to your computer and use it in GitHub Desktop.
Expands all code blocks automatically on the AHK forums
// ==UserScript==
// @name AHKForums_ExpandCode
// @description Expands all code blocks automatically on the AHK forums
// @version 0.5
// @namespace https://www.autohotkey.com/boards
// @author rjf89
// @updateURL https://gist.githubusercontent.com/rf5860/835274248c984b7fc59ddc166102f41d/raw/589d4f5d835e6119d5b78b3e83d2862acb232c01/AHKForums_ExpandAllCode.user.js
// @downloadURL https://gist.githubusercontent.com/rf5860/835274248c984b7fc59ddc166102f41d/raw/589d4f5d835e6119d5b78b3e83d2862acb232c01/AHKForums_ExpandAllCode.user.js
// @match https://www.autohotkey.com/boards/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
console.debug('>>> AHKEC: Script is running');
const dbgArgs = args => args.map(arg => typeof arg === 'function' ? dbg(arg) : arg);
const dbg = fn => {
return function (...args) {
// Find any arguments which are functions, and wrap them in dbg
args = dbgArgs(args)
console.debug(`>>> AHKEC: Calling function ${fn.name} with arguments:`, args);
const result = fn(...args);
console.debug(`>>> AHKEC: Function ${fn.name} returned:`, result);
return result;
};
};
const isLink = e => e.nodeName.toLowerCase() === 'a';
const isExpandable = e => e.getAttribute('onclick')?.startsWith('expandCode');
const isExpandCodeLink = e => isLink(e) && isExpandable(e);
const links = [...document.querySelectorAll('a')];
console.debug('>>> AHKEC: Found links:', links);
links.filter(isExpandCodeLink).forEach(e => e.click());
const observer = new MutationObserver(mutations => {
mutations.flatMap(mutation => [...mutation.addedNodes])
.filter(isExpandCodeLink)
.forEach(e => e.click());
});
observer.observe(document.body, { childList: true, subtree: true });
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment