Skip to content

Instantly share code, notes, and snippets.

@traveaston
Last active December 2, 2019 18:11
Show Gist options
  • Save traveaston/083720c2fc5748e95b4c65790709d50f to your computer and use it in GitHub Desktop.
Save traveaston/083720c2fc5748e95b4c65790709d50f to your computer and use it in GitHub Desktop.
Reverse the direction of Github compare (bookmarklet)

Create a new bookmark with the following code (Github doesn't allow making a draggable bookmarklet) or paste the IIFE (top of the JS file below) into the console on a Github compare page.

javascript:(function()%7B(()%20%3D%3E%20%7Bconst%20url%20%3D%20document.location.href%3Bconst%20targetBasename%20%3D%20url.split('%2F').slice(-1)%5B0%5D.split('...').reverse().join('...')%3Bconst%20lastSlash%20%3D%20url.lastIndexOf('%2F')%3Bconst%20targetURL%20%3D%20url.substring(0%2C%20lastSlash%20%2B%201)%20%2B%20targetBasename%3Bconsole.log('Reversed%20branch%20compare%20URL.%20Navigating%20to'%2C%20targetURL)%3Bdocument.location.href%20%3D%20targetURL%3B%7D)()%7D)()

I prefer to put the bookmarklet in a folder somewhere, hit ⌘+L, type reverse and tap down to the bookmarklet like so:
https://i.imgur.com/brhwipo.png

Shoutout to bitoiu for the Chrome extension code I based this bookmarklet on.

I originally forked the extension, fixed it with Github's current frontend class references, and it worked nicely but was having trouble adding a tooltip title element to the SVG Github uses; it seemed to only take the title into effect when I edited the element's HTML in the console and changed something, then Chrome would re-render the SVG, but couldn't get it working on load, and obviously all searches for SVG caching are trying to achieve the opposite of what I was.

So now it's a bookmarklet/scriptlet that doesn't require installing an extension, and more importantly, won't break when Github makes changes to their frontend.

;(() => {
const url = document.location.href
const targetBasename = url
.split('/') // [..., "github.com", "base_user", ..., "master...head_user:master"]
.slice(-1)[0] // "master...head_user:master"
.split('...') // ["master", "head_user:master"]
.reverse() // ["head_user:master", "master"]
.join('...') // "head_user:master...master"
// github will automatically redirect to the proper url if necessary
const lastSlash = url.lastIndexOf('/')
const targetURL = url.substring(0, lastSlash + 1) + targetBasename
console.log('Reversed branch compare URL. Navigating to', targetURL)
document.location.href = targetURL
})()
// Bookmarklet accepts chaining, but not comments and requires manual semicolons
javascript:(function()%7B(()%20%3D%3E%20%7Bconst%20url%20%3D%20document.location.href%3Bconst%20targetBasename%20%3D%20url.split('%2F').slice(-1)%5B0%5D.split('...').reverse().join('...')%3Bconst%20lastSlash%20%3D%20url.lastIndexOf('%2F')%3Bconst%20targetURL%20%3D%20url.substring(0%2C%20lastSlash%20%2B%201)%20%2B%20targetBasename%3Bconsole.log('Reversed%20branch%20compare%20URL.%20Navigating%20to'%2C%20targetURL)%3Bdocument.location.href%20%3D%20targetURL%3B%7D)()%7D)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment