Expand URL and Show in Modal

64 views
Skip to first unread message

Timothy Hanson

unread,
Dec 21, 2021, 12:12:32 PM12/21/21
to Google Apps Script Community
I am generating a URL and showing it in a ModalDialog box
I click the link and it opens a new tab and then I get the expanded link from the address bar

Is it possible to show the expanded link in the dialog?

I have

```
function openUrl( url,kumuurl ){
  var html = HtmlService.createHtmlOutput('<html>'
  +'<body style="word-break:break-word;font-family:sans-serif;"><a href="'+url+'" target="_blank" onclick="window.close()">' + kumuurl + '</a></body>'
  +'<script>google.script.host.setHeight(150);google.script.host.setWidth(600)</script>'
  +'</html>')
  .setWidth(600).setHeight(1);
  SpreadsheetApp.getUi().showModalDialog( html, "Click the link below and Use the NEW expanded link in the Address bar - Do NOT use the link shown" );
}
```

Martin Hawksey

unread,
Dec 21, 2021, 5:22:02 PM12/21/21
to Google Apps Script Community
if the url in <a href="'+url+'" target="_blank" onclick="window.close()">' + kumuurl + '</a></body> is correct, then you could use it instead of kumuurl e.g.
<a href="'+url+'" target="_blank" onclick="window.close()">' + url + '</a></body>

If the issue is the url generated for the dialog is not the final web location there is an apps script function here you could use https://stackoverflow.com/a/50733029/1027723

function openUrl( url,kumuurl ){
  var html = HtmlService.createHtmlOutput('<html>'
  +'<body style="word-break:break-word;font-family:sans-serif;"><a href="'+url+'" target="_blank" onclick="window.close()">' + getRedirect(kumuurl) + '</a></body>'
  +'<script>google.script.host.setHeight(150);google.script.host.setWidth(600)</script>'
  +'</html>')
  .setWidth(600).setHeight(1);
  SpreadsheetApp.getUi().showModalDialog( html, "Click the link below and Use the NEW expanded link in the Address bar - Do NOT use the link shown" );
}

function getRedirect(url) {
var response = UrlFetchApp.fetch(url, {'followRedirects': false, 'muteHttpExceptions': false});
var redirectUrl = response.getHeaders()['Location']; // undefined if no redirect, so...
var responseCode = response.getResponseCode();
if (redirectUrl) { // ...if redirected...
var nextRedirectUrl = getRedirect(redirectUrl); // ...it calls itself recursively...
Logger.log(url + " is redirecting to " + redirectUrl + ". (" + responseCode + ")");
return nextRedirectUrl;
}
else { // ...until it's not
Logger.log(url + " is canonical. (" + responseCode + ")");
return url;
}
}

function testGetRedirect() {
Logger.log("Returned: " + getRedirect("https://t.co/hO9swbmeeZ"));
}



Timothy Hanson

unread,
Dec 22, 2021, 11:27:46 AM12/22/21
to Google Apps Script Community

Martin Hawksey

unread,
Dec 23, 2021, 2:56:25 AM12/23/21
to Google Apps Script Community
Ah - from memory web app urls are a bit funky when deployed as anything other than 'as me'/anyone. 

Google used to have a URLShortener services in app script where I would shorten urls for web apps. Doesn't solve the issue with people bookmarking the long url but gave something friendlier to include in info emails. Google killed the URLShortener service a long time ago but for other projects I created a similar service which uses bit.ly https://hawksey.info/blog/2018/10/a-bitly-shim-for-urlshortener-service-in-google-apps-script/

Reply all
Reply to author
Forward
0 new messages