Mercurial > ec-userscripts
diff melonbooks-unlazy.user.js @ 109:ef21ef445fc6
Cleanups
- wrap everything in function and call them last
- fix namespace and update url
author | nanaya <me@nanaya.net> |
---|---|
date | Sun, 15 Jan 2023 23:33:41 +0900 |
parents | 2c4470b73ad9 |
children | d9dc190bccaf |
line wrap: on
line diff
--- a/melonbooks-unlazy.user.js Sun Jan 15 23:24:03 2023 +0900 +++ b/melonbooks-unlazy.user.js Sun Jan 15 23:33:41 2023 +0900 @@ -12,35 +12,39 @@ 'use strict'; -function fix (image) { - if (!image.classList.contains('lazyload')) return; +function unlazy () { + function fix (image) { + if (!image.classList.contains('lazyload')) return; + + const src = image.dataset.src; + + if (src == null || src === '') return; - const src = image.dataset.src; + image.classList.remove('lazyload'); + image.src = image.dataset.src; + delete image.dataset.src; + } + + function run (node) { + if (!(node instanceof window.HTMLElement)) return; - if (src == null || src === '') return; + fix(node); + for (const image of node.querySelectorAll('.lazyload')) { + fix(image); + } + } - image.classList.remove('lazyload'); - image.src = image.dataset.src; - delete image.dataset.src; + function onMutate (mutations) { + for (const mutation of mutations) { + for (const node of mutation.addedNodes) { + run(node); + } + } + } + + const observer = new window.MutationObserver(onMutate); + observer.observe(document, { childList: true, subtree: true }); + run(document.body); } -function run (node) { - if (!(node instanceof window.HTMLElement)) return; - - fix(node); - for (const image of node.querySelectorAll('.lazyload')) { - fix(image); - } -} - -function onMutate (mutations) { - for (const mutation of mutations) { - for (const node of mutation.addedNodes) { - run(node); - } - } -} - -const observer = new window.MutationObserver(onMutate); -observer.observe(document, { childList: true, subtree: true }); -run(document.body); +unlazy();