// ==UserScript== // @name Github remove Top repositories // @description 去除Github左侧Top repositories中不属于自己的仓库 // @version 1.0.0 // @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 // ==/UserScript== (async () => { const user_name = await new Promise((resolve, reject) => { GM_cookie.list({name: "dotcom_user"}, (cookies) => { if (cookies.length == 0) { reject() } else { resolve(cookies[0].value) } }) }) if (!user_name) { console.error('无法获取用户名,请检测cookie相关权限') return } 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 } })();