ニコニコ動画(Re:仮)説明文linkify

2024/6/21 13:43:27

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        ニコニコ動画(Re:仮)説明文linkify
// @namespace   net.ghippos.nicovideo.rekari.linkify
// @match       https://www.nicovideo.jp/watch_tmp/*
// @grant       none
// @version     1.1
// @author      mohemohe
// @description 2024/6/21 13:43:27
// @license     WTFPL
// ==/UserScript==

const linkify = async (description) => {
  for (const node of description.childNodes) {
    const content = node.textContent;
    if (!content) {
      continue;
    }
    const links = content.match(/([ns]m\d+)/g);
    if (links) {
      let replacedContent = content;
      const replaces = {};
      for (const link of links) {
        const exists = await fetch(`https://www.nicovideo.jp/api/watch/tmp/${link}?_frontendId=6&_frontendVersion=0.0.0`);
        replacedContent = replacedContent.replace(link, `<a href="https://www.nicovideo.jp/watch_tmp/${link}" style="color: #0969da; text-decoration: underline">${link}${exists.status == 200 ? "[公開中]" : "[非公開]"}</a>`);
      }
      const span = document.createElement("span");
      span.innerHTML = replacedContent;
      node.parentNode.replaceChild(span, node);
    }
  }
};

// NOTE: document.bodyでMutationObserverを試したけど他のサイトと違ってなんか動かなかった
const awaiter = setInterval(async () => {
  const description = document.querySelector("main p");
  if (description) {
    clearInterval(awaiter);
    await linkify(description);
  }
}, 100);