diff mandarake-direct-link.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/mandarake-direct-link.user.js	Sun Jan 15 23:24:03 2023 +0900
+++ b/mandarake-direct-link.user.js	Sun Jan 15 23:33:41 2023 +0900
@@ -1,6 +1,6 @@
 // ==UserScript==
 // @name         mandarake direct link
-// @namespace    https://myconan.net
+// @namespace    https://nanaya.net
 // @version      2.0.1
 // @description  Make proper link on mandarake pages
 // @author       nanaya
@@ -13,43 +13,47 @@
 
 'use strict';
 
-const r18ConfirmLink = '#adult_confirm';
-function fixR18Link (link) {
-  if (!(link instanceof window.HTMLAnchorElement) || link.getAttribute('href') !== r18ConfirmLink) return;
+function directLink () {
+  const r18ConfirmLink = '#adult_confirm';
+  function fixR18Link (link) {
+    if (!(link instanceof window.HTMLAnchorElement) || link.getAttribute('href') !== r18ConfirmLink) return;
+
+    link.setAttribute('href', `/order/detailPage/item?itemCode=${link.id}`);
+    link.removeAttribute('class');
+  }
 
-  link.setAttribute('href', `/order/detailPage/item?itemCode=${link.id}`);
-  link.removeAttribute('class');
-}
+  function removeR18Mark (node) {
+    if (node.classList.contains('r18mark')) {
+      node.remove();
+      return true;
+    }
 
-function removeR18Mark (node) {
-  if (node.classList.contains('r18mark')) {
-    node.remove();
-    return true;
+    for (const mark of node.querySelectorAll('.r18mark')) {
+      mark.remove();
+    }
   }
 
-  for (const mark of node.querySelectorAll('.r18mark')) {
-    mark.remove();
+  function run (node) {
+    if (!(node instanceof window.HTMLElement)) return;
+    if (removeR18Mark(node)) return;
+
+    fixR18Link(node);
+    for (const link of node.querySelectorAll(`a[href='${r18ConfirmLink}']`)) {
+      fixR18Link(link);
+    }
   }
+
+  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;
-  if (removeR18Mark(node)) return;
-
-  fixR18Link(node);
-  for (const link of node.querySelectorAll(`a[href='${r18ConfirmLink}']`)) {
-    fixR18Link(link);
-  }
-}
-
-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);
+directLink();