Closed TohnoCoding closed 6 months ago
So far I'm considering the following approach. Any input is more than welcome.
EDIT: Received some input from Borischess from the /r/grandorder Discord server and did some cleanup; no need to nest the ajax calls if I can make them sequentially.
EDIT 2: Some further sequential cleanup on the ajax calls to the shortening providers.
//=============================================================================================================================
// Short URL
// Multiple providers have been coded and tested to work. More providers to come?
//=============================================================================================================================
function shareURL(site) {
// Setting up data to send to shortener
if (compress_input == "")
{
bootbox.alert(share_none_text);
return;
}
// Make shareable short URL
var full_url = window.location.protocol + "//" + window.location.host + window.location.pathname + "?" + compress_input_parameter + "=" + compress_input;
var mashuSR_str = getMashuSRURLstring(true);
if (mashuSR_str != "") { full_url += "&" + mashuSR_str; }
/*******************************/
/* Shortening services */
/*******************************/
var shorten_successful = false;
//--------------//
// y.gy //
//--------------//
var ygyajaxdata = JSON.stringify({ 'destination_url': full_url });
var y_gy = {
url: "https://api.y.gy/api/v1/link",
contentType: "application/json",
method: "POST",
dataType: "json",
data: ygyajaxdata,
success: function(result) {
if (result.hasOwnProperty('errorcode')) {
alert("Error generating shortened URL! is.gd Error: " + result.errormessage);
}
else {
shorten_successful = true;
shareURL_Do(site, result.link);
}
},
error: function(result) {
alert("Error retrieving short URL from y.gy shortening service. Please try again later.");
}
}
//----------------//
// waa.ai //
//----------------//
var waa_ai = {
headers: {
Authorization: "API-Key 394562B4722f313b7392d97f7ea68f1cf9Df958b",
},
url: "https://api.waa.ai/v2/links",
dataType: "json",
contentType: "application/json",
method: "POST",
data: JSON.stringify({ url: full_url }),
success: function(result) {
if (result.hasOwnProperty('errorcode')) {
console.log("Error calling Akari shortening service: " + result.errormessage);
}
else {
shorten_successful = true;
shareURL_Do(site, result.data.link);
}
},
error: function(result) {
console.log("Error retrieving short URL from Akari shortening service. Attempting y.gy next.");
}
};
//---------------//
// is.gd //
//---------------//
var is_gd = {
url: "https://is.gd/create.php",
dataType: "json",
data: { format : "json", url : full_url },
success: function(result) {
if (result.hasOwnProperty('errorcode')) {
alert("Error generating shortened URL! is.gd Error: " + result.errormessage);
}
else {
shorten_successful = true;
shareURL_Do(site, result.shorturl);
}
},
error: function(result) {
console.log("Error retrieving short URL from is.gd shortening service. Attempting Akari next.");
}
};
$.ajax(is_gd);
if(!shorten_successful) { $.ajax(waa_ai); }
if(!shorten_successful) { $.ajax(y_gy); }
if(!shorten_successful) {
alert("URL shortening is not available at this time, as there were errors with the URL shortening providers.\n " +
"Please consult your browser console for more details. Sorry for the inconvenience.");
}
};
Fixed the functionality for staggered calls to providers. So far is.gd, y.gy and waa.ai are tested working (in that specific order).
Will see if I can find any other providers to work with this so I can set them up and have a bigger variety of options in case any of them fails.
//=============================================================================================================================
// Short URL
// Multiple providers have been coded and tested to work; code blocks have been staggered
// so that if one fails, one of the others will take over. If all the currently available
// providers fail, a message will be shown to the user explaining that URL shortening is
// out of service.
//=============================================================================================================================
function shareURL(site) {
// Setting up data to send to shortener
if (compress_input == "")
{
bootbox.alert(share_none_text);
return;
}
// Make shareable short URL
var full_url = window.location.protocol + "//" + window.location.host +
window.location.pathname + "?" + compress_input_parameter + "=" + compress_input;
var mashuSR_str = getMashuSRURLstring(true);
if (mashuSR_str != "") { full_url += "&" + mashuSR_str; }
/*******************************/
/* Shortening services */
/*******************************/
var shorten_successful = false;
var shortenedFinal = "";
//--------------//
// y.gy //
//--------------//
function ygy() {
var ygyajaxdata = JSON.stringify({ 'destination_url': full_url });
var y_gy = {
url: "https://api.y.gy/api/v1/link",
method: "POST",
dataType: "json",
contentType: "application/json",
data: ygyajaxdata,
success: function(result) {
console.log(result);
return result.url;
}
};
return $.ajax(y_gy);
}
//----------------//
// waa.ai //
//----------------//
function waaai() {
var waa_ai = {
headers: {
Authorization: "API-Key 394562B4722f313b7392d97f7ea68f1cf9Df958b",
},
url: "https://api.waa.ai/v2/links",
dataType: "json",
contentType: "application/json",
method: "POST",
data: JSON.stringify({ url: full_url }),
success: function(result) {
console.log(result);
return result.data.link;
}
};
return $.ajax(waa_ai);
}
//---------------//
// is.gd //
//---------------//
function isgd() {
var is_gd = {
url: "https://is.gd/create.php",
dataType: "json",
data: { format: "json", url: full_url },
success: function(result) {
console.log(result);
return result.shorturl;
}
};
return $.ajax(is_gd);
}
isgd().done(function(result) {
shareURL_Do(site, result.shorturl);
}).fail(function() {
ygy().done(function(result) {
shareURL_Do(site, result.url);
}).fail(function() {
waaai().done(function(result) {
shareURL_Do(site, result.data.link);
}).fail(function() {
alert("URL shortening is not available at this time, as there were errors with the URL shortening providers. " +
"Sorry for the inconvenience.");
});
});
});
};
With the implementation of promises for URL shortening API calls, there's currently three or four shortening providers active right now so for the time being I'm going to close this issue.
With the recent "death" of is.gd, I had to scramble to find a new shortening URL provider. After some days of much code mucking, as of today (2024/01/08 09:16hrs GMT-4 at the time of posting) I managed to get waa.ai and y.gy working, but with how the code is currently structured, only one shortening service can be active at a time. I'd like to be able to have multiple potential shortening services active, so that if one doesn't work or returns an error, there can be a fallback alternative.
Any suggestions as to how to make this work would be welcome. The first thing that popped to mind were nested ajax requests, but I'm eyeballing that that could lead to a whole host of issues as well as not be very efficient.