Serial Code Tracker

Save and mark serial codes as used when accessed.

Από την 06/01/2025. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name           Serial Code Tracker
// @description    Save and mark serial codes as used when accessed.
// @description:ja アクセスされたシリアルコードを保存し、使用済みとしてマークします。
// @author         Ginoa AI
// @namespace      https://greatest.deepsurf.us/ja/users/119008-ginoaai
// @version        1.0
// @match          https://www.hoyolab.com/article/*
// @grant          GM_setValue
// @grant          GM_getValue
// @grant          GM_listValues
// @icon           https://pbs.twimg.com/profile_images/1648150443522940932/4TTHKbGo_400x400.png
// ==/UserScript==

// 保存されているコードの一覧を取得
function getSavedCodes() {
  return new Set(GM_listValues()); // Setを使って効率化
}

// テキスト置換用の関数
function updateLinkText(node) {
  const savedCodes = getSavedCodes(); // 必要時に最新状態を取得
  const links = node.querySelectorAll('a'); // 対象のaタグを取得
  links.forEach((link) => {
    if (link.href.includes('code=')) {
      const url = new URL(link.href);
      const code = url.searchParams.get('code'); // codeパラメータを取得

      if (savedCodes.has(code)) {
        link.textContent = 'アクセス済み'; // テキストを置換
      }
    }
  });
}

// 指定要素が存在するまで待機
function waitForElement(selector, callback) {
  const element = document.querySelector(selector);
  if (element) {
    callback(element);
  } else {
    const observer = new MutationObserver(() => {
      const element = document.querySelector(selector);
      if (element) {
        observer.disconnect();
        callback(element);
      }
    });
    observer.observe(document.body, { childList: true, subtree: true });
  }
}

// クリックイベントでcodeを保存(ホイールクリック対応)
document.addEventListener('mousedown', function (event) {
  if (event.button === 0 || event.button === 1) { // 左クリック (0) またはホイールクリック (1)
    const target = event.target.closest('a'); // aタグをクリックしたか確認
    if (target && target.href.includes('code=')) {
      const url = new URL(target.href);
      const code = url.searchParams.get('code'); // codeパラメータを取得
      if (code) {
        GM_setValue(code, true); // codeを保存
        console.log(`Saved code: ${code}`);

        // 保存後に即時置換処理を実行
        waitForElement('div[class="mhy-article-page__content"]', (ost) => {
          updateLinkText(ost);
        });
      }
    }
  }
});

// メイン処理
waitForElement('div[class="mhy-article-page__content"]', (ost) => {
  // 初期ロード時にテキストを置換
  updateLinkText(ost);

  // DOM変更を監視して必要な部分のみ処理
  const observer = new MutationObserver((mutations) => {
    for (const mutation of mutations) {
      for (const addedNode of mutation.addedNodes) {
        if (addedNode.nodeType === Node.ELEMENT_NODE) {
          updateLinkText(addedNode); // 追加された要素を処理
        }
      }
    }
  });

  observer.observe(ost, { childList: true, subtree: true }); // 必要に応じてsubtreeをtrueに
  console.log('Saved Codes:', getSavedCodes()); // デバッグ用
});