Skip to content

Instantly share code, notes, and snippets.

@Koopzington
Created June 22, 2016 20:48
Show Gist options
  • Select an option

  • Save Koopzington/f95080bffc04705871d5addedfba0655 to your computer and use it in GitHub Desktop.

Select an option

Save Koopzington/f95080bffc04705871d5addedfba0655 to your computer and use it in GitHub Desktop.
When looking on a GitHub repository, this script checks if you already have a fork of it on your account.
// ==UserScript==
// @name Fork-Checker
// @namespace https://github.com/Koopzington
// @version 0.1
// @description When looking on a GitHub repository, this script checks if you already have a fork of it on your account.
// @author [email protected]
// @match https://github.com/*/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
// First we need to check if the current page actually IS a repository since there are other pages matching the URL format
var pagecheck = document.getElementsByClassName('repohead');
if (pagecheck.length > 0) {
// Grab page title
var repoName = document.getElementsByTagName('title')[0].innerHTML;
// Strip the part behind ':'. We've got vendorname/reponame now
repoName = repoName.split(':')[0];
var originalName = repoName;
// We only want the reponame
repoName = repoName.split('/')[1];
// Grab username
var username = document.getElementsByName('user-login')[0].getAttribute('content');
// Check if current repository page isn't actually one of yours.
if (username + "/" + repoName !== originalName) {
var outputTarget = document.getElementsByClassName('pagehead-actions')[0];
var content = document.createElement('button');
content.className = 'btn btn-sm';
GM_xmlhttpRequest({
method: "GET",
url: "https://github.com/" + username + "/" + repoName,
onload: function(responseDetails) {
console.log(responseDetails);
if (responseDetails.status == 200) {
content.className += ' btn-primary';
content.innerHTML = 'Already forked!';
} else if (responseDetails.status == 404) {
content.innerHTML = 'Not forked!';
}
// Add fancy button with result next to the fork button
outputTarget.appendChild(content);
}
});
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment