zhconvert's credit generator

快速產生可以貼入註解的銘謝文字。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           zhconvert's credit generator
// @name:en        zhconvert's credit generator
// @description    快速產生可以貼入註解的銘謝文字。
// @description:en Quickly generate the credit text for comment.
// @namespace      http://github.com/pan93412
// @version        1.0
// @description    try to take over the world!
// @author         pan93412
// @match          https://zhconvert.org/
// @icon           https://www.google.com/s2/favicons?sz=64&domain=zhconvert.org
// @grant          none
// @license        AGPL-3.0-or-later
// ==/UserScript==

/**
 * @returns {boolean} Succeed?
 */
function pushButton() {
  const globalId = "--credit-adder-get-credit-text-button";

  const actionButtonSet = document.querySelector(
    "#portlet-convert-summary .panel-heading div.actions",
  );
  if (!actionButtonSet) return false;

  // Create an action button
  const copyCreditButton = document.createElement("a");
  copyCreditButton.id = globalId;
  copyCreditButton.className = "btn btn-xs grey with-icon";
  copyCreditButton.innerHTML += '<i class="fa-solid fa-share"></i>';
  copyCreditButton.innerHTML += '<span class="text">取得 credit 文字</span>';

  // Add an event listener for the template.
  copyCreditButton.href = "javascript:__getCreditTextHandler()";

  actionButtonSet.append(copyCreditButton);
  return true;
}

(function () {
  "use strict";

  // Workaround to unable to addEventListener :(
  window.__getCreditTextHandler = function () {
    const dictVersion = window
      .$(`tr:has(>th:contains("後端程式碼版本")) + tr > td`)
      .text()
      .trim();
    const date = new Date().toLocaleString("en-UK");

    const creditText = `Processed by 繁化姬 ${dictVersion} @ ${date} | https://zhconvert.org`;

    // Copy to clipboard.
    console.log("Copied: %s", creditText);
    navigator.clipboard
      .writeText(creditText)
      .then(console.log)
      .catch(console.error);
  };

  const mutationObserver = new MutationObserver(function (_, o) {
    console.log("Triggered %s", this);
    const succeed = pushButton();
    if (succeed) {
      o.disconnect();
      console.log("Process succeed.");
    } else {
      console.log("Process failed.");
    }
  });

  console.log(mutationObserver);
  mutationObserver.observe(document, { subtree: true, childList: true });
})();