diff pixiv-fanbox-unlazy.user.js @ 117:d9dc190bccaf

Undo combining fixes into single script per site
author nanaya <me@nanaya.net>
date Sun, 15 Jan 2023 23:49:35 +0900
parents ef21ef445fc6
children 8de2d53a4cb1
line wrap: on
line diff
--- a/pixiv-fanbox-unlazy.user.js	Sun Jan 15 23:46:56 2023 +0900
+++ b/pixiv-fanbox-unlazy.user.js	Sun Jan 15 23:49:35 2023 +0900
@@ -12,54 +12,50 @@
 
 'use strict';
 
-function unlazy () {
-  const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/';
+const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/';
+
+function disableEventLink (event) {
+  event.stopPropagation();
+}
 
-  function disableEventLink (event) {
-    event.stopPropagation();
+function fix (link) {
+  const href = link.href;
+
+  // basic sanity check
+  if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) {
+    return;
   }
 
-  function fix (link) {
-    const href = link.href;
+  // don't run again if already run on passed link
+  if (link._ecUserscript) {
+    return;
+  }
+  link._ecUserscript = true;
 
-    // basic sanity check
-    if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) {
-      return;
-    }
-
-    // don't run again if already run on passed link
-    if (link._ecUserscript) {
-      return;
-    }
-    link._ecUserscript = true;
+  link.addEventListener('click', disableEventLink);
+  const image = document.createElement('img');
+  image.style.width = '100%';
+  image.src = href;
+  link.replaceChildren(image);
+}
 
-    link.addEventListener('click', disableEventLink);
-    const image = document.createElement('img');
-    image.style.width = '100%';
-    image.src = href;
-    link.replaceChildren(image);
-  }
+function run (node) {
+  if (!(node instanceof window.HTMLElement)) return;
 
-  function run (node) {
-    if (!(node instanceof window.HTMLElement)) return;
+  fix(node);
+  for (const link of node.querySelectorAll(`[href^="${imageUrlPrefix}"]`)) {
+    fix(link);
+  }
+}
 
-    fix(node);
-    for (const link of node.querySelectorAll(`[href^="${imageUrlPrefix}"]`)) {
-      fix(link);
+function onMutate (mutations) {
+  for (const mutation of mutations) {
+    for (const node of mutation.addedNodes) {
+      run(node);
     }
   }
-
-  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();
+const observer = new window.MutationObserver(onMutate);
+observer.observe(document, { childList: true, subtree: true });
+run(document.body);