Fix getting improper response

Define the API method as GET
JSON.parse is now parsing the response child of the request instead of the whole request object - This kept failing the script after getting response from server
Add a fail scenario when a link fails testURL
encode URIs to prevent "interesting" server-side URI mangling

Signed-off-by: David Todd <c0de@unps.us>
This commit is contained in:
David Todd 2015-03-13 22:29:09 -05:00
parent ce51fc203e
commit ef606d89bb

View File

@ -46,9 +46,9 @@ function shortenTabURL(tabid){ // Use just a tab id to shorten its url
function shortenURL(url){ // Creates a short url and copies it to clipboard function shortenURL(url){ // Creates a short url and copies it to clipboard
if(testURL(url)){ if(testURL(url)){
//var url = encodeURIComponent(url); var url = encodeURIComponent(url);
sendAPIRequest("?url=" + url, function(req){ sendAPIRequest("?url=" + url, function(req){
var res = JSON.parse(req); var res = JSON.parse(req.response);
var status = res.statuscode; var status = res.statuscode;
var linkID = res.shortcode; var linkID = res.shortcode;
@ -64,7 +64,7 @@ function shortenURL(url){ // Creates a short url and copies it to clipboard
case "201": // No URI provided - /Should/ never show up here case "201": // No URI provided - /Should/ never show up here
showAlert("It appears that you didn't submit a link to fox.ci.\nTry again?"); showAlert("It appears that you didn't submit a link to fox.ci.\nTry again?");
break; break;
case "202": // Attempted to shorten unsupported URI - As of 3/13/2015 http(s) only case "202": // Attempted to shorten unsupported URI - As of 3/13/2015 http(s) only - /Should/ never show up thanks to testURL
showAlert("You tried to shorten an unsupported URL.\nhttp and https links only please."); showAlert("You tried to shorten an unsupported URL.\nhttp and https links only please.");
break; break;
case "203": // URL failed to resolve - Host could be down, or formatting problem case "203": // URL failed to resolve - Host could be down, or formatting problem
@ -76,10 +76,13 @@ function shortenURL(url){ // Creates a short url and copies it to clipboard
break; break;
} }
}); });
}else{
showAlert("Oh noes!\nYour link failed the client-side validation check D:");
} }
} }
function sendAPIRequest(url, callback){ // Sends a GET request to the server function sendAPIRequest(url, callback){ // Sends a GET request to the server
var method = "GET";
var req = new XMLHttpRequest(); var req = new XMLHttpRequest();
req.open(method, "http://fox.ci/api/" + url, true); req.open(method, "http://fox.ci/api/" + url, true);
req.onload = function(){ req.onload = function(){