Annict Work Links

Add links to "Shoboi Calendar", "MyAnimeList" and "AniList" on Annict works page.

目前為 2022-08-31 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            Annict Work Links
// @namespace       https://spica.starry.blue/
// @version         0.2.3
// @author          SlashNephy <[email protected]>
// @description     Add links to "Shoboi Calendar", "MyAnimeList" and "AniList" on Annict works page.
// @description:ja  Annict の作品ページに「しょぼいカレンダー」「MyAnimeList」「AniList」へのリンクを追加します。
// @homepage        https://scrapbox.io/slashnephy/Annict_%E3%81%AE%E4%BD%9C%E5%93%81%E3%83%9A%E3%83%BC%E3%82%B8%E3%81%AB%E5%90%84%E7%A8%AE%E3%82%B5%E3%82%A4%E3%83%88%E3%81%B8%E3%81%AE%E3%83%AA%E3%83%B3%E3%82%AF%E3%82%92%E8%A1%A8%E7%A4%BA%E3%81%99%E3%82%8B_UserScript
// @homepageURL     https://scrapbox.io/slashnephy/Annict_%E3%81%AE%E4%BD%9C%E5%93%81%E3%83%9A%E3%83%BC%E3%82%B8%E3%81%AB%E5%90%84%E7%A8%AE%E3%82%B5%E3%82%A4%E3%83%88%E3%81%B8%E3%81%AE%E3%83%AA%E3%83%B3%E3%82%AF%E3%82%92%E8%A1%A8%E7%A4%BA%E3%81%99%E3%82%8B_UserScript
// @icon            https://www.google.com/s2/favicons?sz=64&domain=annict.com
// @supportURL      https://github.com/SlashNephy/.github/issues
// @match           https://annict.com/*
// @connect         raw.githubusercontent.com
// @grant           GM_xmlhttpRequest
// @license         MIT license
// ==/UserScript==

const executeXhr = async (request) => {
  return new Promise((resolve, reject) => {
    GM_xmlhttpRequest({
      ...request,
      onload: (response) => {
        resolve(response)
      },
      onerror: (error) => {
        reject(error)
      },
    })
  })
}

const fetchArmEntries = async () => {
  const response = await executeXhr({
    method: 'GET',
    url: 'https://raw.githubusercontent.com/kawaiioverflow/arm/master/arm.json',
  })
  return JSON.parse(response.responseText)
}

const ANNICT_WORK_PAGE_URL_PATTERN = /^https:\/\/annict\.com\/works\/(\d+)/
let cachedEntries = null
const main = async () => {
  const match = location.href.match(ANNICT_WORK_PAGE_URL_PATTERN)
  if (!match) {
    return
  }
  const annictId = parseInt(match[1])
  if (!annictId) {
    throw new Error('Failed to extract Annict work id')
  }
  const links = document.querySelector(
    'div.c-work-header.pt-3 > div.container > div > div.col.mt-3.mt-sm-0 > ul.list-inline.mb-0'
  )
  if (!links || links.childNodes.length === 0) {
    throw new Error('Failed to find target container')
  }
  const entries = cachedEntries ?? (await fetchArmEntries())
  cachedEntries = entries
  const entry = entries.find((x) => x.annict_id === annictId)
  if (!entry) {
    console.warn(`arm entry not found: annict_id=${annictId}`)
    return
  }
  if (entry.syobocal_tid && links.firstChild) {
    const link = links.firstChild.cloneNode(true)
    const a = link.firstChild
    a.href = `https://cal.syoboi.jp/tid/${entry.syobocal_tid}`
    a.childNodes[0].textContent = 'しょぼいカレンダー'
    links.appendChild(link)
  }
  if (entry.mal_id && links.firstChild) {
    const link = links.firstChild.cloneNode(true)
    const a = link.firstChild
    a.href = `https://myanimelist.net/anime/${entry.mal_id}`
    a.childNodes[0].textContent = 'MyAnimeList'
    links.appendChild(link)
  }
  if (entry.anilist_id && links.firstChild) {
    const link = links.firstChild.cloneNode(true)
    const a = link.firstChild
    a.href = `https://anilist.co/anime/${entry.anilist_id}`
    a.childNodes[0].textContent = 'AniList'
    links.appendChild(link)
  }
}
main().catch(console.error)
window.addEventListener('turbo:load', () => {
  main().catch(console.error)
})