Sankaku Bring back post sources

Bring back the source link in beta.sankakucomplex.com

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Sankaku Bring back post sources
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Bring back the source link in beta.sankakucomplex.com
// @author       You
// @match        https://beta.sankakucomplex.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=sankakucomplex.com
// @grant        none
// @license      MIT
// ==/UserScript==

'use strict';
(w => {
    const LOG_ENABLED = false

    const log = (...data) => {
        if (!LOG_ENABLED) return
        console.log(...data)
    }

    const matchPostId = () => w.location.pathname.match?.(/\/post\/show\/(\d+)$/)?.[1]
    const fetchPostInfo = (id) =>
        w.fetch(`https://capi-v2.sankakucomplex.com/posts?lang=en&page=1&limit=1&tags=id_range:${id}`)
         .then(res => res.json())
         .then(res => res?.[0])
    const selectorAll = sel => [...w.document.querySelectorAll(sel)]
    const findUploaderField = () =>
        selectorAll('.MuiGrid-item')
          .find(el => el.textContent === 'Uploader')
          ?.parentElement
          ?.lastElementChild
    const makeSourceLink = () => {
        const el = w.document.createElement('a')
        el.textContent = 'Source'
        el.style.textAlign = 'right'
        el.dataset.bring_back_src = '1'
        return el
    }

    let rememberLastId

    const run = async () => {
        const id = matchPostId()
        if (!id) {
            log('not in post page')
            return
        }

        const uploaderEl = findUploaderField()
        if (!uploaderEl) {
            log('not found uploader field to inject into')
            return
        }

        if (id === rememberLastId) {
            log('same as last one')
            return
        }
        rememberLastId = id

        let post
        try {
            post = await fetchPostInfo(id)
        } catch (e) {
            log('error fetching source for post using api', e)
            return
        }
        const src = post?.source

        const alreadyInjected = uploaderEl.lastElementChild?.dataset.bring_back_src
        let link
        if (alreadyInjected) {
            link = uploaderEl.lastElementChild
        } else {
            link = makeSourceLink()
            uploaderEl.appendChild(link)
        }

        log('updating source link')
        if (src) {
            link.href = src
            link.style.pointerEvents = null
            link.style.color = null
            link.style.textDecoration = null
        } else {
            link.href = '#'
            link.style.pointerEvents = 'none'
            link.style.color = 'inherit'
            link.style.textDecoration = 'none'
        }
        uploaderEl.style.display = 'flex'
        uploaderEl.style.flexFlow = 'column'
    }

    setInterval(run, 500)
})(window);