Arkrec Wiki Assistant

Automatically query if this video has been in the arkrec wiki database on video loading

目前為 2021-10-04 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Arkrec Wiki Assistant
// @name:zh-CN   明日方舟少人WIKI辅助插件
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Automatically query if this video has been in the arkrec wiki database on video loading
// @description:zh-CN  在打开视频时自动判断是否已上传至少人WIKI数据库,并提供上传捷径链接
// @author       philimao
// @match        https://www.bilibili.com/video/BV*
// @icon         https://www.google.com/s2/favicons?domain=arkrec.wiki
// @grant        unsafeWindow
// ==/UserScript==

(async function () {
  const wikiBtn = document.createElement("div");

  // https://greasyfork.org/en/scripts/398655-%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9%E8%A7%86%E9%A2%91%E9%A1%B5%E9%9D%A2%E5%B8%B8%E9%A9%BB%E6%98%BE%E7%A4%BAav-bv%E5%8F%B7-%E5%B7%B2%E5%AE%8C%E5%85%A8%E9%87%8D%E6%9E%84-%E6%94%AF%E6%8C%81%E6%98%BE%E7%A4%BA%E5%88%86p%E6%A0%87%E9%A2%98
  async function playerReady() {
    let counter = 0;
    return new Promise((resolve, reject) => {
      const id = setInterval(() => {
        if (
          unsafeWindow.player &&
          unsafeWindow.player.isInitialized &&
          unsafeWindow.player.isInitialized()
        ) {
          resolve(clearInterval(id));
        }
        if (counter > 100) {
          reject(clearInterval(id));
        }
        counter++;
      }, 100);
    });
  }

  async function injection() {
    const elem = document.querySelector("[data-inject='arkrec']");
    if (elem) elem.remove();

    let host = "https://arkrec.wiki";
    // host = "http://127.0.0.1:3001";
    const { bvid, p } = unsafeWindow.__INITIAL_STATE__;
    const url =
      "https://www.bilibili.com/video/" + bvid + (p === 1 ? "" : "?p=" + p);
    const resRaw = await fetch(host + "/api/query-url", {
      method: "POST",
      mode: "cors",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ url }),
    });
    if (resRaw.ok) {
      console.log("Abort injecting");
    } else {
      document.querySelector(".video-toolbar").appendChild(wikiBtn);
      console.log("Wiki Icon Injecting", bvid, "Page", p);
    }
  }

  // https://greasyfork.org/en/scripts/398542-bilibili-%E6%98%BE%E7%A4%BA-av-%E5%8F%B7
  function registerObserver() {
    console.log("Register Re-Injection Listener");
    const target = unsafeWindow.__INITIAL_STATE__;
    let videoData = target.videoData;
    const desc = Object.getOwnPropertyDescriptor(target, "videoData");
    const vueHook = desc.set;
    Object.defineProperty(target, "videoData", {
      get: desc.get || (() => videoData),
      set(data) {
        videoData = data;
        injection();
        if (vueHook) vueHook.call(this, data);
      },
      enumerable: true,
      configurable: true,
    });
  }

  const wikiSvg =
    "data:image/svg+xml,%3Csvg%20id%3D%22svg%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20width%3D%22400%22%20height%3D%22176.24521072796932%22%20viewBox%3D%220%2C%200%2C%20400%2C176.24521072796932%22%20version%3D%221.1%22%3E%3Cg%20id%3D%22svgg%22%3E%3Cpath%20id%3D%22path0%22%20d%3D%22M0.000%2081.302%20C%200.000%20178.729%2C-0.539%20176.253%2C20.648%20176.239%20C%2035.296%20176.230%2C36.588%20174.889%2C57.999%20137.469%20C%2068.404%20119.283%2C77.715%20103.341%2C78.689%20102.042%20C%2080.149%20100.093%2C83.915%20105.722%2C100.158%20134.127%20C%20123.163%20174.358%2C124.902%20176.245%2C138.968%20176.245%20C%20148.294%20176.245%2C149.315%20175.895%2C154.176%20171.034%20L%20159.387%20165.824%20159.387%2082.912%20L%20159.387%200.000%20141.762%200.000%20L%20124.138%200.000%20124.138%2052.487%20L%20124.138%20104.974%20107.142%2075.475%20L%2090.146%2045.977%2080.303%2045.977%20L%2070.460%2045.977%2065.395%2054.023%20C%2062.610%2058.448%2C54.860%2071.635%2C48.173%2083.326%20L%2036.015%20104.583%2035.612%2052.292%20L%2035.209%200.000%2017.604%200.000%20L%200.000%200.000%200.000%2081.302%20M163.985%2088.123%20L%20163.985%20176.245%20180.843%20176.245%20L%20197.701%20176.245%20197.701%2088.123%20L%20197.701%200.000%20180.843%200.000%20L%20163.985%200.000%20163.985%2088.123%20M202.299%2088.123%20L%20202.299%20176.245%20213.374%20176.245%20L%20224.450%20176.245%20292.685%20108.065%20L%20360.920%2039.884%20361.424%2019.942%20L%20361.929%200.000%20344.183%200.000%20L%20326.437%200.000%20326.437%2012.654%20L%20326.437%2025.307%20281.992%2069.732%20L%20237.548%20114.156%20237.548%2057.078%20L%20237.548%200.000%20219.923%200.000%20L%20202.299%200.000%20202.299%2088.123%20M364.751%2088.123%20L%20364.751%20176.245%20382.375%20176.245%20L%20400.000%20176.245%20400.000%2088.123%20L%20400.000%200.000%20382.375%200.000%20L%20364.751%200.000%20364.751%2088.123%20M301.143%20102.688%20L%20288.948%20114.956%20307.692%20133.745%20L%20326.437%20152.535%20326.437%20164.390%20L%20326.437%20176.245%20344.061%20176.245%20L%20361.686%20176.245%20361.686%20159.487%20C%20361.686%20150.270%2C360.962%20140.787%2C360.078%20138.414%20C%20358.678%20134.659%2C316.353%2090.421%2C314.160%2090.421%20C%20313.708%2090.421%2C307.850%2095.942%2C301.143%20102.688%20%22%20stroke%3D%22none%22%20fill%3D%22%23000000%22%20fill-rule%3D%22evenodd%22%3E%3C%2Fpath%3E%3C%2Fg%3E%3C%2Fsvg%3E";
  wikiBtn.setAttribute("data-inject", "arkrec");
  wikiBtn.style.width = "5rem";
  wikiBtn.style.height = "1.8rem";
  wikiBtn.style.marginRight = "2rem";
  wikiBtn.style.float = "right";
  wikiBtn.style.backgroundImage = `url(${wikiSvg})`;
  wikiBtn.style.backgroundSize = "contain";
  wikiBtn.style.backgroundRepeat = "no-repeat";
  wikiBtn.style.backgroundPosition = "center";
  wikiBtn.style.filter = "invert(0.4)";
  wikiBtn.style.transform = "translateY(2px)";
  wikiBtn.style.cursor = "pointer";

  const toast = document.querySelector("div");
  toast.id = "arkrec_toast";
  toast.innerText = "复制成功";
  toast.style.width = "80px";
  toast.style.height = "30px";
  toast.style.position = "absolute";
  toast.style.left = 0;
  toast.style.top = 0;
  toast.style.backgroundColor = "black";
  toast.style.color = "white";
  toast.style.fontSize = "13px";
  toast.style.textAlign = "center";
  toast.style.lineHeight = "30px";
  toast.style.opacity = 0;
  toast.style.display = "none";
  toast.style.borderRadius = "4px";
  toast.style.transition = "all 0.3s ease-in-out";
  toast.style.zIndex = 3000;

  wikiBtn.addEventListener("click", (evt) => {
    evt.preventDefault();
    const { bvid, p } = unsafeWindow.__INITIAL_STATE__;
    const url =
      "https://www.bilibili.com/video/" + bvid + (p === 1 ? "" : "?p=" + p);
    navigator.clipboard.writeText(url).catch((err) => {
      throw err !== undefined
        ? err
        : new DOMException("The request is not allowed", "NotAllowedError");
    });
    toast.style.display = "block";
    toast.style.opacity = 1;
    toast.style.transform = `translate(${evt.x - 40}px, ${evt.y - 40}px)`;
    setTimeout(() => {
      toast.style.opacity = 0;
      setTimeout(() => {
        toast.style.display = "none";
      }, 300);
    }, 800);
  });

  wikiBtn.addEventListener("contextmenu", (evt) => {
    evt.preventDefault();
    const { bvid, p } = unsafeWindow.__INITIAL_STATE__;
    window.open(
      "https://arkrec.wiki/submit?bvid=" + bvid + (p === 1 ? "" : "&p=" + p)
    );
  });

  wikiBtn.addEventListener("mouseenter", () => {
    wikiBtn.style.filter =
      "invert(1) brightness(0.5) sepia(1) hue-rotate(-194deg) saturate(3)";
  });
  wikiBtn.addEventListener("mouseleave", () => {
    wikiBtn.style.filter = "invert(0.4)";
  });

  await playerReady();
  await injection();
  registerObserver();
  document.querySelector("body").append(toast);
})();