Skip to content

Commit

Permalink
Merge pull request #186 from mdo/xmr/dev
Browse files Browse the repository at this point in the history
Modernize JS
  • Loading branch information
XhmikosR authored Dec 8, 2022
2 parents bd27a1c + 04cc9fc commit 23c200b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 68 deletions.
2 changes: 1 addition & 1 deletion docs/github-btn.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang=en><meta charset=utf-8><style>body{padding:0;margin:0;overflow:hidden;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif;font-size:11px;font-weight:700;line-height:14px}.github-btn{height:20px;overflow:hidden}.gh-btn,.gh-count,.gh-ico{float:left}.gh-btn,.gh-count{padding:2px 5px 2px 4px;color:#333;text-decoration:none;white-space:nowrap;cursor:pointer;border-radius:3px}.gh-btn{background-color:#eee;background-image:-webkit-gradient(linear,left top,left bottom,color-stop(0,#fcfcfc),to(#eee));background-image:linear-gradient(to bottom,#fcfcfc 0,#eee 100%);background-repeat:no-repeat;border:1px solid #d5d5d5}.gh-btn:focus,.gh-btn:hover{text-decoration:none;background-color:#ddd;background-image:-webkit-gradient(linear,left top,left bottom,color-stop(0,#eee),to(#ddd));background-image:linear-gradient(to bottom,#eee 0,#ddd 100%);border-color:#ccc}.gh-btn:active{background-color:#dcdcdc;background-image:none;border-color:#b5b5b5;box-shadow:inset 0 2px 4px rgba(0,0,0,.15)}.gh-ico{width:14px;height:14px;margin-right:4px;background:url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40' viewBox='12 12 40 40'%3e%3cpath fill='%23333' d='M32 13.4c-10.5 0-19 8.5-19 19 0 8.4 5.5 15.5 13 18 1 .2 1.3-.4 1.3-.9v-3.2c-5.3 1.1-6.4-2.6-6.4-2.6-.9-2.1-2.1-2.7-2.1-2.7-1.7-1.2.1-1.1.1-1.1 1.9.1 2.9 2 2.9 2 1.7 2.9 4.5 2.1 5.5 1.6.2-1.2.7-2.1 1.2-2.6-4.2-.5-8.7-2.1-8.7-9.4 0-2.1.7-3.7 2-5.1-.2-.5-.8-2.4.2-5 0 0 1.6-.5 5.2 2 1.5-.4 3.1-.7 4.8-.7 1.6 0 3.3.2 4.7.7 3.6-2.4 5.2-2 5.2-2 1 2.6.4 4.6.2 5 1.2 1.3 2 3 2 5.1 0 7.3-4.5 8.9-8.7 9.4.7.6 1.3 1.7 1.3 3.5v5.2c0 .5.4 1.1 1.3.9 7.5-2.6 13-9.7 13-18.1 0-10.5-8.5-19-19-19z'/%3e%3c/svg%3e") 0 0/100% 100% no-repeat}.gh-count{position:relative;display:none;margin-left:4px;background-color:#fafafa;border:1px solid #d4d4d4}.gh-count:focus,.gh-count:hover{color:#0366d6}.gh-count::after,.gh-count::before{position:absolute;display:inline-block;width:0;height:0;content:"";border-color:transparent;border-style:solid}.gh-count::before{top:50%;left:-3px;margin-top:-4px;border-width:4px 4px 4px 0;border-right-color:#fafafa}.gh-count::after{top:50%;left:-4px;z-index:-1;margin-top:-5px;border-width:5px 5px 5px 0;border-right-color:#d4d4d4}.github-btn-large{height:30px}.github-btn-large .gh-btn,.github-btn-large .gh-count{padding:3px 10px 3px 8px;font-size:16px;line-height:22px;border-radius:4px}.github-btn-large .gh-ico{width:20px;height:20px}.github-btn-large .gh-count{margin-left:6px}.github-btn-large .gh-count::before{left:-5px;margin-top:-6px;border-width:6px 6px 6px 0}.github-btn-large .gh-count::after{left:-6px;margin-top:-7px;border-width:7px 7px 7px 0}.no-text .gh-ico{margin-right:0}</style><span class=github-btn><a class=gh-btn href=# rel="noopener noreferrer" target=_blank><span class=gh-ico aria-hidden=true></span> <span class=gh-text></span> </a><a class=gh-count href=# rel="noopener noreferrer" target=_blank aria-hidden=true></a></span><script>!function(){"use strict";function e(t){return String(t).replace(/(\d)(?=(\d{3})+$)/g,"$1,")}var t,a=function(){for(var t,e=[],a=window.location,r=a.href.slice(a.href.indexOf("?")+1).split("&"),s=0;s<r.length;s++)t=r[s].split("="),e.push(t[0]),e[t[0]]=t[1];return e}(),r=a.user,s=a.repo,n=a.type,o=a.count,c=a.size,l=a.v,a=a.text,i=document.querySelector(".gh-btn"),u=document.querySelector(".github-btn"),b=document.querySelector(".gh-text"),h=document.querySelector(".gh-count"),f=" on GitHub",d="https://github.com/",g="https://api.github.com/",x=d+r+"/"+s,C=r+"/"+s;switch(window.callback=function(t){if("Not Found"!==t.data.message){switch(n){case"watch":"2"===l?(h.textContent=t.data.subscribers_count&&e(t.data.subscribers_count),h.setAttribute("aria-label",h.textContent+" watchers"+f)):(h.textContent=t.data.stargazers_count&&e(t.data.stargazers_count),h.setAttribute("aria-label",h.textContent+" stargazers"+f));break;case"star":h.textContent=t.data.stargazers_count&&e(t.data.stargazers_count),h.setAttribute("aria-label",h.textContent+" stargazers"+f);break;case"fork":h.textContent=t.data.network_count&&e(t.data.network_count),h.setAttribute("aria-label",h.textContent+" forks"+f);break;case"follow":h.textContent=t.data.followers&&e(t.data.followers),h.setAttribute("aria-label",h.textContent+" followers"+f)}"true"===o&&""!==h.textContent&&(h.style.display="block",h.removeAttribute("aria-hidden"))}},i.href=x,n){case"watch":"2"===l?(u.className+=" github-watchers",b.textContent="Watch",h.href=x+"/watchers"):(u.className+=" github-stargazers",b.textContent="Star",h.href=x+"/stargazers"),t=b.textContent+" "+C;break;case"star":u.className+=" github-stargazers",b.textContent="Star",h.href=x+"/stargazers",t=b.textContent+" "+C;break;case"fork":u.className+=" github-forks",b.textContent="Fork",i.href=x+"/fork",h.href=x+"/network",t=b.textContent+" "+C;break;case"follow":u.className+=" github-me",b.textContent="Follow @"+r,i.href=d+r,h.href=d+r+"?tab=followers",t=b.textContent;break;case"sponsor":u.className+=" github-me",b.textContent="Sponsor @"+r,i.href=d+"sponsors/"+r,t=b.textContent}"false"===a&&(i.className+=" no-text",b.setAttribute("aria-hidden",!0),b.style.display="none",b.textContent=""),i.setAttribute("aria-label",t+f),document.title=t+f,"large"===c&&(u.className+=" github-btn-large"),"true"===o&&"sponsor"!==n&&"false"!==a&&(c="follow"===n?g+"users/"+r:g+"repos/"+r+"/"+s,a=document.head,(g=document.createElement("script")).src=c+"?callback=callback",a.insertBefore(g,a.firstChild))}()</script>
<!doctype html><html lang=en><meta charset=utf-8><style>body{padding:0;margin:0;overflow:hidden;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif;font-size:11px;font-weight:700;line-height:14px}.github-btn{height:20px;overflow:hidden}.gh-btn,.gh-count,.gh-ico{float:left}.gh-btn,.gh-count{padding:2px 5px 2px 4px;color:#333;text-decoration:none;white-space:nowrap;cursor:pointer;border-radius:3px}.gh-btn{background-color:#eee;background-image:-webkit-gradient(linear,left top,left bottom,color-stop(0,#fcfcfc),to(#eee));background-image:linear-gradient(to bottom,#fcfcfc 0,#eee 100%);background-repeat:no-repeat;border:1px solid #d5d5d5}.gh-btn:focus,.gh-btn:hover{text-decoration:none;background-color:#ddd;background-image:-webkit-gradient(linear,left top,left bottom,color-stop(0,#eee),to(#ddd));background-image:linear-gradient(to bottom,#eee 0,#ddd 100%);border-color:#ccc}.gh-btn:active{background-color:#dcdcdc;background-image:none;border-color:#b5b5b5;box-shadow:inset 0 2px 4px rgba(0,0,0,.15)}.gh-ico{width:14px;height:14px;margin-right:4px;background:url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40' viewBox='12 12 40 40'%3e%3cpath fill='%23333' d='M32 13.4c-10.5 0-19 8.5-19 19 0 8.4 5.5 15.5 13 18 1 .2 1.3-.4 1.3-.9v-3.2c-5.3 1.1-6.4-2.6-6.4-2.6-.9-2.1-2.1-2.7-2.1-2.7-1.7-1.2.1-1.1.1-1.1 1.9.1 2.9 2 2.9 2 1.7 2.9 4.5 2.1 5.5 1.6.2-1.2.7-2.1 1.2-2.6-4.2-.5-8.7-2.1-8.7-9.4 0-2.1.7-3.7 2-5.1-.2-.5-.8-2.4.2-5 0 0 1.6-.5 5.2 2 1.5-.4 3.1-.7 4.8-.7 1.6 0 3.3.2 4.7.7 3.6-2.4 5.2-2 5.2-2 1 2.6.4 4.6.2 5 1.2 1.3 2 3 2 5.1 0 7.3-4.5 8.9-8.7 9.4.7.6 1.3 1.7 1.3 3.5v5.2c0 .5.4 1.1 1.3.9 7.5-2.6 13-9.7 13-18.1 0-10.5-8.5-19-19-19z'/%3e%3c/svg%3e") 0 0/100% 100% no-repeat}.gh-count{position:relative;display:none;margin-left:4px;background-color:#fafafa;border:1px solid #d4d4d4}.gh-count:focus,.gh-count:hover{color:#0366d6}.gh-count::after,.gh-count::before{position:absolute;display:inline-block;width:0;height:0;content:"";border-color:transparent;border-style:solid}.gh-count::before{top:50%;left:-3px;margin-top:-4px;border-width:4px 4px 4px 0;border-right-color:#fafafa}.gh-count::after{top:50%;left:-4px;z-index:-1;margin-top:-5px;border-width:5px 5px 5px 0;border-right-color:#d4d4d4}.github-btn-large{height:30px}.github-btn-large .gh-btn,.github-btn-large .gh-count{padding:3px 10px 3px 8px;font-size:16px;line-height:22px;border-radius:4px}.github-btn-large .gh-ico{width:20px;height:20px}.github-btn-large .gh-count{margin-left:6px}.github-btn-large .gh-count::before{left:-5px;margin-top:-6px;border-width:6px 6px 6px 0}.github-btn-large .gh-count::after{left:-6px;margin-top:-7px;border-width:7px 7px 7px 0}.no-text .gh-ico{margin-right:0}</style><span class=github-btn><a class=gh-btn href=# rel="noopener noreferrer" target=_blank><span class=gh-ico aria-hidden=true></span> <span class=gh-text></span> </a><a class=gh-count href=# rel="noopener noreferrer" target=_blank aria-hidden=true></a></span><script>!function(){"use strict";const o=new Set(["user","repo","type","count","size","text","v"]);function e(t){return String(t).replace(/(\d)(?=(\d{3})+$)/g,"$1,")}function t(t){var e=document.createElement("script");e.src=t+"?callback=callback",document.head.insertBefore(e,document.head.firstChild)}var a=function(){var t=window.location.href.slice(window.location.href.indexOf("?")+1).split("&"),e=new Map;for(const s of t){var[a,r]=s.split("=");o.has(a)&&e.set(a,r)}return e}(),r=a.get("user"),s=a.get("repo");const n=a.get("type"),c=a.get("count");var i=a.get("size"),l=a.get("text");const u=a.get("v");var d=document.querySelector(".gh-btn"),b=document.querySelector(".github-btn"),h=document.querySelector(".gh-text");const f=document.querySelector(".gh-count"),g="on GitHub";var x="https://github.com/",a="https://api.github.com/",w=x+r+"/"+s,C=r+"/"+s;window.callback=function(t){if("Not Found"!==t.data.message){switch(n){case"watch":"2"===u?(f.textContent=t.data.subscribers_count&&e(t.data.subscribers_count),f.setAttribute("aria-label",f.textContent+" watchers "+g)):(f.textContent=t.data.stargazers_count&&e(t.data.stargazers_count),f.setAttribute("aria-label",f.textContent+" stargazers "+g));break;case"star":f.textContent=t.data.stargazers_count&&e(t.data.stargazers_count),f.setAttribute("aria-label",f.textContent+" stargazers "+g);break;case"fork":f.textContent=t.data.network_count&&e(t.data.network_count),f.setAttribute("aria-label",f.textContent+" forks "+g);break;case"follow":f.textContent=t.data.followers&&e(t.data.followers),f.setAttribute("aria-label",f.textContent+" followers "+g)}"true"===c&&""!==f.textContent&&(f.style.display="block",f.removeAttribute("aria-hidden"))}},d.href=w;let k;switch(n){case"watch":"2"===u?(b.classList.add("github-watchers"),h.textContent="Watch",f.href=w+"/watchers"):(b.classList.add("github-stargazers"),h.textContent="Star",f.href=w+"/stargazers"),k=h.textContent+" "+C;break;case"star":b.classList.add("github-stargazers"),h.textContent="Star",f.href=w+"/stargazers",k=h.textContent+" "+C;break;case"fork":b.classList.add("github-forks"),h.textContent="Fork",d.href=w+"/fork",f.href=w+"/network",k=h.textContent+" "+C;break;case"follow":b.classList.add("github-me"),h.textContent="Follow @"+r,d.href=x+r,f.href=x+r+"?tab=followers",k=h.textContent;break;case"sponsor":b.classList.add("github-me"),h.textContent="Sponsor @"+r,d.href=x+"sponsors/"+r,k=h.textContent}"false"===l&&(d.classList.add("no-text"),h.setAttribute("aria-hidden",!0),h.style.display="none",h.textContent=""),d.setAttribute("aria-label",k+" "+g),document.title=k+" "+g,"large"===i&&b.classList.add("github-btn-large"),"true"===c&&"sponsor"!==n&&"false"!==l&&("follow"===n?t(a+"users/"+r):t(a+`repos/${r}/`+s))}()</script>
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,19 @@
"browser"
],
"parserOptions": {
"ecmaVersion": 5,
"ecmaVersion": 2015,
"sourceType": "script"
},
"space": true,
"rules": {
"capitalized-comments": "off",
"default-case": "off",
"no-negated-condition": "off",
"no-var": "off",
"prefer-destructuring": "off",
"object-curly-spacing": [
"error",
"always"
],
"prefer-template": "error",
"space-before-function-paren": [
"error",
"never"
Expand Down
127 changes: 63 additions & 64 deletions src/js.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
(function() {
'use strict';

// Read a page's GET URL variables and return them as an associative array.
// Source: https://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
const allowedQueryParams = new Set(['user', 'repo', 'type', 'count', 'size', 'text', 'v']);

function getUrlParameters() {
var vars = [];
var hash;
var location = window.location;
var hashes = location.href.slice(location.href.indexOf('?') + 1).split('&');

for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
// TODO: Replace with URLSearchParams later
const hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
const parameters = new Map();

for (const hash of hashes) {
const [parameter, value] = hash.split('=');

if (allowedQueryParams.has(parameter)) {
parameters.set(parameter, value);
}
}

return vars;
return parameters;
}

// Add commas to numbers
Expand All @@ -24,36 +25,34 @@
}

function jsonp(path) {
var head = document.head;
var script = document.createElement('script');
const script = document.createElement('script');

script.src = path + '?callback=callback';
head.insertBefore(script, head.firstChild);
script.src = `${path}?callback=callback`;
document.head.insertBefore(script, document.head.firstChild);
}

var parameters = getUrlParameters();

// Parameters
var user = parameters.user;
var repo = parameters.repo;
var type = parameters.type;
var count = parameters.count;
var size = parameters.size;
var v = parameters.v;
var noText = parameters.text;
const parameters = getUrlParameters();
const user = parameters.get('user');
const repo = parameters.get('repo');
const type = parameters.get('type');
const count = parameters.get('count');
const size = parameters.get('size');
const noText = parameters.get('text');
const v = parameters.get('v');

// Elements
var button = document.querySelector('.gh-btn');
var mainButton = document.querySelector('.github-btn');
var text = document.querySelector('.gh-text');
var counter = document.querySelector('.gh-count');
const button = document.querySelector('.gh-btn');
const mainButton = document.querySelector('.github-btn');
const text = document.querySelector('.gh-text');
const counter = document.querySelector('.gh-count');

// Constants
var LABEL_SUFFIX = ' on GitHub';
var GITHUB_URL = 'https://github.com/';
var API_URL = 'https://api.github.com/';
var REPO_URL = GITHUB_URL + user + '/' + repo;
var USER_REPO = user + '/' + repo;
const LABEL_SUFFIX = 'on GitHub';
const GITHUB_URL = 'https://github.com/';
const API_URL = 'https://api.github.com/';
const REPO_URL = `${GITHUB_URL + user}/${repo}`;
const USER_REPO = `${user}/${repo}`;

window.callback = function(obj) {
if (obj.data.message === 'Not Found') {
Expand All @@ -64,30 +63,30 @@
case 'watch': {
if (v === '2') {
counter.textContent = obj.data.subscribers_count && addCommas(obj.data.subscribers_count);
counter.setAttribute('aria-label', counter.textContent + ' watchers' + LABEL_SUFFIX);
counter.setAttribute('aria-label', `${counter.textContent} watchers ${LABEL_SUFFIX}`);
} else {
counter.textContent = obj.data.stargazers_count && addCommas(obj.data.stargazers_count);
counter.setAttribute('aria-label', counter.textContent + ' stargazers' + LABEL_SUFFIX);
counter.setAttribute('aria-label', `${counter.textContent} stargazers ${LABEL_SUFFIX}`);
}

break;
}

case 'star': {
counter.textContent = obj.data.stargazers_count && addCommas(obj.data.stargazers_count);
counter.setAttribute('aria-label', counter.textContent + ' stargazers' + LABEL_SUFFIX);
counter.setAttribute('aria-label', `${counter.textContent} stargazers ${LABEL_SUFFIX}`);
break;
}

case 'fork': {
counter.textContent = obj.data.network_count && addCommas(obj.data.network_count);
counter.setAttribute('aria-label', counter.textContent + ' forks' + LABEL_SUFFIX);
counter.setAttribute('aria-label', `${counter.textContent} forks ${LABEL_SUFFIX}`);
break;
}

case 'follow': {
counter.textContent = obj.data.followers && addCommas(obj.data.followers);
counter.setAttribute('aria-label', counter.textContent + ' followers' + LABEL_SUFFIX);
counter.setAttribute('aria-label', `${counter.textContent} followers ${LABEL_SUFFIX}`);
break;
}
}
Expand All @@ -102,73 +101,73 @@
// Set href to be URL for repo
button.href = REPO_URL;

var title;
let title;

// Add the class, change the text label, set count link href
switch (type) {
case 'watch': {
if (v === '2') {
mainButton.className += ' github-watchers';
mainButton.classList.add('github-watchers');
text.textContent = 'Watch';
counter.href = REPO_URL + '/watchers';
counter.href = `${REPO_URL}/watchers`;
} else {
mainButton.className += ' github-stargazers';
mainButton.classList.add('github-stargazers');
text.textContent = 'Star';
counter.href = REPO_URL + '/stargazers';
counter.href = `${REPO_URL}/stargazers`;
}

title = text.textContent + ' ' + USER_REPO;
title = `${text.textContent} ${USER_REPO}`;
break;
}

case 'star': {
mainButton.className += ' github-stargazers';
mainButton.classList.add('github-stargazers');
text.textContent = 'Star';
counter.href = REPO_URL + '/stargazers';
title = text.textContent + ' ' + USER_REPO;
counter.href = `${REPO_URL}/stargazers`;
title = `${text.textContent} ${USER_REPO}`;
break;
}

case 'fork': {
mainButton.className += ' github-forks';
mainButton.classList.add('github-forks');
text.textContent = 'Fork';
button.href = REPO_URL + '/fork';
counter.href = REPO_URL + '/network';
title = text.textContent + ' ' + USER_REPO;
button.href = `${REPO_URL}/fork`;
counter.href = `${REPO_URL}/network`;
title = `${text.textContent} ${USER_REPO}`;
break;
}

case 'follow': {
mainButton.className += ' github-me';
text.textContent = 'Follow @' + user;
mainButton.classList.add('github-me');
text.textContent = `Follow @${user}`;
button.href = GITHUB_URL + user;
counter.href = GITHUB_URL + user + '?tab=followers';
counter.href = `${GITHUB_URL + user}?tab=followers`;
title = text.textContent;
break;
}

case 'sponsor': {
mainButton.className += ' github-me';
text.textContent = 'Sponsor @' + user;
button.href = GITHUB_URL + 'sponsors/' + user;
mainButton.classList.add('github-me');
text.textContent = `Sponsor @${user}`;
button.href = `${GITHUB_URL}sponsors/${user}`;
title = text.textContent;
break;
}
}

if (noText === 'false') {
button.className += ' no-text';
button.classList.add('no-text');
text.setAttribute('aria-hidden', true);
text.style.display = 'none';
text.textContent = '';
}

button.setAttribute('aria-label', title + LABEL_SUFFIX);
document.title = title + LABEL_SUFFIX;
button.setAttribute('aria-label', `${title} ${LABEL_SUFFIX}`);
document.title = `${title} ${LABEL_SUFFIX}`;

// Change the size if requested
if (size === 'large') {
mainButton.className += ' github-btn-large';
mainButton.classList.add('github-btn-large');
}

// If count is not requested or type is sponsor,
Expand All @@ -178,8 +177,8 @@
}

if (type === 'follow') {
jsonp(API_URL + 'users/' + user);
jsonp(`${API_URL}users/${user}`);
} else {
jsonp(API_URL + 'repos/' + user + '/' + repo);
jsonp(`${API_URL}repos/${user}/${repo}`);
}
})();

0 comments on commit 23c200b

Please sign in to comment.