Skip to content

Instantly share code, notes, and snippets.

@GenbuHase
Created December 29, 2016 03:36
Show Gist options
  • Save GenbuHase/6d356cee4bd883c6c573d4ffd294fd9b to your computer and use it in GitHub Desktop.
Save GenbuHase/6d356cee4bd883c6c573d4ffd294fd9b to your computer and use it in GitHub Desktop.
Data Helper Release 2.0 == Ajaxについて
with (new DataHelper()) {
let GitAPI = function (Token) {
Gitthis = this;
this.Token = Token;
this.Repo = {
RepoURL: "",
File: {
Get: function (Path, Branch) {
return JSON.parse(Net.Ajax("GET", "https://api.github.com/repos/" + Gitthis.Repo.RepoURL + "/contents/" + Path + "?ref=" + (Branch ? Branch : "master") + "&access_token=" + Gitthis.Token, null).response);
},
Create: function (Path, Branch, Message) {
if (!Gitthis.Repo.File.IsVaild(Path, Branch)) {
Net.Ajax("PUT", "https://api.github.com/repos/" + Gitthis.Repo.RepoURL + "/contents/" + Path + "?access_token=" + Gitthis.Token, {
message: Message ? Message : "",
content: "",
branch: Branch ? Branch : "master"
});
clientMessage("<Sync Helper || Repo.File.Create> it has finished without any problems.");
}
},
Delete: function (Path, Branch, Message) {
if (Gitthis.Repo.File.IsVaild(Path, Branch)) {
Net.Ajax("DELETE", "https://api.github.com/repos/" + Gitthis.Repo.RepoURL + "/contents/" + Path + "?access_token=" + Gitthis.Token, {
message: Message ? Message : "",
branch: Branch ? Branch : "master",
sha: Gitthis.Repo.File.Get(Path, Branch).sha
});
clientMessage("<Sync Helper || Repo.File.Delete> it has finished without any problems.");
}
},
Write: function (Path, Content, Branch, Message) {
if (Gitthis.Repo.File.IsVaild(Path, Branch)) {
Net.Ajax("PUT", "https://api.github.com/repos/" + Gitthis.Repo.RepoURL + "/contents/" + Path + "?access_token=" + Gitthis.Token, {
message: Message ? Message : "",
content: btoa(unescape(encodeURIComponent(Content))),
branch: Branch ? Branch : "master",
sha: Gitthis.Repo.File.Get(Path, Branch).sha
});
clientMessage("<Sync Helper || Repo.File.Write> it has finished without any problems.");
}
},
Read: function (Path, Branch) {
let FileGetter = Net.Ajax("GET", "https://api.github.com/repos/" + Gitthis.Repo.RepoURL + "/contents/" + Path + "?ref=" + (Branch ? Branch : "master") + "&access_token=" + Gitthis.Token, null);
let ContentGetter = Net.Ajax("GET", JSON.parse(FileGetter.response).git_url + "?access_token=" + Gitthis.Token, null);
return android.util.Base64.decode(JSON.parse(ContentGetter.response).content, android.util.Base64.DEFAULT);
},
IsVaild: function (Path, Branch) {
return Net.Ajax("GET", "https://api.github.com/repos/" + Gitthis.Repo.RepoURL + "/contents/" + Path + "?ref=" + (Branch ? Branch : "master") + "&access_token=" + Gitthis.Token, null).status == 404 ? false : true;
}
}
}
}
}
var GitAPI = function (Token) {
Gitthis = this;
this.Token = Token;
this.Repo = {
RepoURL: "",
File: {
Get: function (Path, Branch) {
var FileGetter = new XMLHttpRequest();
FileGetter.open("GET", "https://api.github.com/repos/" + Gitthis.Repo.RepoURL + "/contents/" + Path + "?ref=" + (Branch ? Branch : "master") + "&access_token=" + Gitthis.Token, false);
FileGetter.send(null);
return JSON.parse(FileGetter.responseText);
},
Create: function (Path, Branch, Message) {
if (Gitthis.Repo.File.IsVaild(Path, Branch) == false) {
var FileCreator = new XMLHttpRequest();
FileCreator.open("PUT", "https://api.github.com/repos/" + Gitthis.Repo.RepoURL + "/contents/" + Path + "?access_token=" + Gitthis.Token, true);
FileCreator.onload = function (Event) {
console.log("<Sync Helper || Repo.File.Create> it has finished without any problems.");
}
FileCreator.send(
JSON.stringify(
{
message: Message ? Message : "",
content: "",
branch: Branch ? Branch : "master"
}
)
);
}
},
Delete: function (Path, Branch, Message) {
if (Gitthis.Repo.File.IsVaild(Path, Branch) == true) {
var FileDeleter = new XMLHttpRequest();
FileDeleter.open("DELETE", "https://api.github.com/repos/" + Gitthis.Repo.RepoURL + "/contents/" + Path + "?access_token=" + Gitthis.Token, true);
FileDeleter.onload = function (Event) {
console.log("<Sync Helper || Repo.File.Delete> it has finished without any problems.");
}
FileDeleter.send(
JSON.stringify(
{
message: Message ? Message : "",
branch: Branch ? Branch : "master",
sha: Gitthis.Repo.File.Get(Path, Branch).sha
}
)
);
}
},
Write: function (Path, Content, Branch, Message) {
if (Gitthis.Repo.File.IsVaild(Path, Branch) == true) {
var FileUpdater = new XMLHttpRequest();
FileUpdater.open("PUT", "https://api.github.com/repos/" + Gitthis.Repo.RepoURL + "/contents/" + Path + "?access_token=" + Gitthis.Token, true);
FileUpdater.onload = function (Event) {
console.log("<Sync Helper || Repo.File.Write> it has finished without any problems.");
}
FileUpdater.send(
JSON.stringify(
{
message: Message ? Message : "",
content: btoa(unescape(encodeURIComponent(Content))),
branch: Branch ? Branch : "master",
sha: Gitthis.Repo.File.Get(Path, Branch).sha
}
)
);
}
},
Read: function (Path, Branch) {
var FileGetter = new XMLHttpRequest();
FileGetter.open("GET", "https://api.github.com/repos/" + Gitthis.Repo.RepoURL + "/contents/" + Path + "?ref=" + (Branch ? Branch : "master") + "&access_token=" + Gitthis.Token, false);
FileGetter.send(null);
var ContentGetter = new XMLHttpRequest();
ContentGetter.open("GET", JSON.parse(FileGetter.responseText).git_url + "?access_token=" + Gitthis.Token, false);
ContentGetter.send(null);
return atob(JSON.parse(ContentGetter.responseText).content);
},
Download: function (Path, Branch) {
var Data = new Blob([Gitthis.Repo.File.Read(Path, Branch)], {
type: "Text/Plain"
});
if (window.navigator.msSaveBlob) {
window.navigator.msSaveBlob(Data, Gitthis.Repo.File.Get(Path, Branch).name);
} else {
Link = document.createElement("A");
Link.href = URL.createObjectURL(Data);
Link.download = Gitthis.Repo.File.Get(Path, Branch).name;
Link.target = "_blank";
var Click = document.createEvent("MouseEvents");
Click.initEvent("click", false, true);
Link.dispatchEvent(Click);
URL.revokeObjectURL(Data);
}
},
IsVaild: function (Path, Branch) {
var VaildChecker = new XMLHttpRequest();
VaildChecker.open("GET", "https://api.github.com/repos/" + Gitthis.Repo.RepoURL + "/contents/" + Path + "?ref=" + (Branch ? Branch : "master") + "&access_token=" + Gitthis.Token, false);
VaildChecker.send(null);
return VaildChecker.status == 404 ? false : true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment