GitHub one-click sync fork

Sync your GitHub fork repo within one click

目前為 2019-02-19 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitHub one-click sync fork
  3. // @namespace https://blog.maple3142.net/
  4. // @version 0.4
  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 = 'Are you sure?\nAll the changes you have made will be DELETED!\nIT CANNOT BE RECOVERED!'
  24. const search = new URL(location.href).searchParams
  25. if (search.has('code')) {
  26. const code = search.get('code')
  27. xf.post('https://github.com/login/oauth/access_token', {
  28. json: { client_id: '5c0954a832a0f2bb68f2', client_secret: '799a5a49ef0ebde906f8bedc2e3327a99cd0d92a', code }
  29. }).text(str => {
  30. const search = new URLSearchParams(str)
  31. GM_setValue('token', search.get('access_token'))
  32. window.close()
  33. })
  34. }
  35.  
  36. const addBtn = () => {
  37. const currentUser = $('.user-profile-link>strong').textContent
  38. const currentRepoOwner = location.pathname.split('/')[1]
  39. const isUserMatch = currentUser === currentRepoOwner
  40. const isFork = !!$('.fork-flag')
  41.  
  42. if (!isUserMatch || !isFork) return
  43. $$('.one-click-sync-fork').forEach(btn => btn.remove())
  44. const repo = location.pathname
  45. .split('/')
  46. .slice(1, 3)
  47. .join('/')
  48. const upstream = $('.fork-flag a')
  49. .getAttribute('href')
  50. .slice(1)
  51. const fnav = $('.file-navigation')
  52. const branchbtn = $('.file-navigation>.select-menu')
  53. if (!branchbtn) return
  54. const branch =
  55. fnav
  56. .querySelector('a')
  57. .href.split('/')
  58. .pop() || branchbtn.querySelector('summary>span').textContent
  59. const el = document.createElement('div')
  60. el.classList.add('one-click-sync-fork')
  61. el.classList.add('btn')
  62. el.classList.add('btn-sm')
  63. el.classList.add('btn-primary')
  64. el.textContent = `Update fork from ${upstream}/${branch}`
  65. el.onclick = async () => {
  66. const token = GM_getValue('token')
  67. if (!token) {
  68. window.open(oauthurl, '_target')
  69. return
  70. }
  71. if (!confirm(confirmmsg)) return
  72. const sha = await gh.get(`/repos/${upstream}/branches/${branch}`).json(b => b.commit.sha)
  73. await gh
  74. .patch(`/repos/${repo}/git/refs/heads/${branch}`, {
  75. headers: { Authorization: `token ${token}` },
  76. json: { sha, force: true }
  77. })
  78. .json()
  79. location.reload()
  80. }
  81. branchbtn.insertAdjacentElement('afterend', el)
  82. }
  83. addBtn()
  84. new MutationObserver(addBtn).observe(document.body, { childList: true })
  85. })()