From ef606d89bb4e792c91e9a552bbbcc3b7ab55eb30 Mon Sep 17 00:00:00 2001 From: David Todd Date: Fri, 13 Mar 2015 22:29:09 -0500 Subject: [PATCH] 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 --- Fox.ci_Chrome_Extension/src/js/background.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Fox.ci_Chrome_Extension/src/js/background.js b/Fox.ci_Chrome_Extension/src/js/background.js index 151b64d..8fddd85 100755 --- a/Fox.ci_Chrome_Extension/src/js/background.js +++ b/Fox.ci_Chrome_Extension/src/js/background.js @@ -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 if(testURL(url)){ - //var url = encodeURIComponent(url); + var url = encodeURIComponent(url); sendAPIRequest("?url=" + url, function(req){ - var res = JSON.parse(req); + var res = JSON.parse(req.response); var status = res.statuscode; 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 showAlert("It appears that you didn't submit a link to fox.ci.\nTry again?"); 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."); break; 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; } }); + }else{ + showAlert("Oh noes!\nYour link failed the client-side validation check D:"); } } function sendAPIRequest(url, callback){ // Sends a GET request to the server + var method = "GET"; var req = new XMLHttpRequest(); req.open(method, "http://fox.ci/api/" + url, true); req.onload = function(){