Google Apps Script: Getting a URL parameter to use in Javascript - javascript

I have a web app with Google Apps Script and would like to take a URL parameter and use it in modifying my HTML via Javascript, but am finding this tricky.
If I try using window.location in my Javascript it gives a different URL than the one shown in the address bar. The URL shown in the address bar is like this ... https://script.google.com/macros/s/MY_SCRIPT_ID/exec?param1=value1 .... but window.location gives something like this https://SOME_SORT_OF_LONG_ID-script.googleusercontent.com/userCodeAppPanel (it doesn't have param1 / value1 at all).
I know how to get the parameter value when I'm in the doGet(e) function -- by using e.parameter.param1 -- but I don't know how to be able to then subsequently use that value in some Javascript.
Help, please!

The html that GAS provides is never the actual URL, it is essentially another ID that google uses to keep track of its web pages. Remember that all Google apps are running on the Google server.
This may not be the same with a standalone script, but I suspect it will be, but I know if you get a google doc, the actual URL is:
https://docs.google.com/document/d/{{{ Your Document ID }}}
I expect a standalone app will be similar. Try using your webapp.getId(), and then adding it to the actual url of your script.

Related

Can I get the address bar url from the javascript console when the page has failed to load?

Just say I typed in a bad hostname in the address bar.
For example, say I wasn't running a local web server, and I load:
http://localhost/callback_url
In Chrome, this will give me a "This webpage is not available" message.
Is there anyway I can find out what the url is in the address bar from the Javascript console, even though the page failed to load?
I know I can normally use window.location.href to get this, but that returns "data:text/html,chromewebdata" in this instance.
So in this example, I'd like to know if there's some javascript that returns http://localohost/callback_url
EDIT: The main reason I'd like to do this is so I know if server side redirect failed when using ChromeDriver with Selenium. So I'd prefer to avoid using extensions if possible, and am open to Chrome and ChromeDriver specific solutions if applicable! The callback_url may have extra info in it, added by the server, and I'd like to see what this info is. I'd like to avoid running another server to get this data if possible.
The loadTimeData object included in the ERR_CONNECTION_REFUSED page has the failed URL:
> loadTimeData.data_.summary.failedUrl
"http://localhost/foo?request_url=bar"
You can get it from the title of the page.
By typing document.title and doing some regex you can get the URL.
Another way I found is by using the following
var data = loadTimeData.createJsEvalContext();
console.log(data.a.$top.summary.failedUrl);
If you open the developer tools and search for a part of the URL in source code, you will see that Chrome creates the loadTimeData in the "not available page".

Get URL in javascript without file extension

In Google Analytics I'm tracking goals with virtual page views. I take
trackingURL = window.location.pathname+'thankyou.php';
and then
_gaq.push(['_trackPageview',trackingURL]);
The issue is when a page is something like www.domain.com/page.php it ends up being domain.com/page.phpthankyou.php and not tracking properly, but if it was domain.com/page/ and then it became domain.com/page/thankyou.php it would track properly.
How can I get the full url, without the extension, so I can add on /thankyou.php, but if it is already a directory with the / at the end, then I just want to add thankyou.php to it?
Thanks!
Use this (which only replaces the extension with a slash if it finds it):
trackingURL = window.location.pathname.replace(".php","/") +'thankyou.php';
Which replaces domain.com/page.php with domain.com/page/

Passing data from javascript to actionscript

Does anyone know how to pass data from javascript to actionscript in flex? I have tried the method involving the LocationChangeEvent listener in flex, but I have trouble getting the changed URL. I am working on a Flex Mobile project. Please help.
[Edit]
I am working on a mobile project where I am trying to load a html with javascript in it. I set the size of the StageWebView to zero because I dont need the web interface, I just need to load the javascript. From there, I am trying to send data to my flex application by modifying the document.location inside the javascript like this:
document.location = "mydata";
Then, my flex application is listening to this event LocationChangeEvent where it will be triggered if there has been a change of URL happened inside the StageWebView. For some reason, I does not trigger.
[Solved]
The LocationChangeEvent is actually working in my case but I did change something in the javascript.
I changed the following
document.location = "mydata";
to
window.location = "mydata";
You can make calls like Actionscript -> Javascript and Javascript -> Actionscript with
ExternalInterface
You could try listening for LocationChangeEvent.LOCATION_CHANGING or Event.COMPLETE on your StageWebView (note that there is LOCATION_CHANGE and LOCATION_CHANGING)
var myStageWebView:StageWebView = new StageWebView();
myStageWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGING, handler);
myStageWebView.addEventListener(Event.COMPLETE, onComplete);
or
In your change/changing/complete handler, trace event.target to see the html. Maybe you could simply add your javascript redirect url to the html body when it loads.
Using StageWebViews for data on mobile can be a little convoluted. Alternatively, if you have access to server code, you could try making a POST using a URLLoader to get the proper redirect url from the server (thus bypassing the need for javascript). In your complete handler for the URLLoader, access the returned data from event.currentTarget.data and pass that into your StageWebView location.

GM_xmlhttpRequest's responsetext is missing some HTML

If I go to this Google Maps page, some of the HTML is missing in View Source, but shows up in Firebug.
Likewise, when that same URL is passed to my function, the following HTML does not show up in the responseText, but it does show in Firebug when I open the page.
<a id="mapmaker-link" class="kd-button mini left" style="" href="https://www.google.com/mapmaker?ll=41.06877,-112.047203&spn=0.038696,0.132093&t=h&z=14&vpsrc=0&q=1093+W+3090+S,Syracuse,+UT&utm_medium=website&utm_campaign=relatedproducts_maps&utm_source=mapseditbutton_normal">
Here is the function I'm using:
function updateMap(url) {
GM_xmlhttpRequest(
{
method: 'GET',
url: url,
onload: function(resp) {
var ll = resp.responseText.split("mapmaker?")[1];
ll = ll.split("&")[0];
document.getElementById('googlemap').href = url+"&"+ll;
}
});
}
I have placed a sample responseText value at pastebin.com/Tt8nrzG8.
The response is "missing" HTML because the called page loads that HTML (and almost all of the page's content) via AJAX.
GM_xmlhttpRequest (and all other current AJAX methods) only gets the static source of a given page. Such XHR requests cannot process a requested page's javascript, like a browser does when you browse to the page.
In fact, if you save that sample responseText, that you linked, as an HTML file; you'll see it looks like this:
See "How to get an AJAX get-request to wait for the page to be rendered before returning a response?", for the same type of problem. But note that the answer recommends that you use an API, if one is available.
So, use the Google Maps API to get the lat/long you want for your URL.
Or, the easiest approach is still to have the script also run on Google maps pages and do a one-time zoom on links with your special URL parameter -- like I recommended on your previous question. This has the added advantage that no calls to Google are made/needed until you actually decide to click your Google Maps link.
If you do opt for the iframe approach (again, NOT recommended for ANY Google site), beware that you will need to adjust the URL to tell Google to allow iframing and the lat/long information will be in a different part of the page.

How to get url of embedding page for a javascript widget

(Rewording the question as there were very few views otherwise).
I want to build a widget that others can include on their website, and the widget itself will be hosted on my website. I am aware of just one method to build widgets that can be embedded on other websites: The website that wants to embedd the widget sources a javascript from my site, which does "document.write" on the page. Something like:
<script language="javascript" src="http://www.my-website-that-will-host-the-widget.com/javascript-emitter.php?id=1234&width=200&bordercolor=000000&bg=ffffff&textcolor=000000"></script>
Now, I want to make a particular widget accessible from only particular domains. For this, I want to know the URL of the page that is embedding my widget reliably . No-one should be able to spoof it. For example, if I have an explicit variable in the embedding code, people can change it.
How do I do it? (I also want that there minimal code to write for the person who is embedding my widget).
regards,
JP
Explanation 1:
Lets say I want to do this: If widget is accessed from 1.com, display A, else display B. How do I do it reliably. Thing is, "A" is something that should not be visible in the code unless the widget is accessed from 1.com. (Thus, if it is embedded in 2.com, I don't want to output if(location.href == 1.com) write(A) else write (B)
Note 1:
(As an aside, if someone feels my method is not good/efficient and can suggest better methods/tutorials, etc., that would be great help. Most google queries give you sites that explain how to build/obtain widget for "your site".... and usually point to websites that allow you to build widgets hosted with them, I want to understand how to build widgets that can be embedded by other websites from my site)
In javascript on the client-side, you can use location.href to get the url of the current page:
var url = location.href;
If you do not want to output any javascript at all for a forbidden domain, in your php you can check the HTTP_REFERER header with the global variable $HTTP_REFERER. In your javascript-emitter.php script try this:
<?php
echo $HTTP_REFERER;
?>
However be warned that this is not always to be trusted: it is up to the client (the browser) to send the correct REFERER header. And of course if someone really wanted to include your widget on their site, they could easily request your javascript server-side spoofing the REFERER header - that is set it to something that's on your whitelist - before forwarding it to the client.
In short there's no way you can easily and absolutely block blacklisted sites from using your widget.

Categories