-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitzip-button.js
53 lines (46 loc) · 2.36 KB
/
gitzip-button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//fix for pjax
document.addEventListener("pjax:success", addButtons);
setToken();
addButtons();
GitZip.registerCallback(inputFn, this);
function addButtons() {
//folder button
if (!document.getElementsByClassName("select-menu get-repo-select-menu js-menu-container float-right select-menu-modal-right")[0]) {
var downloadButton = '<div id="downloadButton" class="select-menu get-repo-select-menu js-menu-container float-right select-menu-modal-right"><button class="btn btn-sm btn-primary" type="button" tabindex="0"><span>Download</span> </button></div>';
var container = document.getElementsByClassName("file-navigation")[0];
container.innerHTML = downloadButton + container.innerHTML;
var downloadButtonElement = document.getElementById("downloadButton");
downloadButtonElement.onclick = function () {
startDownload(document.URL)
};
}
//file buttons
if(!document.getElementById("downloadButton0") && !document.getElementById("downloadButton1"))
var entries = document.getElementsByClassName("files js-navigation-container js-active-navigation-container")[0].getElementsByClassName("js-navigation-item");
for (var i = 0; i < entries.length; i++) {
var tr = entries[i];
if (tr.getElementsByTagName("a")[0].getAttribute("title") != "Go to parent directory") {
tr.innerHTML = tr.innerHTML + '<td class="age"><a href="#" id="downloadButton' + i + '" >Download</a></td>';
document.getElementById("downloadButton" + i).onclick = function (e) {
startDownload(e.target.parentElement.parentElement.getElementsByClassName("content")[0].getElementsByTagName("a")[0].getAttribute("href"))
};
}
}
}
function startDownload(url) {
if (!url.startsWith("https://github.com"))
url = "https://github.com" + url;
GitZip.zipRepo(url);
}
function inputFn(status, message, percent) {
console.log("status: " + status, "; message: " + message + "; percent: " + percent + ";");
if (status == "error") {
window.alert("Error: "+ message);
window.alert("Go to settings page of this addon to set your access token.")
}
}
function setToken() {
chrome.runtime.sendMessage({context:"getData",dataKey:"token"}, function(message){
GitZip.setAccessToken(message.data);
})
}