// ==UserScript== // @name GithubDashboardEnhance // @description Show the latest 30 starred repos in new Github dashboard and shortcuts in header navigation. // @description:zh-CN 在 Github 新首页显示最近 30 个 star 项目,在头部导航栏中显示快捷方式 // @description:zh-TW 在 Github 新首頁顯示最近 30 個 star 項目,在頭部導航欄中顯示快捷方式 // @author ladit // @version 1.1.2 // @namespace https://greasyfork.org/zh-CN/scripts/33511 // @homepageURL https://github.com/ladit/Userscripts // @supportURL https://github.com/ladit/Userscripts // @grant GM.setValue // @grant GM.getValue // @grant GM.xmlHttpRequest // @run-at document-idle // @include https://github.com/* // @connect api.github.com // @downloadURL https://update.greasyfork.icu/scripts/33511/GithubDashboardEnhance.user.js // @updateURL https://update.greasyfork.icu/scripts/33511/GithubDashboardEnhance.meta.js // ==/UserScript== (async () => { let userName = document.querySelector('meta[name="user-login"]').getAttribute('content') if (userName === '') { return; } const actionMenu = document.querySelector('.AppHeader-actions > action-menu') if (actionMenu) { actionMenu.insertAdjacentHTML('afterend', `
`) } const rightFooter = document.querySelector('div[aria-label="Explore repositories"] > .footer') if (!rightFooter) { return; } const fetchMap = async (key, defaultMap) => { const v = await GM.getValue(key) if (!v) { return defaultMap } return new Map(JSON.parse(v)) } const storeMap = async (key, m) => { await GM.setValue(key, JSON.stringify(Array.from(m.entries()))) } const lastStoreColors = await GM.getValue('lastStoreColors', 0) if (lastStoreColors + 30 * 86400000 < Date.now()) { GM.xmlHttpRequest({ method: 'GET', timeout: 5000, responseType: 'json', url: 'https://raw.githubusercontent.com/ozh/github-colors/master/colors.json', onload: async resp => { let languageColors = new Map() for (const [language, v] of Object.entries(resp.response)) { languageColors.set(language, v.color) } await storeMap('languageColors', languageColors) await GM.setValue('lastStoreColors', Date.now()) }, onerror: resp => { console.log('[GithubDashboardEnhance]: request colors failed: ', resp) }, ontimeout: () => { console.log('[GithubDashboardEnhance]: Request colors timeout.') }, }) } const lastStoreStarredReposTime = await GM.getValue('lastStoreStarredReposTime', 0) if (lastStoreStarredReposTime + 86400000 < Date.now()) { GM.xmlHttpRequest({ method: 'GET', timeout: 5000, responseType: 'json', url: `https://api.github.com/users/${userName}/starred`, onload: async resp => { const languageColors = await fetchMap('languageColors', new Map()) let starredReposBlock = '

Recent Starred repositories

' let i = 0 for (const repo of resp.response) { i += 1 let border = '' if (i < resp.response.length) { border = 'border-bottom' } starredReposBlock += `

${repo.description}

${repo.stargazers_count > 1000 ? (repo.stargazers_count / 1000).toFixed(1) + 'k' : repo.stargazers_count}
${repo.language}
` } starredReposBlock += `More →
` await GM.setValue('starredReposBlock', starredReposBlock) await GM.setValue('lastStoreStarredReposTime', Date.now()) }, onerror: resp => { console.log('[GithubDashboardEnhance]: request failed: ', resp) }, ontimeout: () => { console.log('[GithubDashboardEnhance]: Request timeout.') }, }) } rightFooter.insertAdjacentHTML('beforebegin', await GM.getValue('starredReposBlock', '')) })()