changeset 103:b2d0b37f945f

Update mandarake script - update download url - remove iife - run at document start - use observer - remove search link fixer (seems to be fixed already)
author nanaya <me@nanaya.net>
date Sat, 24 Dec 2022 22:10:30 +0900
parents 3fded109e23a
children 93e21738b588
files mandarake-direct-link.user.js
diffstat 1 files changed, 32 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/mandarake-direct-link.user.js	Sat Dec 24 21:33:30 2022 +0900
+++ b/mandarake-direct-link.user.js	Sat Dec 24 22:10:30 2022 +0900
@@ -1,43 +1,51 @@
 // ==UserScript==
 // @name         mandarake direct link
 // @namespace    https://myconan.net
-// @version      1.3.6
+// @version      2.0.0
 // @description  Make proper link on mandarake pages
 // @author       nanaya
 // @match        https://order.mandarake.co.jp/*
 // @match        http://order.mandarake.co.jp/*
 // @grant        none
-// @downloadURL  https://hg.myconan.net/ec-userscripts/raw-file/tip/mandarake-direct-link.user.js
+// @run-at       document-start
+// @downloadURL  https://hg.nanaya.net/ec-userscripts/raw-file/tip/mandarake-direct-link.user.js
 // ==/UserScript==
 
-/* global $ */
-;(function () {
-  'use strict'
+'use strict'
+
+const r18ConfirmLink = '#adult_confirm'
+function fixR18Link (link) {
+  if (!(link instanceof window.HTMLAnchorElement) || link.getAttribute('href') !== r18ConfirmLink) return
 
-  $('.r18mark').remove()
-  $(".r18item ~ a[href^='#adult_confirm']").remove()
+  link.setAttribute('href', `/order/detailPage/item?itemCode=${link.id}`)
+  link.removeAttribute('class')
+}
 
-  const fixR18Link = function (_i, el) {
-    const url = `/order/detailPage/item?itemCode=${el.id}`
-
-    el.setAttribute('href', url)
-    el.setAttribute('class', '')
+function removeR18Mark (node) {
+  if (node.classList.contains('r18mark')) {
+    node.remove()
+    return true
   }
 
-  $("[href^='#adult_confirm']").each(fixR18Link)
+  for (const mark of node.querySelectorAll('.r18mark')) {
+    mark.remove()
+  }
+}
 
-  const currentQuery = new URLSearchParams(window.location.search)
-  const fixSearchLink = function (_i, el) {
-    const query = new URLSearchParams(el.search)
+function onMutate (mutations) {
+  for (const mutation of mutations) {
+    for (const node of mutation.addedNodes) {
+      if (node instanceof window.HTMLElement) {
+        if (removeR18Mark(node)) continue
 
-    for (const [key, value] of currentQuery) {
-      if (!query.has(key)) {
-        query.set(key, value)
+        fixR18Link(node)
+        for (const link of node.querySelectorAll(`a[href='${r18ConfirmLink}']`)) {
+          fixR18Link(link)
+        }
       }
     }
+  }
+}
 
-    el.setAttribute('href', `/order/listPage/list?${query.toString()}`)
-  }
-
-  $("[href^='/order/listPage/list?']").each(fixSearchLink)
-}).call()
+const observer = new window.MutationObserver(onMutate)
+observer.observe(document, { childList: true, subtree: true })