Greasy Fork 还支持 简体中文。

GitHub one-click sync fork

Sync your GitHub fork repo within one click

目前為 2018-10-29 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitHub one-click sync fork
  3. // @namespace https://blog.maple3142.net/
  4. // @version 0.2
  5. // @description Sync your GitHub fork repo within one click
  6. // @author maple3142
  7. // @match https://github.com/*
  8. // @require https://unpkg.com/xfetch-js@0.2.3/xfetch.min.js
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @compatible firefox >=52
  12. // @compatible chrome >=55
  13. // ==/UserScript==
  14.  
  15. ;(function() {
  16. 'use strict'
  17. const $ = s => document.querySelector(s)
  18. const $$ = s => [...document.querySelectorAll(s)]
  19. const gh = xf.extend({ baseURI: 'https://api.github.com/' })
  20. const oauthurl = // eslint-disable-next-line max-len
  21. 'https://github.com/login/oauth/authorize?client_id=5c0954a832a0f2bb68f2&scope=repo&redirect_uri=https://github.com/'
  22. const confirmmsg = 'Are you sure?\nAll the changes you have made will be DELETED!\nIT CANNOT BE RECOVERED!' // eslint-disable-next-line max-len
  23. const search = new URL(location.href).searchParams
  24. if (search.has('code')) {
  25. const code = search.get('code')
  26. xf.post('https://github.com/login/oauth/access_token', {
  27. json: { client_id: '5c0954a832a0f2bb68f2', client_secret: '799a5a49ef0ebde906f8bedc2e3327a99cd0d92a', code }
  28. }).text(str => {
  29. const search = new URLSearchParams(str)
  30. GM_setValue('token', search.get('access_token'))
  31. window.close()
  32. })
  33. }
  34.  
  35. const addBtn = () => {
  36. const currentUser = $('.user-profile-link>strong').textContent
  37. const currentRepoOwner = location.pathname.split('/')[1]
  38. const isUserMatch = currentUser === currentRepoOwner
  39. const isFork = !!$('.fork-flag')
  40.  
  41. if (!isUserMatch || !isFork) return
  42. $$('.one-click-sync-fork').forEach(btn => btn.remove())
  43. const repo = location.pathname
  44. .split('/')
  45. .slice(1, 3)
  46. .join('/')
  47. const upstream = $('.fork-flag a')
  48. .getAttribute('href')
  49. .slice(1)
  50. const branchbtn = $('.file-navigation>div.select-menu')
  51. if (!branchbtn) return
  52. const branch = branchbtn.querySelector('button').title || branchbtn.querySelector('button>span').textContent
  53. const el = document.createElement('div')
  54. el.classList.add('one-click-sync-fork')
  55. el.classList.add('btn')
  56. el.classList.add('btn-sm')
  57. el.classList.add('btn-primary')
  58. el.textContent = `Update fork from ${upstream}/${branch}`
  59. el.onclick = async () => {
  60. const token = GM_getValue('token')
  61. if (!token) {
  62. window.open(oauthurl, '_target')
  63. return
  64. }
  65. if (!confirm(confirmmsg)) return
  66. const sha = await gh.get(`/repos/${upstream}/commits`).json(c => c[0].sha)
  67. await gh
  68. .patch(`/repos/${repo}/git/refs/heads/${branch}`, {
  69. headers: { Authorization: `token ${token}` },
  70. json: { sha, force: true }
  71. })
  72. .json()
  73. location.reload()
  74. }
  75. branchbtn.insertAdjacentElement('afterend', el)
  76. }
  77. addBtn()
  78. new MutationObserver(addBtn).observe(document.body, { childList: true })
  79. })()