Skip to content

Instantly share code, notes, and snippets.

@schockocraft
Created September 21, 2022 20:05
Show Gist options
  • Save schockocraft/490e9712ebdd9826165df86f1b128d22 to your computer and use it in GitHub Desktop.
Save schockocraft/490e9712ebdd9826165df86f1b128d22 to your computer and use it in GitHub Desktop.
MDN js code examples fix
// ==UserScript==
// @name MDN js code examples fix
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Fixes MDN code examples formatting glitch on javascript function's pages loading in background by automatically reloading the frame
// @author schockocraft
// @match https://developer.mozilla.org/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// ==/UserScript==
const stopListening = () => {
// blur happens before focus, we already had focus
console.log("page loaded in foreground -> removing event listener")
window.removeEventListener('focus', fixIFrame)
}
const fixIFrame = () => {
// focus happens before blur, we had no focus before
console.log("page loaded in background -> reloading potentially glitched iframes")
document.querySelector('h2#try_it + div > iframe[src*="/pages/js/"]').src += ''
window.removeEventListener('blur', stopListening)
}
window.addEventListener('blur', stopListening, { once: true })
window.addEventListener('focus', fixIFrame, { once: true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment