-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Agregado boton git history cuando ingresas por primera vez a un archi…
…vo. Fix #2
- Loading branch information
1 parent
08e6bda
commit 11e8435
Showing
1 changed file
with
43 additions
and
9 deletions.
There are no files selected for viewing
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
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; | ||
} | ||
} |