Get the full browser URL in Apps Script - google-apps-script

I'm looking to get the full URL of the current browser window in Apps Scripts while displaying the web app via HTMLService
When I try this, it doesn't work because the Apps Scripts is sandboxed and I get another server url of some sort
window.location.href
When I try this, I can only fetch the hash or parameters, not the full url
google.script.url.getLocation(function(location) {
console.log(location.parameters);
console.log(location.hash);
});
source: https://developers.google.com/apps-script/guides/html/reference/url#methods
Purpose for the above:
I want a button that people click on in the published Web App to remove the browser toolbar. However, the browser toolbar is not removable in the current browser window. So I want to fetch the Web App URL and put it in window.open() as a new window with the current web app because the browser toolbar can be remove this way. But I can't seem to get the current URL.
Any ideas how I can achieve this?
Thanks in advance!

I don't know what you meant by developer mode either but I took a guess at this being the answer to the question. If not, let me know and I'll remove it.
On the clientside you can use Javascript:
<script>
function getUrl(){
google.script.run
.withSuccessHandler(function(url){
document.getElementById('mydiv').innerHTML='<p>' + url + '</p>';
})
.getScriptUrl()
</script>
<body>
<div id="mydiv"></div>
</body>
In Google Script:
function getScriptURL() {
return ScriptApp.getService().getUrl();
}
Jan 10, 2021: Tested the above code in a window.onload and it is returning the correct url with a business account, V8 runtime and the new editor.

I'd suggest this code for the current script URL:
function getScriptURL() {
// dev URI
'https://script.google.com/macros/d/' + ScriptApp.getScriptId() + '/edit';
}

Related

Way to execute Google Standalone Script directly from Drive

Well, I've been reading the documentation for a long time but, as per usual, it's really unclear...
I'm developing a Google Script with the Script Editor that is already working as expected (it have nothing to see with any G suite program but with Drive).
By now, the only way to execute it is opening the script editor and press on the "play" button or the "execute" bar and press on the function.
Obviously this is not a solution since anyone from the bussines could modify/see the code.
So the question is this: how can I make from this script something like an "exe" that I just have to double ckick (its obviously located at Drive) and it executes the script?
I saw this but seems it says no way to do it except from opening the code and execute from the google app script editor...
A Web App is triggered by either an HTTPS GET or POST request. You don't need to use Google Drive.
Ways to trigger a Web App:
Chrome Extension - You can create a Chrome Extension to make an HTTPS GET or POST request. The user would need to install the Chrome extension, and the Chrome extension could put a button in the browser.
Link - Some HTML with a link in it. An email with a link in it. Click the link.
Address Bar - Browser's address bar - Every browser's address bar issues an HTTPS GET request, so you can run a Web App directly from the address bar. Put the published Web App url into the browser address bar and click whatever the browser uses to load the page. (Only for a GET request)
Bookmark - The user could bookmark the link to your Web App. So, they would need to click the link in their bookmark.
Any program that can make an HTTPS GET or POST request. For example, make a POST request from Python or C++.
For a GET request, you need a doGet() function in your script, and to react to a POST request you need a doPost() function.
From what I understand of your requirements the only way would be to deploy your script as a web app with a minimal user interface, just a short message to confirm proper execution for example.
You will never have a "local" executable file since everything in Apps Script is done on google's servers, not in our computers. Instead you will have an url... (with the advantage that it is completely OS independent ! )
The script will remain private unless you share it and you'll be able to choose who can use that url.
Try this:
Build the menu first.
Click "Run My Code".
Click the "Run My Code" button.
runmycode.html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function runMyCode()
{
google.script.run
.withSuccessHandler(updateMyDiv)
.myFunctionName();
}
function updateMyDiv(hl)
{
document.getElementById('MyDiv').innerHTML+='<br />'+ hl;
}
</script>
</head>
<body>
<input type="button" value="Run My Code" onclick="runMyCode()" />
<div id="MyDiv"></div>
</body>
</html>
runMyCode.gs:
function myFunctionName() {
return "Your function has run";
}
function startYourSideBar()
{
var ui=HtmlService.createHtmlOutputFromFile('runmycode');
SpreadsheetApp.getUi().showSidebar(ui);
}
function buildMenu()
{
SpreadsheetApp.getUi().createMenu('Run My Code')
.addItem('Run My Code', 'startYourSideBar')
.addToUi()
}

how to refresh a google site using google apps scripts ?

My site fetches chart from spreadsheet but it is getting updated only when it is refreshed manually. Publishing on web is not working as expected. So Any method to refresh a site is much appreciated. Please do not include browser extensions or plugins or widgets, because of site audience.
After some hours struggling with this, i found a solution that worked for me.
Place this code in Templated HTML file. Then call function reloadSite() in Javascript to reload current site.
<script>
function reloadSite() {
window.top.location.href = '<?!= SitesApp.getActivePage().getUrl(); ?>';
}
</script>
Should be able to end the onSubmit() with the function that calls the Ui. You state it is doGet(), like this :
function onSubmit(e){
// Your code ...
doGet();
}

Apps Script HTMLService not working and it was working perfectly

I have created a lot of apps using Apps Script and HTMLService for my work and all the apps have been working perfectly(reports,submission tools,trackers,etc) however, since Feb 25th,2014 they are not working, not even a single one of them.
When I click the button of a html form in my app it should load another page but now I click on the link and nothing happen, I really need your help since I dont know why they are not working when they used to work without any inconvenient.
Below you can find a code that worked before and now is not working,
Code.gs
function doGet() {
var template= HtmlService.createTemplateFromFile('test');
template.action = ScriptApp.getService().getUrl();
return template.evaluate()
}
function doPost(e){
var template = HtmlService.createTemplateFromFile('test.html');
template.action = ScriptApp.getService().getUrl();
return template.evaluate();
}
test.html
<!DOCTYPE html>
<html>
<body>
Exception Tracker Report <br/><br/>
<form >
<input name = "test" value="test">
Test<input type="submit" value="Load" />
</form>
</body>
</html>
I really request your help in this matter since all the apps that I have created using HTMLService are not working (20 or more apps).
Once again, the code above should work without any problem but I dont know if there have been changes in the HTMLService Google class or anything regarding this.
Thanks for your help
Google made a change yesterday on htmlservice, by defect if not mentioned the sandbox mode was emulated. They changed it to native.
As it's not mentioned in your script, you where running in emulated mode and since yesterday ... it changed to native mode.
You need to specify in your script that you want to use the emulated mode.
this way:
function doGet() {
var template= HtmlService.createTemplateFromFile('test');
template.action = ScriptApp.getService().getUrl();
return template.evaluate().setSandboxMode(HtmlService.SandboxMode.EMULATED);
}

Chrome Extension Content Script on https://chrome.google.com/webstore/

Is Chrome blocking access to the webstore url?
I would like to make an extension that displays a like button beside the +1 button, but it looks like that content scripts are not working on https://chrome.google.com/webstore/*
Is that true?
TL;DR The webstore cannot be scripted by extensions, and the flag that previously allowed you to do that (--allow-scripting-gallery) has been removed in Chrome 35.
Chrome extensions cannot execute Content scripts / insert CSS the Chrome Web Store. This is explicitly defined in the source code, at function IsScriptableURL (click on the previous link to see the full logic).
// The gallery is special-cased as a restricted URL for scripting to prevent
// access to special JS bindings we expose to the gallery (and avoid things
// like extensions removing the "report abuse" link).
// TODO(erikkay): This seems like the wrong test. Shouldn't we we testing
// against the store app extent?
GURL store_url(extension_urls::GetWebstoreLaunchURL());
if (url.host() == store_url.host()) {
if (error)
*error = manifest_errors::kCannotScriptGallery;
return false;
}
manifest_errors::kCannotScriptGallery is defined here:
const char kCannotScriptGallery[] =
"The extensions gallery cannot be scripted.";
The error can be viewed in the background page's console when you use chrome.tabs.executeScript to inject a script in a Web Store tab. For instance, open https://chrome.google.com/webstore/, then execute the following script in the background page of an extension (via the console, for live debugging):
chrome.tabs.query({url:'https://chrome.google.com/webstore/*'}, function(result) {
if (result.length) chrome.tabs.executeScript(result[0].id, {code:'alert(0)'});
});

Automatically Redirecting to a Page

Inside a button click handler, I'm creating a new web page like so:
var page = SitesApp.getPageByUrl(url).createPageFromTemplate(title, name, template);
and I want to redirect the user automatically to that page.
I wasn't able to find much information, can this be done?
Corey G's answer worked for me, but the problem is that the web page that I was redirecting was embedded into an iframe in the response of the GAS, so the real URL in the web browser was the script's one.
This is what actually worked for me:
function doGet() {
return HtmlService.createHtmlOutput(
"<script>window.top.location.href='https://example.com';</script>"
);
}
This similar Q/A helped me to solve it.
Hope it helps.
This cannot be done in UiApp but it's doable in HtmlService:
function doGet() {
return HtmlService.createHtmlOutput(
"<form action='http://www.google.com' method='get' id='foo'></form>" +
"<script>document.getElementById('foo').submit();</script>");
}
This should be easier; please file a feature request in the issue tracker and we will see how we can make this more pleasant.
(Edit: To be clear, there's no way to do this from a UiApp callback; your entire app would have to be using HtmlService for this to work.)