Skip to content

Instantly share code, notes, and snippets.

@joahg
Created November 26, 2013 17:28
Show Gist options
  • Save joahg/7662451 to your computer and use it in GitHub Desktop.
Save joahg/7662451 to your computer and use it in GitHub Desktop.
Library of AJAX requests that make beta testing Codecademy Projects easier.
(function () {
$.ajax({
url: "http://www.codecademy.com/projects/",
dataType: "json",
type: "POST",
data: {
authentication_token: CCDATA.current_user.authentication_token,
codebit: {
name: prompt("Name your Codebit."),
files: [
{
filename: "index.html",
content: ""
},
{
filename: "stylesheet.css",
content: ""
},
{
filename: "script.js",
content: ""
}
],
source: {
type: "None"
}
}
},
beforeSend: function (e) {
e.setRequestHeader("X-Requested-With", "XMLHttpRequest");
e.setRequestHeader("Accept", "application/json, text/javascript, */*; q=0.01")
e.setRequestHeader("X-CSRF-Token",csrf_token);
},
success: function (e) {
console.log(e);
alert("Codebit Created.")
window.location.href = e['odata.editLink']
},
error: function(e) {
console.log(e);
alert("An error occurred while creating the Codebit. Please check your JS Console.")
}
})
})()
(function () {
var _files = CCDATA.codebits.current.files;
_files.push({
content: "",
filename: prompt("New file name")
})
$.ajax({
url: "http://www.codecademy.com/projects/" + (window.location.href.split("/"))[(window.location.href.split("/")).length - 1] != "" ? (window.location.href.split("/"))[(window.location.href.split("/")).length - 1] : (window.location.href.split("/"))[(window.location.href.split("/")).length - 2],
dataType: "json",
type: "PUT",
data: {
authentication_token: CCDATA.current_user.authentication_token,
codebit: {
id: CCDATA.codebits.current.id,
name: CCDATA.codebits.current.name,
files: _files
}
},
beforeSend: function (e) {
e.setRequestHeader("X-Requested-With", "XMLHttpRequest");
e.setRequestHeader("Accept", "*/*")
e.setRequestHeader("X-CSRF-Token",csrf_token);
},
success: function (e) {
console.log(e);
alert("New file successfully created.");
location.reload();
},
error: function(e) {
console.log(e);
alert("An error occurred while creating the file. Please check your JS Console.")
}
})
})()
(function () {
var _files = CCDATA.codebits.current.files;
_files.splice(parseInt(prompt("Index of file to remove"),10),1)
$.ajax({
url: "http://www.codecademy.com/projects/" + (window.location.href.split("/"))[(window.location.href.split("/")).length - 1] != "" ? (window.location.href.split("/"))[(window.location.href.split("/")).length - 1] : (window.location.href.split("/"))[(window.location.href.split("/")).length - 2],
dataType: "json",
type: "PUT",
data: {
authentication_token: CCDATA.current_user.authentication_token,
codebit: {
id: CCDATA.codebits.current.id,
name: CCDATA.codebits.current.name,
files: _files
}
},
beforeSend: function (e) {
e.setRequestHeader("X-Requested-With", "XMLHttpRequest");
e.setRequestHeader("Accept", "*/*")
e.setRequestHeader("X-CSRF-Token",csrf_token);
},
success: function (e) {
console.log(e);
alert("File successfully removed.");
location.reload();
},
error: function(e) {
console.log(e);
alert("An error occurred while removing the file. Please check your JS Console.")
}
})
})()
(function () {
$.ajax({
url: "http://www.codecademy.com/projects/" + (window.location.href.split("/"))[(window.location.href.split("/")).length - 1] != "" ? (window.location.href.split("/"))[(window.location.href.split("/")).length - 1] : (window.location.href.split("/"))[(window.location.href.split("/")).length - 2],
dataType: "json",
type: "PUT",
data: {
authentication_token: CCDATA.current_user.authentication_token,
codebit: {
id: CCDATA.codebits.current.id,
name: prompt("What name would you like?")
}
},
beforeSend: function (e) {
e.setRequestHeader("X-Requested-With", "XMLHttpRequest");
e.setRequestHeader("Accept", "*/*")
e.setRequestHeader("X-CSRF-Token",csrf_token);
},
success: function (e) {
console.log(e);
alert("Name successfully changed.")
},
error: function(e) {
console.log(e);
alert("An error occurred while changing the name. Please check your JS Console.")
}
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment