Publish button in repo on GitHub

5/28/2020, 5:17:05 PM

  1. // ==UserScript==
  2. // @name Publish button in repo on GitHub
  3. // @namespace Violentmonkey Scripts
  4. // @match https://github.com/*/*
  5. // @grant none
  6. // @version 1.4
  7. // @author -
  8. // @description 5/28/2020, 5:17:05 PM
  9. // ==/UserScript==
  10.  
  11. const username = $("a.url.fn").innerText
  12.  
  13. // DOM-lib
  14. Element.prototype.makeElement = function (tag, options={}) {
  15. return makeElement(tag, options, this)
  16. }
  17.  
  18. Element.prototype.find = Element.prototype.querySelector
  19.  
  20. function makeElement(tag, options={}, parent) {
  21. const el = document.createElement(tag)
  22. Object.assign(el, options)
  23. if (parent) parent.append(el)
  24. return el
  25. }
  26.  
  27. function $(selector, text) {
  28. if (text) return [...$$(selector)].find(el => el.innerText == text)
  29. return document.querySelector(selector)
  30. }
  31.  
  32. function $$(selector) {
  33. return [...document.body.querySelectorAll(selector)].filter(el => el.tagName != "SCRIPT")
  34. }
  35.  
  36. // Script
  37. if (username == location.href.match(/github.com\/([^\/]*)/)[1]) {
  38. if (!location.href.endsWith('/settings') && !localStorage.publish) {
  39. $('.pagehead-actions').makeElement('li').makeElement('button', {className: 'btn btn-sm', innerText: 'Publish'}).onclick = () => {
  40. console.log('start publish')
  41. localStorage.publish = 1
  42. location.href += '/settings/pages'
  43. }
  44. } else if (location.href.endsWith('/settings/pages') && localStorage.publish == 1) {
  45. localStorage.publish = 2
  46. setTimeout(() => $('[value=master]').click(), 300)
  47. setTimeout(() => [...document.forms].find(form => form.action.endsWith("/pages/source")).submit(), 1000)
  48. } else if (location.href.endsWith('/settings/pages') && localStorage.publish == 2) {
  49. localStorage.publish = 3
  50. location.href = location.href.replace('/settings/pages', '')
  51. } else if (!location.href.endsWith('/settings/pages') && localStorage.publish == 3) {
  52. delete localStorage.publish
  53. repo_homepage.value = 'https://' + username + '.github.io/' + location.href.replace('https://github.com/' + username + '/', '') + '/'
  54. repo_metadata_form.submit()
  55. }
  56. }