Skip to content

Commit

Permalink
Initial commit - v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Ferretti committed Oct 18, 2020
0 parents commit 14e5114
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.ignoreme
_generated_builds
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Forks Diff Counter for GitHub

## How to pack

Use the `pack.sh` script to generate the zip file.
8 changes: 8 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"appName": {
"message": "Forks Diff Counter for GitHub"
},
"appDescription": {
"message": "This extension will show commits behind and ahead for every fork."
}
}
Binary file added icons/icon.128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon.16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon.32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon.48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon.64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions icons/source.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 105 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
(function() {
"use strict";

function appendSpace(element) {
element.appendChild(document.createTextNode(" "));
}

function loadDiff(repo) {
// Add spinner
let spinner = document.createElement("img");
spinner.src = "https://github.githubassets.com/images/spinners/octocat-spinner-32.gif";
spinner.width = 16;
spinner.height = 16;
spinner.style.verticalAlign = "middle";
repo.appendChild(spinner);

// Extract repo URL
let repoUrl = repo.getElementsByTagName("a")[2].href;

let commitsAhead = 0;
let commitsBehind = 0;

let request = new XMLHttpRequest();
request.addEventListener("load", function() {
let body = this.responseText;

// Remove spinner
repo.removeChild(spinner);

// Check if it's even
if (body.includes("This branch is even with")) {
let commitSpan = document.createElement("span");
commitSpan.className = "text-gray";
commitSpan.appendChild(document.createTextNode("is even"));
repo.appendChild(commitSpan);
return;
}

// Extract ahead and behind
let commitHistory = body.match("This branch is ([0-9]*) commit.+ahead, ([0-9]*) commit");
if (commitHistory != null) {
commitsAhead = parseInt(commitHistory[1]);
commitsBehind = parseInt(commitHistory[2]);
} else {
// Extract ahead
let commitHistory = body.match("This branch is ([0-9]*) commit.+ahead");
if (commitHistory != null) {
commitsAhead = parseInt(commitHistory[1]);
}

// Extract behind
commitHistory = body.match("This branch is ([0-9]*) commit.+behind");
if (commitHistory != null) {
commitsBehind = parseInt(commitHistory[1]);
}
}

// Add ahead commits
if (commitsAhead != 0) {
let commit_ahead_counter = document.createElement("span");
commit_ahead_counter.className = "cadd";
commit_ahead_counter.appendChild(document.createTextNode("+" + commitsAhead));
repo.appendChild(commit_ahead_counter);
}

// Add space
appendSpace(repo);

// Add behind commits
if (commitsBehind != 0) {
let commit_behind_counter = document.createElement("span");
commit_behind_counter.className = "cdel";
commit_behind_counter.appendChild(document.createTextNode("-" + commitsBehind));
repo.appendChild(commit_behind_counter);
}
});

// Send request
request.open("GET", repoUrl);
request.send();
}

const network = document.getElementById("network");

function loadButtonAction(e) {
e.target.setAttribute("class", "btn btn-sm disabled");
e.target.removeEventListener("click", loadButtonAction, false);

// Iterate through all
const repos = network.children;
for (let i = 0; i < repos.length; i++) {
if (repos[i].getElementsByClassName("network-tree").length === 0) continue; // Skip original

loadDiff(repos[i]);
}
}

const loadButton = document.createElement("button");
loadButton.className = "btn btn-sm";
loadButton.style.float = "right";
loadButton.innerHTML = '<svg style="fill: currentColor;" class="octicon" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M5 3.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zm0 2.122a2.25 2.25 0 10-1.5 0v.878A2.25 2.25 0 005.75 8.5h1.5v2.128a2.251 2.251 0 101.5 0V8.5h1.5a2.25 2.25 0 002.25-2.25v-.878a2.25 2.25 0 10-1.5 0v.878a.75.75 0 01-.75.75h-4.5A.75.75 0 015 6.25v-.878zm3.75 7.378a.75.75 0 11-1.5 0 .75.75 0 011.5 0zm3-8.75a.75.75 0 100-1.5.75.75 0 000 1.5z"></path></svg> Load diff';
loadButton.addEventListener("click", loadButtonAction);

network.insertBefore(loadButton, network.childNodes[0]);
})();
25 changes: 25 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"manifest_version": 2,
"name": "__MSG_appName__",
"version": "1.0.2",
"description": "__MSG_appDescription__",
"default_locale": "en",
"icons": {
"16": "icons/icon.16.png",
"32": "icons/icon.32.png",
"48": "icons/icon.48.png",
"64": "icons/icon.64.png",
"128": "icons/icon.128.png"
},
"content_scripts": [
{
"matches": [
"*://github.com/*/network/members"
],
"js": [
"js/main.js"
],
"run_at": "document_end"
}
]
}
17 changes: 17 additions & 0 deletions pack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

VERSION="1.0.2"
ICONS_SIZES="16 32 48 64 128"
OUTPUT_FOLDER="_generated_builds"

# Prepare icons
for size in ${ICONS_SIZES}; do
(
cd icons || exit 1
inkscape -w "${size}" -h "${size}" source.svg --export-filename "icon.${size}.png"
)
done

# Pack to zip
mkdir "${OUTPUT_FOLDER}"
7z a "${OUTPUT_FOLDER}/${VERSION}.zip" -x!pack.sh -x!icons/source.svg -x!"${OUTPUT_FOLDER}"

0 comments on commit 14e5114

Please sign in to comment.