Wikipedia article name with language

In the 'Languages' list in the left sidebar, the language name linking to the same article in that language is complemented with the name of that article.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Wikipedia article name with language
// @version     1.0.2
// @namespace   http://www.agj.cl/
// @description In the 'Languages' list in the left sidebar, the language name linking to the same article in that language is complemented with the name of that article.
// @license     Unlicense
// @include     http*://*.wikipedia.org/wiki/*
// @grant       none
// ==/UserScript==

(function () {
  "use strict";

  console.log("Executing Wikipedia article name with language.");

  // Utilities.

  const sel = document.querySelector.bind(document);
  const selAll = document.querySelectorAll.bind(document);
  const eq = (a) => (b) => a === b;
  const test = (regex) => (text) => regex.test(text);
  const testOn = (text) => (regex) => regex.test(text);
  const prepend = (a) => (b) => a + b;
  const makeEl = (tag, attrs, ...content) => {
    const el = document.createElement(tag);
    if (attrs)
      Object.keys(attrs).forEach((attr) => el.setAttribute(attr, attrs[attr]));
    content
      .map((obj) =>
        typeof obj === "string" ? document.createTextNode(obj) : obj,
      )
      .forEach((node) => el.appendChild(node));
    return el;
  };
  const pipe = (...fs) =>
    fs.reduce(
      (left, right) =>
        (...args) =>
          right(left(...args)),
    );
  const uniq = (list) => {
    const seen = [];
    return list.filter((item) =>
      seen.some(eq(item)) ? false : (seen.push(item), true),
    );
  };
  const not =
    (f) =>
    (...args) =>
      !f(...args);

  //

  const elements = Array.from(selAll(".interlanguage-link"));

  elements.forEach((el) => {
    const a = el.getElementsByTagName("a")[0];
    const langName = a.textContent;
    const titleAndLanguage = a.getAttribute("title");
    const titleMatcher = [
      /(.+) – .+/, // English, etc.
      /(.+) +\(.+\)/, // Spanish, etc.
      /.+: (.+)/, // Japanese, etc.
    ].find(testOn(titleAndLanguage));
    const title = titleAndLanguage.replace(titleMatcher, "$1");
    a.textContent = "";
    a.appendChild(makeEl("span", { class: "language-name" }, langName));
    a.appendChild(document.createTextNode(" "));
    a.appendChild(makeEl("span", { class: "article-title" }, title));
  });

  // Styles.

  sel("head").appendChild(
    makeEl(
      "style",
      null,
      `
		.interlanguage-link .language-name {
			display: block;
			text-transform: uppercase;
			font-size: 0.7em;
		}
		`,
    ),
  );
})();