Skip to content

Commit

Permalink
Agregado boton git history cuando ingresas por primera vez a un archi…
Browse files Browse the repository at this point in the history
…vo. Fix #2
  • Loading branch information
LuisReinoso committed Feb 16, 2019
1 parent 08e6bda commit 11e8435
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions extension/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
let url = chrome ? document.URL : window.location.href;
const auxUrl = url.split('/');
auxUrl[2] = 'github.githistory.xyz'
url = auxUrl.join('/');
this.isButtonInserted(document.URL);
let oldUrl = document.URL;
// It's necessary because git use pjax to refresh views
// https://stackoverflow.com/questions/3522090/event-when-window-location-href-changes#46428962
window.onload = () => {
let bodyList = document.querySelector('body');
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (oldUrl != document.URL) {
if (isButtonInserted(document.URL)) {
oldUrl = document.URL;
}
}
});
});

const buttonGithubHistory = document.createElement('a');
buttonGithubHistory.innerHTML = 'Open in Git History';
buttonGithubHistory.setAttribute('class', 'btn btn-sm BtnGroup-item');
buttonGithubHistory.setAttribute('href', url);
document.getElementById("raw-url").parentNode.appendChild(buttonGithubHistory);
let config = {
childList: true,
subtree: true
};
observer.observe(bodyList, config);
};

function isButtonInserted(url) {
if (/https:\/\/github\.com\/[a-zA-Z0-9-_]*\/[a-zA-Z0-9-_]*\/blob\/.*/.test(url)) {
const auxUrl = url.split('/');
auxUrl[2] = 'github.githistory.xyz';
url = auxUrl.join('/');
const buttonGithubHistory = document.createElement('a');
buttonGithubHistory.innerHTML = 'Open in Git History';
buttonGithubHistory.setAttribute('class', 'btn btn-sm BtnGroup-item');
buttonGithubHistory.setAttribute('href', url);
try {
document
.getElementById('raw-url')
.parentNode.appendChild(buttonGithubHistory);
return true;
} catch (error) {
return false;
}
} else {
return false;
}
}

0 comments on commit 11e8435

Please sign in to comment.