Last active
September 22, 2024 13:29
-
-
Save rf5860/835274248c984b7fc59ddc166102f41d to your computer and use it in GitHub Desktop.
Expands all code blocks automatically on the AHK forums
This file contains hidden or 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 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