Ruby reference manaul version switcher

Switch version of Ruby reference manual by key shortcut

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name          Ruby reference manaul version switcher
// @name:ja      るりまバージョンスイッチャー for Rubyリファレンスマニュアル
// @namespace     https://greasyfork.org/ja/users/570127
// @version       2025.12.20.0
// @description    Switch version of Ruby reference manual by key shortcut
// @description:ja  るりまのバージョンをキーボードのショートカットで切り替えます
// @author        universato
// @match         https://docs.ruby-lang.org/ja/*/*.html
// @match         https://docs.ruby-lang.org/en/*/*.html
// @icon          https://www.google.com/s2/favicons?sz=64&domain=ruby-lang.org
// @license       MIT
// @grant         none
// ==/UserScript==

// en: master, 2.0.0 ~ 2.7.0, 3.0 ~
// ja: en + latest, 1.8.7, 1.9.3

// '1.9' -> '1.9.3'
// '2.6' -> '2.6.0'
// '3.1' -> '3.1'
function ruremaVersion(version){
    if(version.match(/1\.[0-8]/)){ return '1.8.7'; }
    else if(version === '1.9'){ return '1.9.3'; }
    else if(version.match(/2\.[0-7]/)){ return version + '.0'; }
    else if(version.match(/2\.[7-9]/)){ return '2.7.0'; } // Ruby 2.7 is the last 2.x version.
    else if(version.match(/3\.[0-5]/)){ return version; }
    else{ return 'master'; }
}

// Reference table for Ruby versions (not used directly)
const RUREMA_VERSIONS = {
    1.8: '1.8.7',
    1.9: '1.9.7',
    2.0: '2.0.0',
    2.1: '2.1.0',
    2.2: '2.2.0',
    2.3: '2.3.0',
    2.4: '2.4.0',
    2.5: '2.5.0',
    2.6: '2.6.0',
    2.7: '2.7.0',
    2.8: '2.7.0',
    2.9: '2.7.0',
    3.0: '3.0',
    3.1: '3.1',
    3.2: '3.2',
    3.3: '3.3',
    3.4: '3.4',
    3.5: '3.5',
};

// Shortcuts to switch version by numeric key
(function() {
  let keyInput = "";

  document.addEventListener('keydown', function (event) {
      const activeTagName = document.activeElement.tagName;
      const inTextarea = ['TEXTAREA', 'INPUT'].includes(activeTagName);
      const invalidInput = event.key.match(/[^0-9.lm]/);

      // テキストエリア内の入力、数字・ドット以外の入力であれば、終了。
      if(inTextarea || invalidInput){
          keyInput = '';
          return;
      }

      if(event.ctrlKey || event.metaKey){ return; }

      // 入力はいったん最後の3文字以内にする
      keyInput = (keyInput + event.key).slice(-3);

      // この時点で、入力は2文字か3文字。
      // 最後の入力(末尾2桁)が数字なら、ドット付きのマイナーバージョンまでの形にする。
      if(keyInput.match(/[0-9][0-9]$/)){
          const n = keyInput.length;
          keyInput = keyInput[n - 2] + '.' + keyInput[n - 1];
      }

      console.log(`[version switcher]: ${keyInput}`);

      const paths = location.pathname.split('/');
      const language = paths[1];
      const currentVersion = paths[2];

      // バージョンを期待するところ、数字・ドット以外の文字があれば終了する。
      // Expect version segment: master | latest | x.y(.z)
      if(currentVersion === 'master'){}
      else if(currentVersion === 'latest'){}
      else if(currentVersion.match(/[1-9]\.[0-9](\.[0-9])?/)){}
      else{
          console.log("[version switcher] not version url");
          keyInput = '';
          return;
      }

      // 英語版ドキュメントはversion 1.xがなさそうなので、終了する。
      if(language === 'en' && keyInput.match(/1\.[0-9]/)){
          console.log("[version switcher] en don't support 1.x ");
          return;
      }

      // 1.0 ~ 4.9のときに限り、バージョンを変更する。
      let newVersion;
      if(keyInput.match(/[1-4]\.[0-9]/)){
          newVersion = ruremaVersion(keyInput);
      }else if(event.key === 'l' && language === 'ja'){
          newVersion = 'latest';
      }else if(event.key === 'm'){
          newVersion = 'master';
      }

      if(newVersion){
          location.href = location.href.replace(currentVersion, newVersion);
      }

  }, false);
})();