Sankaku Bring back post sources

Bring back the source link in beta.sankakucomplex.com

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);