Skip to content

Instantly share code, notes, and snippets.

@simonwep
Last active January 15, 2020 15:58
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 simonwep/d32b18a2d807fdba492aa29579100bc6 to your computer and use it in GitHub Desktop.
Save simonwep/d32b18a2d807fdba492aa29579100bc6 to your computer and use it in GitHub Desktop.
Enhanced contribution info
// ==UserScript==
// @name GitHub contribution hints
// @version 1.0
// @namespace http://tampermonkey.net/
// @description A few more infos about your contribs on the front page
// @match https://*.github.com/*
// @grant none
// @run-at document-end
// ==/UserScript==
(() => {
const headerText = document.querySelector('.js-yearly-contributions h2.f4');
const dailyContributions = [
...document.querySelectorAll('.js-calendar-graph-svg .day')
].map(v => ({
count: Number(v.dataset.count),
date: new Date(v.dataset.date)
}));
// Daily stuff
const today = dailyContributions[dailyContributions.length - 1];
const todayLastYearDate = (() => {
const date = new Date(today.date.getTime());
date.setFullYear(today.date.getFullYear() - 1);
return date.getTime();
})();
const todayLastYear = dailyContributions.find(v => v.date.getTime() === todayLastYearDate);
const todayDiff = todayLastYear ? today.count - todayLastYear.count : null;
const todayMet = todayDiff !== null ? todayDiff >= 0 : null;
// Weekly stuff
const weeklyAverage = Math.ceil((() => {
let weeks = 0;
let total = 0;
for (let i = 0, sum = 0; i < dailyContributions.length; i++) {
sum += dailyContributions[i].count;
if (!(i % 7)) {
total += sum;
weeks++;
sum = 0;
}
}
return total / weeks;
})());
const thisWeek = (() => {
let total = 0;
for (let i = dailyContributions.length - 1; i >= 0; i--) {
const item = dailyContributions[i];
total += item.count;
if (item.date.getDay() === 1) {
break;
}
}
return total;
})();
const weeklyDiff = thisWeek - weeklyAverage;
const weeklyAverageMet = weeklyDiff >= 0;
const additionalHeader = headerText.cloneNode(true);
additionalHeader.innerHTML = `
<p>
<span>AVG Week: <b>${weeklyAverage}</b></span>
<span>/</span>
<span style="color: ${weeklyAverageMet ? 'green' : 'red'}">
Week: <b>${thisWeek}</b>
${weeklyAverageMet ? `(+${weeklyDiff})` : `(${weeklyAverage - thisWeek} to go)`}
</span>
${todayLastYear ? `
<span>/</span>
<span style="color: ${todayMet ? 'green' : 'red'}">
Today: ${today.count} of ${todayLastYear.count}
${todayMet ? `(+${todayDiff})` : `(${Math.abs(todayDiff)} to go)`}
</span>
` : ''}
</p>
`;
headerText.insertAdjacentElement('afterend', additionalHeader);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment