improved GitLab blame

add link to skip a certain commit

  1. // ==UserScript==
  2. // @name improved GitLab blame
  3. // @namespace https://franklinyu.gitlab.io
  4. // @description add link to skip a certain commit
  5. // @version 0.1
  6. // @match https://gitlab.com/*/blame/*
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. if (!Array.prototype.last){
  11. Array.prototype.last = function() {
  12. return this[this.length - 1]
  13. }
  14. }
  15.  
  16. const projectName = encodeURIComponent(location.pathname.split('/blame')[0].slice(1))
  17. const commits = document.getElementsByClassName('commit-sha')
  18.  
  19. ;(async () => {
  20. for (const commit of commits) {
  21. const sha1 = commit.href.split('/').last()
  22. const resp = await fetch(`https://gitlab.com/api/v4/projects/${projectName}/repository/commits/${sha1}`)
  23. const jsonResp = await resp.json()
  24. if (jsonResp.parent_ids.length === 1) {
  25. const anchor = document.createElement('a')
  26. anchor.href = location.href.replace(/\/blame\/[\w-]+\//, `/blame/${jsonResp.parent_ids[0]}/`)
  27. anchor.innerText = 'prev'
  28. const floatRight = document.createElement('div')
  29. floatRight.classList.add('float-right')
  30. floatRight.append(anchor, '\u00A0')
  31. commit.parentElement.parentElement.nextElementSibling.append(floatRight)
  32. }
  33. }
  34. })()