Skip to content

Instantly share code, notes, and snippets.

@mattkuo mattkuo/github-explorer.user.js Secret

Last active Feb 18, 2019
Embed
What would you like to do?
Github explorer
// ==UserScript==
// @name Github Explorer
// @namespace http://matthewkuo.com
// @version 0.1
// @description Shows the README file of a github repository
// @match https://github.com/explore
// @require https://code.jquery.com/jquery-3.3.1.min.js
// ==/UserScript==
(function() {
let $stories = $('.Story');
let $body = $('body');
let $modal = $('<div id="modal"><div id="close-modal"><span>X</span></div><div id="modal-content"></div></div>');
let $close = $modal.find('#close-modal');
let $modalContent = $modal.find('#modal-content');
var timer;
init();
function init() {
$body.append($modal);
setupCss('#modal{display:none;position:fixed;width:100%;bottom:0;left:0;right:0;background:white;padding:10px;max-height:20vw;overflow-y:scroll;border-top:1px solid grey}#close-modal{position:fixed;right:30px}');
setupMouseHandlers();
}
function setupMouseHandlers() {
$close.click(function() {
$modal.hide();
});
$stories.mouseenter(function() {
let $this = $(this);
timer = setTimeout(function() {
let href = $this.find('div > div > h1 > a').last().attr('href');
let re = /^\/(.+)\/(.+)$/;
let [_, owner, repoName] = href.match(re);
displayReadme(owner, repoName);
}, 1000);
}).mouseleave(function() {
clearTimeout(timer);
});
}
function displayReadme(owner, repoName) {
jQuery.ajax( {
type: 'GET',
url: `https://api.github.com/repos/${owner}/${repoName}/readme`,
dataType: 'JSON',
success: function(data) {
let markdown = atob(data.content);
let strippedString = markdown.replace(/(<([^>]+)>)/ig,"");
$modalContent[0].innerText = strippedString;
$modal.show();
}
});
}
function setupCss(css) {
let head = $('head');
let style = document.createElement('style');
style.setAttribute('type', 'text/css');
style.textContent = css;
head.append(style);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.