google.script.host.close is not a function / self-closing popup - javascript

Trying to figure out an annoyance. I have 2 separate web apps in Google Apps Script (one runs as the user, one runs as the script owner. They don't actually talk to each other - other than script 1 opens a popup with a pre-formatted URL with the correct parameters etc.)
Web App 1 opens Web App 2 in a popup. Web App 2 should ideally be able to close itself, but I can't seem to figure out how. I've tried open(location, '_self').close(); as well as google.script.host.close();
My current setup is below:
Web App 1 Client-Side (No issues)
function common_token_ready(instructionsObject){
var url = instructionsObject.pickerURL;
window.open(url,"_blank","height=720,width=1280");
}
Web App 2 Client Side (Works other than being unable to close itself)
function save_success_handler(response){
document.getElementById("main").innerHTML = "Please close this window to continue.";
google.script.host.close();
}
I get a TypeError google.script.host.close is not a function
Any ideas?

Related

How to determine in js whether an app is installed on a phone?

The question concerns js more than the specific task of Telegram determination.
In the normal case, on a web page there is code that, in case when an app is installed (in our case it's Telegram), sends the user to the app at once.
<script type="text/javascript">
var url = "tg://resolve?domain=";
setTimeout(function() {
window.location = url;
}, 100);
</script>
There is also code that, for example, will also open a page in a browser after some time.
<script type="text/javascript">
var now = new Date().valueOf();
setTimeout(function () {
if (new Date().valueOf() - now > 200) return;
window.location = "https://itunes.apple.com/";
}, 25);
window.location = "tg://resolve?domain=";
</script>
Meanwhile, we would like only one of the options to work - either an app or a web page should open (if the app isn't installed).
In this regard, there is a question if we can somehow monitor the following developments:
- the user left the page but didn't open it (some events that can prompt that the user doesn't interact with the page). We mean that in this case the user opened Telegram and it is installed.
- if it's possible, to note the time during which the user didn't interact with the page (in case when the user went to Telegram and after some time - e. g., a few minutes - came back to the browser on our page).
So, the algorithm is like this:
if Telegram is installed, then we suppose the tab with our web page will be open, and no actions will be performed on the page - for quite long, at least 20 seconds.
if Telegram is not installed, then the user will either close the web page or will somehow interact with it (scroll, click, etc.).

FileMaker Web Viewer javascript step fails when used as a local file: window.open()

I ran into an odd issue with a bit of Javascript that is running in a Web Viewer (WV) in a FileMaker database. (For those that don't know, FileMaker is an integrated UI/DB platform which has a 'web viewer' object, which is a cut down version of a web browser that runs inside the solution. It is NOT a full web browser.)
We have had this WV+Javascript working for quite a while now, in files that are hosted using FileMaker Server (FMS). There's a button on a layout that opens a pop-out window that contains this web viewer. This web viewer is a media player, with pause/play buttons; it has some special functions for capturing the current video position and reporting it back to the database: it has some buttons and reads keystrokes, and calls back to the open FMP file to affect changes there. Again, this has been working fine when the file is hosted on FMS.
But then we wanted to make a trial/demo version of this file, and have the user run it locally or offline. In testing this offline file it was noticed that the pop-out window web viewer stopped calling back to the open file. (FileMaker Pro also registers a new URL scheme, "fmp://". This can be used to make callbacks to the database from web viewers that are inside that database. So the code was changed to use the '$' reference in the "fmp://" URL, which causes the FileMaker client software to reference the currently open file of the given name.) But even with that fixed, there was one step that was failing still.
To effect these 'callback' events from the WV+javascript to the FM file, the javascript was using this code:
window.open('fmp://' + fmdbHost + '/' + fmdbFilename + '?script=keyPressed&param=' + e.keyCode, 'form_b');
(Here, the variable 'fmdbhost' = "$", as noted; 'fmdbFilename' =(nameoftheopenfile); 'e.keyCode'=(keypressed); "form_b" is an iframe that is defined in the HTML of the page.) This code (when it's working on the hosted environment) does not actually open a new window; that's OK, I don't want it to open a new window. It just a technique used to execute the URL and trigger functionality in the FileMaker world.
My solution/workaround, was to instead use this code:
window.location.href = ( 'fmp://' + fmdbHost + '/' + fmdbFilename + '?script=keyPressed&param=' + e.keyCode );
This appears to work out just fine, but I'm curious to know why the original step was failing when it's run locally. This was working if the file was hosted...so why would that fail if the file is being run locally? Does it do some kind of URL verification, and not like the "fmp://" or perhaps the "$" part? If so...why isn't it doing this when using 'window.location'? (If I used the original 'fmdbHost' definition - which used an IP Address - it would at least execute the window.open() method and attempt to open the "fmp://" URL. It's just that when run locally this ended up being the wrong file - it needs to use the "$" reference to affect the currently open file.)
The specification for 'window.open()' indicates that the second parameter is a window name (sounds more like a reference to the new window). So in the original code the 'form_b' reference is attempting to reuse the iframe defined in the HTML as the conduit for the call. But is that iframe even an actual window 'reference' initially? Does something parse the DOM and extract iframes and add them to the list of current 'windows'?
I'm using FM 15.01 for testing/development, on OS X 10.11.6. Ultimately, this will have to run on Windows as well...but that can be ignored for now.
Thanks,
J

When using facebook deep URL to open app, both app and webpage open together

I am using this as my link in webpage.
<a class="btn btn-lg btn-orange" role="button" onclick="myFunction()">like me</a>
And this as my script.
function myFunction() {
$(function(){
window.location = "fb://profile/1456471431314551";
setTimeout(function () { window.location = "https://www.facebook.com/angelsatwork2015"; }, 25);
})
}
When I open in mobile both the browser fb page and the fb app open. Please help so that if the app opens it does not redirect to fb browser page.
Also when I use this code on desktop two different browser pages open.
This is most likely the expected behavior for that code.
What you're doing when that button is clicked is immediately opening the URL fb://profile/1456471431314551. Assuming this is iOS, this causes the system to show an alert asking if you want the app to launch. However, as of iOS 9.2, this alert is non-blocking, which means other code continues to execute in the background. This is why, 25 milliseconds later, you're opening the URL https://www.facebook.com/angelsatwork2015 as a regular webpage.
Unfortunately there is no good solution to this in the iOS 9.2+ world (thanks, Apple...). What we do at Branch.io is a combination of Universal Links (which launch the app when it is installed...most of the time) and redirections like the one you're trying for edge cases where Universal Links don't work. We set tracking cookies based on device UUID to know when we are safe to attempt launching the app, but in reality, this often means we have to redirect to the App Store if we aren't sure, just to avoid the behavior you're encountering.

Never Show Window On Page Launch

I am using C# and asp.net to launch a webpage that I am passing parameters to. That works well! I come from a Windows.Forms background so please forgive me if I am trying to achieve the impossible. What I would like is set the Visibility property of the program (either IE or chrome) to false so the user never sees that a webpage is being launched. I have been using this JS function to close the page, but it seems that the page must completely load before closing which sometimes can take a few seconds.
Does asp.net have the capability to achieve such? And this is my JS code I have been using
string close = #"<script type = 'text/javascript'>
window.returnValue = true;
window.close();
</script>";
base.Response.Write(close);
If you don't want the User to see the page, I assume you just want to post some information to the page. In that case, make an HTTP request via c# code, instead of opening the webpage up in a browser.
On the Project Properties page, Web tab, Start Action section, click the radio button for "Don't open a page. Wait for a request from an external application".

Executing javascript from within an iframe

I have:
A web server (server 1)
An application server running some beast of a legacy web app (server 2)
An iframe on server 1 pulling in the application from server 2
My problem is:
The legacy app uses JS validation on its forms. When a user attempts to submit an incomplete form, an alert pops up to notify the user that they are a dummy. Of course, this fails when the app is run inside of an iframe because server 1 and server 2 live at different domains.
I tried setting the following proxy directives on server 1:
ProxyPass /legacy_app http://server2.url/legacy_app
ProxyPassReverse /legacy_app http://server2.url/legacy_app
I'm now able to serve the iframe from http://server1.url/legacy_app, but I'm still unable to execute javascript inside that iframe -- I get the same security/access errors as I did when the app was running on a different domain.
Is there something else I can try?
How is the legacy app checking if the boxes are filled in? Simple javascript? Ajax?
The alert box itself should still work. I'm thinking the code for determining if the alert should be issued might be what's broken.
Running the following code on my local apache server still gives me the alert onLoad even though the page is on a remote host:
<html>
<body>
<div>
<iframe src="http://www.crowderassoc.com/javascript/alertbox.html" width="300" height="200">
</div>
</body>
</html>
Try copying the above code to a page on server #1 and see if you get the alert box from that remote site in the iframe.
Have you tried hosting the script inside of a .js file hosted on server #1 but running out of the iframe (referenced out of server #2)?
I think a browser is okay with referencing an external site, but doesn't like it when it is referenced by an external site.
Haven't tried it myself, but I believe that's how I've heard of this sort of a problem being worked around. I know this is the method that Google Analytics uses - you have to request the .js file from Google's servers, but once it's there, it has access to the browser.
Joe, I think you are correct. A quick test with other servers shows that I can trigger alerts from remotely-hosted scripts quite easily.
The legacy server is the client's and we don't have easy access to it, but glancing at their JS it looks like they're doing some sort of cross-site/framing detection -- worth further investigation.
I've had this situation in the past where I was trying to build an app around a heavily scripted pre-existing app on a remote server, and the app would run fine if it was opened in its own window, but if I tried loading it into a frame, it would break.
What I ended up doing for this project was opening the local application in a pop-up with a width of 495px, loading the external app in the main (already existing) window, resizing the main external app window to the screen width minus 495px, and positioning the windows side by side on the screen. This gave the end user a similar effect to what I had been trying to do with frames, only it worked.
In case it helps, here is the code I used from my index.php file:
// Manipulating the current window
window.location.href = 'http://www.someExternalApp.com'; // setting the page location.
window.name = 'legacyapp'; // setting the window name just the for heck of it.
moveTo(0,0); // moving it to the top left.
// Resizing the current window to what I want.
mainWindowWidth = screen.width - 495;
mainWindowHeight = screen.height; // Makes the window equal to the height of the users screen.
resizeTo(mainWindowWidth,mainWindowHeight);
// function for opening pop-up
function openWin(){
win2 = window.open(page,'',winoptions);
win2.focus();
}
// internal app location (for use in pop-up)
page = 'someLocalApp.php';
// internal app Window Options (for pop-up)
winoptions = 'width=490,height='+mainWindowHeight+',top=0,left='+mainWindowWidth+'leftscrollbars=1,scrolling=1,scrollbars=1,resizable=1,toolbar=0,location=0,menubar=0,status=0,directories=0';
// Opens the local app pop-up
openWin();

Categories