Last active
December 28, 2021 17:59
-
-
Save hazrpg/0338eb8061bd009f1e01dcfe6d335857 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name GitHub Link Fixer - The Download Friendly fix you didn't know you needed! | |
// @namespace https://github.com/hazrpg/ | |
// @version 0.2 | |
// @updateURL https://gist.github.com/hazrpg/0338eb8061bd009f1e01dcfe6d335857/raw/github-link-fixer.user.js | |
// @description This script fixes GitHub links are more usable. When you right-click and save-as it will now download the proper raw version. If you click it will continue to work as normal. This is the fix Linus from LTT needed. | |
// @author hazrpg | |
// @match https://*.github.com/* | |
// @icon https://www.google.com/s2/favicons?domain=github.com | |
// @grant none | |
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/js/all.min.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Your code here... | |
jQuery(document).ready(function() { | |
console.log('GitHub Link Fixer warming up...'); | |
let links = jQuery('a.js-navigation-open'); | |
links.each(function() { | |
// Define the required variables we'll need. | |
let link = jQuery(this).attr('href'); | |
let raw = link.replace('/blob/', '/raw/'); | |
let filename = link.substring(link.lastIndexOf('/')+1); | |
let isDotFile = filename.substring(0, 1) == '.'; | |
let isFile = link.includes('/blob/'); | |
let isFolder = link.includes('/tree/'); | |
// Change the links. | |
jQuery(this).attr('href', raw); | |
jQuery(this).attr('download', filename); | |
// Add an informative graphic to let people know that its worked. | |
// Check if its a file and add the graphic. | |
if(isFile === true) { | |
jQuery(this).after(' <i class="fas fa-mouse" title="Link is right-click and save-as enabled."></i>'); | |
} | |
// Check if its a dot file and add extra information. | |
if(isDotFile) { | |
jQuery(this).after(' <i style="color: red;" class="fas fa-exclamation-circle" title="This file will download as a .txt, make sure to rename it to this: '+filename+'"></i>'); | |
} | |
// Chanage the behaviour for clicking so that it takes you to the normal place - where devs expect. | |
jQuery(this).click(function(e) { | |
e.preventDefault(); | |
location.href = link; | |
parent.location = link; | |
window.location.href = link; | |
}); | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment