GitHub one-click sync fork

Sync your GitHub fork repo within one click

  1. // ==UserScript==
  2. // @name GitHub one-click sync fork
  3. // @namespace https://blog.maple3142.net/
  4. // @version 0.5
  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. // eslint-disable-next-line max-len
  23. const confirmmsg =
  24. 'Are you sure?\nAll the changes you have made will be DELETED!\nIT CANNOT BE RECOVERED!'
  25. const search = new URL(location.href).searchParams
  26. if (search.has('code')) {
  27. const code = search.get('code')
  28. xf.post('https://github.com/login/oauth/access_token', {
  29. json: {
  30. client_id: '5c0954a832a0f2bb68f2',
  31. client_secret: '799a5a49ef0ebde906f8bedc2e3327a99cd0d92a',
  32. code
  33. }
  34. }).text(str => {
  35. const search = new URLSearchParams(str)
  36. GM_setValue('token', search.get('access_token'))
  37. window.close()
  38. })
  39. }
  40.  
  41. const addBtn = () => {
  42. const currentUser = $('.user-profile-link>strong').textContent
  43. const currentRepoOwner = location.pathname.split('/')[1]
  44. const isUserMatch = currentUser === currentRepoOwner
  45. const forkText = $('.flex-auto>.text-small')
  46. const isFork =
  47. !!forkText && forkText.textContent.includes('forked from')
  48.  
  49. if (!isUserMatch || !isFork) return
  50. $$('#one-click-sync-fork').forEach(btn => btn.remove())
  51. const repo = location.pathname.split('/').slice(1, 3).join('/')
  52. const upstream = forkText.querySelector('a').textContent.trim()
  53. const fnav = $('.file-navigation')
  54. const branch = $('link[href$=atom]').href.split('/').pop().split('.')[0]
  55. const el = document.createElement('a')
  56. el.classList.add('btn')
  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
  67. .get(`/repos/${upstream}/branches/${branch}`)
  68. .json(b => b.commit.sha)
  69. await gh
  70. .patch(`/repos/${repo}/git/refs/heads/${branch}`, {
  71. headers: { Authorization: `token ${token}` },
  72. json: { sha, force: true }
  73. })
  74. .json()
  75. location.reload()
  76. }
  77. const wrapper = document.createElement('div')
  78. wrapper.style.marginLeft = '6px'
  79. wrapper.id = 'one-click-sync-fork'
  80. wrapper.appendChild(el)
  81. const branchbtn = $('#branch-select-menu')
  82. branchbtn.insertAdjacentElement('afterend', wrapper)
  83. }
  84. addBtn()
  85. new MutationObserver(addBtn).observe(document.body, { childList: true })
  86. })()