Last active
March 23, 2023 19:16
-
-
Save vic4key/d8f974c8febacd823ffb9fd108f943a5 to your computer and use it in GitHub Desktop.
Tampermonkey - GitHub - Fork State
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 - Fork State | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description To let you know immediately that you already forked the visiting repository or not | |
// @author Vic P. @ https://vic.onl/ | |
// @match https://github.com/*/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com | |
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js | |
// @run-at document-start | |
// @grant none | |
// ==/UserScript== | |
let log = (...args) => console.log(`%cGitHub - Fork State >`, 'font-weight: bold; color: yellow;', ...args) | |
function regex_extract_capture_groups(text, regex) | |
{ | |
let result = [] | |
let matches; | |
while (matches = regex.exec(text)) | |
{ | |
if (matches.length > 0) | |
{ | |
result.push(matches[1]); | |
} | |
} | |
return result; | |
} | |
function truncate_string(str, num) | |
{ | |
return str.length <= num ? str : str.slice(0, num) + "..."; | |
} | |
function github_update_fork_state() { | |
let regex_pattern = /github.com\/([+-_\d\w]+)\/([+-_\d\w]+)/gm; | |
let parts = regex_pattern.exec(window.location.href); | |
if (parts && parts.length == 3) | |
{ | |
let repo_user = parts[1]; | |
let repo_name = parts[2]; | |
let repo_url = `https://github.com/${repo_user}/${repo_name}`; | |
log(`Repository Loaded '${repo_url}'`); | |
$.ajax(`${repo_url}/my_forks_menu_content?can_fork=true`, { | |
type: "GET", | |
success: function (data, status, xhr) { | |
// get forked count from repo info | |
let forked_count = $(`a[href*='/${repo_user}/${repo_name}/forks'][data-view-component]`).find("strong").text(); | |
let repo_fork_button = $(`#fork-button`); // $(`a[href*='/${repo_user}/${repo_name}/fork']`); | |
let repo_fork_button_text = `Fork [${forked_count}]`; | |
// check that you have forked or not | |
let forked = data.indexOf("don't have any") == -1; | |
if (forked) { | |
let repo_fork_button_url = ""; | |
let forked_urls = regex_extract_capture_groups(data, /normal.*href=\"(.*)\".*role/gm); | |
if (forked_urls && forked_urls.length > 0) { // you already forked | |
if (forked_urls.length == 1) { // only you or your org | |
let forked_url = forked_urls[0]; | |
repo_fork_button_url = ` '${forked_url}'`; | |
repo_fork_button.prop("href", `https://github.com${forked_url}`); // change fork-button'url to your forked repo url | |
} | |
else { // both you and your org(es) | |
repo_fork_button_text += ` (${forked_urls.length} existing forks)`; // change fork-button's caption (repo forked count + your existing forked count) | |
} | |
} | |
// repo_fork_button.css({"background-color": "rgba(var(--color-btn-bg), 0.8)"}); // change fork-button's background color | |
repo_fork_button_url = truncate_string(repo_fork_button_url, 30); | |
repo_fork_button.text(`${repo_fork_button_text}${repo_fork_button_url}`); // change fork-button's caption (repo forked count + your forked url) | |
} | |
else { // you did not fork | |
repo_fork_button.text(`${repo_fork_button_text} (Forkable)`); | |
} | |
}, | |
error: function (jqXhr, textStatus, errorMessage) { | |
log(errorMessage); | |
} | |
}); | |
} | |
} | |
window.addEventListener("DOMContentLoaded", function(event) { | |
github_update_fork_state(); | |
}); | |
window.addEventListener('popstate', function (e) { | |
if (e.state !== null) { | |
github_update_fork_state(); | |
} | |
}); | |
(function() { | |
'use strict'; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment