Last active
February 21, 2019 00:27
-
-
Save YuJianrong/744c629dc2b093dc661525c83ac504f9 to your computer and use it in GitHub Desktop.
gist to load the stars and watchs from the <a> link
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
! 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