Check for custom URL scheme in browser - javascript

I have an application that registers a new URL scheme when it's installed and I'm looking for a reliable way to launch this application from our web interface.
Right now I have an IFrame hidden away which I will update it's source after a button is clicked to launch the application, but it seems that in Internet Explorer the parent window always captures the URL scheme not recognized error and navigates to the location it can't understand anyway.
So my question: Is there a way for a programmer to check to see if a scheme is supported by a browser before attempting to navigate to the new URI to direct a user to download the application first?

You cannot check for URL schemes installed on the device.
What you can do is this:
setTimeout(function () { window.location = "appCustomUrlSchemeHere"; }, 25);
window.location = "fallbackUrlHere";
But I imagine that will still cause issues in IE as it will attempt to open the URL scheme.

Related

Opening _blank links in Electron Browser

I'm creating a web browser with Electron (please don't answer that that is "pointless", "not smart", etc.). When my user navigates to a link with the _blank attribute, I want a new Electron Web Browser window to open. Or, as a second choice, open the URL in their default web browser (such as Chrome, Edge, Opera, etc.).
I've looked at several other StackOverflow questions but with zero luck every time.
const { app, BrowserWindow, shell } = require("electron");
...
mainWindow.webContents.on("new-window", (_, url) => {
_.preventDefault();
const protocol = require("url").parse(url).protocol;
if (protocol === "http:" || protocol === "https:") {
shell.openExternal(url);
}
});
This event listener will trigger the callback whenever the user attempts to open the URL on your renderer. This callback will open your target URL on the default browser.
And regarding your other approach, I'd recommend not opening a new electron browser instance for accessing the target URL. This will be the cause of heavy resource usage.

Open video URL in native player from Google Chrome

I'm trying to open a remote video (let's say it's located at http://www.example.com/video.mp4) with the default Android player launched directly from Google Chrome, making use of the brand new intent://.
This is the URI I called through an href tag:
intent://www.example.com/video.mp4#Intent;scheme=file;action=android.intent.action.VIEW;end;
Of course, this URI doesn't work, and Chrome returns error "Unable to perform navigation". I've also tried the same URI omitting scheme=file.
Here's the documentation I've been following: https://developer.chrome.com/multidevice/android/intents
Thanks in advance!
A quick browse of the Gallery App in AOSP shows that it can be launched from a browser. It has a category of BROWSABLE and DEFAULT. This means given the correct intent URL you should be able to launch it.
Obviously specifying a package should work, but that is not flexible, what if there are two gallery apps.
The following Intent scheme url works:
intent://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4#Intent;action=android.intent.action.VIEW;scheme=http;type=video/mp4;end
Note:
scheme = http (it needs to be that or https),
there is a // before the domain, if that is not there the URL is not constructed correctly in the player
action = android.intent.action.VIEW
type = video/mp4 - if this is not in place the video will open in the browser
I have created a demo that works

open custom url scheme via Spotify Apps API

From a Spotify app I want to communicate with a native application that has been registered with a custom URL scheme. I am testing with a clickable anchor tag
open custom url scheme
as well as javascript code
location.href = "myscheme:/test";
which both work fine from any browser. At first, nothing happened when I clicked the link/ran the JavaScript in Spotify. After extending the permissions in manifest.json to
"RequiredPermissions": [
"https://*",
"http://*",
"myscheme:/*"
]
I am getting this page in the content view for both cases (click and JS)
<head></head>
<body>Error -302 when loading url myscheme:/test</body>
and the console output says
I [mainview:6886] Load complete (1) url:
I [mainview:6886] Load complete (0) url: cef-error:
Update: For the scheme mailto: this seems to work just fine. Even without an explicit entry to RequiredPermissions.
Update 2: In more recent versions (e.g. 0.8.4.124) clicks on links with custom URIs have no effect to the content anymore. The log states:
W [CefAppInstance.cpp:49 ] App spotify:app:tutorial is not allowed to access resource: myscheme:/test
The mailto: still to works without explicit RequiredPermissions.
Do you have any idea?
Unfortunately, as far as I'm aware this isn't supported in the Apps API.

How can I detect if an app is installed on an android device from within a web page

Here is the situation:
I have a web page that needs to check through JavaScript if my app is already installed on the android device it is currently running on.
If the app is installed the page will show a link (with custom protocol) to launch the app,
otherwise the page should show a link to the android market.
I can manage the links to app and to market. The only remaining step is to detect the presence of the app on the device from within JavaScript code (or perhaps try to catch a possible error of unsupported protocol as an indication of not existing app).
When I
click on a web link with
my custom app protocol and
the app is not yet installed on the device
I can see that the android environment generates an "protocol is not supported" type error.
Unfortunately, I am not able to capture that error in the JavaScript code to divert the user to the android market.
I guess both direct detection and error detection are valid methods if they exist at all.
Any ideas how can I accomplish that?
Thanks for help.
Don't use a custom protocol.
Instead, set up your <intent-filter> to watch for a well-known URL (e.g., set the scheme, host, and path). That URL should point to the page where your "download the app" instructions lie.
Then, if the user clicks the link to the well-known URL from someplace else, it will launch your app if installed, or will bring up your Web page if not.
I also had the same question, but I solve it like this:
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1;
if(isAndroid) { // if is android
// your app address for local
var ifrSrc = 'intent://example.com#Intent;scheme=my_scheme;action=android.intent.action.VIEW;end';
var ifr = document.createElement('iframe');
ifr.src = ifrSrc ;
ifr.onload = function() { // if app is not installed, then will go this function
window.location = 'http://your.app_download_page.com';
};
ifr.style.display = 'none';
document.body.appendChild(ifr);
setTimeout(function(){
document.body.removeChild(ifr); // remove the iframe element
}, 1000);
} else { // if is not android
window.location = 'http://google.com';
}
Hope this can help someone may has this problem.
You can use an iframe which src url is the custom protocol of your app while at the same time you can display your webpage if the custom protocol is not handled. You can also set the iframe to invisble using frameborder="0" and width and height = 0;

Accessing and modifying tabs opened using window.open in Google Chrome

I used to be able to do this to create an exported HTML page containing some data. But the code is not working with the latest version of Google Chrome (It works all right with Chrome 5.0.307.11 beta and all other major browsers).
function createExport(text) {
var target = window.open();
target.title = 'Memonaut - Exported View';
target.document.open();
target.document.write(text);
target.document.close();
}
Chrome now complains that the domains don't match and disallows the JavaScript calls as unsafe. How can I access and modify the document of a newly opened browser-tab in such a scenario?
I also got this problem when using a local page using the file:// protocol (in Chromium 5.0.342.9 (Developer Build 43360) under Linux). The exact error message is:
Unsafe JavaScript attempt to access
frame with URL about:blank from frame
with URL
file:///home/foo/bar/index.htm.
Domains, protocols and ports must
match.
Apparently the protocols don't match, but the good news is: when this page on a web server, Chromium also opens a new window as "about:blank", but it doesn't complain any longer. It also works when using a local web server accessed via http://localhost.
EDIT: there is bug filed upstream about this. According to this comment, it is fixed and it will be rolled into trunk shortly.
UPDATE: this bug is now fixed, the following test case works properly:
var target = window.open();
target.title = 'Memonaut - Exported View';
target.document.open();
target.document.write("test");
target.document.close();
Here is the explanation I think
http://groups.google.com/group/chromium-dev/browse_thread/thread/9844b1823037d297?pli=1
Are you accessing any data from other domain? Not sure, but that might be causing this problem.
One alternative would be a data: protocol URL.
https://developer.mozilla.org/en/data_URIs
http://msdn.microsoft.com/en-us/library/cc848897%28VS.85%29.aspx
var durl = "data:text/html," + encodeURIComponent(text);
var target = window.open(durl);
Supported in all modern browsers except IE7 and below.
you can also try closing self tab/window by using this code,
originally I've made this small greasemonkey script sometime ago in order to close
bad popups and ad windows, it worked fair (not too brilliant though)...
//window.addEventListener("load", function () {
window.addEventListener("onbeforeunload", function () {
try {
// clear inner html content to prevent malicious JS overrides.
document.getElementsByTagName("html")[0].innerHTML = "";
window.open("javascript:window.close();", "_self", "");
window.open("javascript:window.close();", "_self", "");
}
catch (e) {}
}(), false);

Categories

Resources