Skip to content

Instantly share code, notes, and snippets.

@YuJianrong
Last active February 21, 2019 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YuJianrong/744c629dc2b093dc661525c83ac504f9 to your computer and use it in GitHub Desktop.
Save YuJianrong/744c629dc2b093dc661525c83ac504f9 to your computer and use it in GitHub Desktop.
gist to load the stars and watchs from the <a> link
! function () {
window.addEventListener('mousemove', async ev => {
const path = ev.path || ev.composedPath();
const elm = path.find(ev => ev.tagName === 'A');
if (!elm || elm.visited) {
return;
}
elm.visited = true;
const parts = /.*github\.com\/(.*?)\/(.*?)\/.*/.exec(elm.href + "/");
if (!parts) {
return;
}
const [_, user, repo] = parts;
const url = `https://api.github.com/repos/${user}/${repo}`;
var headers = new Headers(),
username = '****@gmail.com',
password = '****';
headers.append('Authorization', 'Basic ' + btoa(username + ':' + password));
try {
const res = await fetch(url, {
headers
});
const json = await res.json();
let r = document.createElement('span');
r.innerHTML = "(star:" + json.watchers + ", forks:" + json.forks + ")";
r.style.color = '#F44';
elm.appendChild(r);
} catch (err) {
setTimeout(() => elm.visited = !1, 1000);
var r = document.createTextNode("(error[" + err + ")");
r.style.color = '#F44';
elm.appendChild(r)
};
}, true);
}();
void 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment