Switch CharacterID

Switch Characters ID by Ctrl + arrow buttons in panel.php

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Switch CharacterID
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Switch Characters ID by Ctrl + arrow buttons in panel.php
// @author       grin3671
// @match        https://myanimelist.net/panel.php*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    window.addEventListener('keyup', (event) => {
      // No extra action w/o modifier
      if (event.getModifierState('Control')) {
        let params, linkID, newCID;
        // Get current url parameters
        params = new URLSearchParams(document.location.search.substring(1));
        // Get current Character ID (CID)
        linkID = parseInt(params.get('character_id'), 10);
        // Open character page if pressed Arrow UP
        if (!isNaN(linkID) && event.code === 'ArrowUp') {
          // NOTE: "href" create new history entry so we can return to panel.php
          window.location.href = 'https://myanimelist.net/character/' + linkID.toString();
        }
        // Set new CID but don't change it if used any other button
        newCID = event.code === 'ArrowLeft' ? linkID - 1 : event.code === 'ArrowRight' ? linkID + 1 : linkID;
        if (!isNaN(newCID) && linkID !== newCID) {
          // Set new url parameters
          params.set('character_id', newCID);
          // Change current url
          // NOTE: "replace" doesn't create new history entry so we can return to start point faster
          window.location.replace(window.location.origin + window.location.pathname + '?' + params.toString());
        }
      }
    });
})();