Simplified Chinese Characters For Wiktionary

When using the English Wiktionary, the search for Simplified Chinese Characters will always suggest the user to navigate to the corresponding Traditional Chinese Characters page. So we automatically redirect the user.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Simplified Chinese Characters For Wiktionary
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  When using the English Wiktionary, the search for Simplified Chinese Characters will always suggest the user to navigate to the corresponding Traditional Chinese Characters page. So we automatically redirect the user.
// @author       moraesvic
// @source       https://gist.github.com/moraesvic/5a8ff36c17fcd3feace5c5d6369c5583
// @match        http*://*.wiktionary.org/*
// @license      GNU GPLv3
// @grant        none
// ==/UserScript==

/**
 * This script has been published to Greasy Fork at
 * https://greasyfork.org/en/scripts/484991-simplified-chinese-characters-for-wiktionary
 *
 * It can be added as a user script to browsers like Firefox and Chrome, by adding it to a browser extension
 * like TamperMonkey, GreaseMonkey, or ViolentMonkey.
 */

"use strict";

const DEBUG = true;

const debug = (...args) => {
  if (DEBUG) {
    console.debug("[simplified-chinese]", ...args);
  }
};

/**
 * @param {string} x
 */
const $$ = (x) => Array.from(document.querySelectorAll(x));

/**
 * @param {Document} document The current value of `document`
 * @returns {boolean}
 */
const hasChineseEntry = (document) => {
  return document.querySelector("#Chinese") !== null;
};

/**
 * @param {Document} document The current value of `document`
 * @returns {string | null}
 */
const getNewHref = (document) => {
  debug("Starting.");

  if (!hasChineseEntry(document)) {
    debug("Page has no entry for Chinese, leaving.");
    return null;
  }

  const href = $$("i")
    .find((x) => x.innerHTML.startsWith("This term is the simplified form of"))
    ?.nextElementSibling?.querySelector("a")?.href;

  if (href === undefined) {
    debug("Can't find traditional form, leaving.");
    return null;
  }

  return href;
};

const main = () => {
  const href = getNewHref(document);

  if (href === null) {
    return;
  }

  debug("Assigning new location", href);
  location.assign(href);
};

main();