remove Top repositories

去除Top repositories中不属于自己的仓库

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

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

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.

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

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         remove Top repositories
// @description  去除Top repositories中不属于自己的仓库
// @version      2.0.1
// @author       girl-dream
// @namespace    https://github.com/girl-dream
// @license      The Unlicense
// @match        https://github.com/
// @icon         https://github.com/favicon.ico
// @grant        unsafeWindow
// @grant        GM_cookie
// @run-at       document-start
// ==/UserScript==

(async () => {
    'use strict'
    const user_name = await new Promise((resolve, reject) => {
        GM_cookie.list({ name: "dotcom_user" }, (cookies) => {
            resolve(cookies[0].value)
        })
    })

    if (user_name) {
        const originalFetch = window.fetch

        const processHtmlResponse = async (clonedResponse, selector, getUsername) => {
            const html = new DOMParser().parseFromString(await clonedResponse.text(), 'text/html')
            html.querySelectorAll(selector).forEach((el) => {
                const repo_username = getUsername(el)
                if (repo_username != user_name) {
                    el.remove()
                }
            })
            return new Response(html.body.innerHTML, clonedResponse)
        }

        const filterJsonArray = (data, getValue) => {
            if (Array.isArray(data)) {
                return data.filter(item => {
                    const repo_username = getValue(item).split('/')[0]
                    return repo_username == user_name
                })
            } else if (data.repositories) {
                data.repositories = data.repositories.filter(item => item.ownerLogin === user_name)
                return data
            }
            throw new Error('未知情况')
        }

        unsafeWindow.fetch = async function (...args) {
            if (args[0] instanceof Request) {
                const response = await originalFetch.apply(this, [args[0]])
                const url = response.url
                const clonedResponse = response.clone()

                if (url.includes('repositories')) {
                    if (url.includes('mobile')) {
                        return processHtmlResponse(clonedResponse, '.source', (el) => {
                            return el.querySelector('span').parentElement.textContent.trim().split('/')[0]
                        })
                    } else {
                        return processHtmlResponse(clonedResponse, 'li.source', (el) => {
                            return el.querySelector('.color-fg-muted').parentElement.textContent.trim().split('/')[0]
                        })
                    }
                }
                return response
            }

            const response = await originalFetch.apply(this, args)
            const clonedResponse = response.clone()

            if (args[0].includes('global.json')) {
                let data = await clonedResponse.json()
                data.repositories = data.repositories.filter(item => {
                    const repo_username = item.value.split('/')[0]
                    return repo_username == user_name
                })
                return new Response(JSON.stringify(data), response)
            }
            if (args[0].includes('repositories')) {
                let type = response.headers.get('content-type')
                if (type.includes('application/json')) {
                    let data = await clonedResponse.json()
                    const filtered = filterJsonArray(data, (item) => item.value || item.name)
                    return new Response(JSON.stringify(filtered), response)
                } else if (type.includes('text/html')) {
                    return processHtmlResponse(clonedResponse, 'li', (el) => {
                        return el.querySelector('.wb-break-word a').textContent.trim().split('/')[0]
                    })
                }
            }
            return response
        }
    }
})();