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. // 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 branchbtn = $('.file-navigation>div.select-menu')
  52. if (!branchbtn) return
  53. const branch = branchbtn.querySelector('button').title || branchbtn.querySelector('button>span').textContent
  54. const el = document.createElement('div')
  55. el.classList.add('one-click-sync-fork')
  56. el.classList.add('btn')
  57. el.classList.add('btn-sm')
  58. el.classList.add('btn-primary')
  59. el.textContent = `Update fork from ${upstream}/${branch}`
  60. el.onclick = async () => {
  61. const token = GM_getValue('token')
  62. if (!token) {
  63. window.open(oauthurl, '_target')
  64. return
  65. }
  66. if (!confirm(confirmmsg)) return
  67. const sha = await gh.get(`/repos/${upstream}/commits`).json(c => c[0].sha)
  68. await gh
  69. .patch(`/repos/${repo}/git/refs/heads/${branch}`, {
  70. headers: { Authorization: `token ${token}` },
  71. json: { sha, force: true }
  72. })
  73. .json()
  74. location.reload()
  75. }
  76. branchbtn.insertAdjacentElement('afterend', el)
  77. }
  78. addBtn()
  79. new MutationObserver(addBtn).observe(document.body, { childList: true })
  80. })()