I have a website for url//link shortening, that uses Google Spreadsheets and Google App Scripts,
I have been trying to find a way to redirect the users when they type my link, but app scripts either tries to load the redirection URL in an iframe which gets rejected by most sites these days or I have to open up a new tab using window.open(url, _blank); which acts as a pop up that has to require permission from the user to open up the first time.
Now i'm aware that I can create an html button which will then redirect them, but thats a really slow and tedious process for a url shortener.
So if anybody is aware on how I can redirect the user automatically instead of making a click button or opening a new tab with popup please do share.
Here is the link to my website that shortens links and then redirects:
Heres the doGet function for the Code.gs script
function doGet(e) {
var slug = e.queryString;
if (slug == '') {
var title = 'Dashboard | ' + serviceName;
return HtmlService.createHtmlOutputFromFile('Dashboard').setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL).setTitle(title);
} else {
var notFound = false;
var redirectsData = getRedirects();
for (var i = 0; i < redirectsData.length; i++) {
if (slug == redirectsData[i].slug) {
var redirectURL = redirectsData[i].longURL;
var html = "<body>Redirected! You can now close this tab.</body><script>window.open('" + redirectURL + "','_blank');</script>";
return HtmlService.createHtmlOutput(html).setTitle('Redirected!');
break;
} else {
notFound = true;
}
}
if (notFound) {
var html = "<body>No redirections found! Redirected to Link Loom. You can now close this tab.</body><script>alert('No redirects found!');window.open('" + customDomain + "','_blank');</script>";
return HtmlService.createHtmlOutput(html).setTitle('Error 404');
}
}
}
If you would like to check out my full script, here is the link: