Last active
May 12, 2017 20:15
-
-
Save englishextra/b9a8140e1c1b8aa01772375aeacbf49b to your computer and use it in GitHub Desktop.
Open external links in default browser out of Electron / nwjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* Open external links in default browser out of Electron / nwjs | |
* gist.github.com/englishextra/b9a8140e1c1b8aa01772375aeacbf49b | |
* stackoverflow.com/questions/32402327/how-can-i-force-external-links-from-browser-window-to-open-in-a-default-browser | |
* github.com/nwjs/nw.js/wiki/shell | |
* electron - file: | nwjs - chrome-extension: | http: Intel XDK | |
* wont do in electron and nw, | |
* so manageExternalLinks will set target blank to links | |
* var win = w.open(url, "_blank"); | |
* win.focus(); | |
* @param {String} url URL/path string | |
* openDeviceBrowser(url) | |
* detect Node.js | |
* github.com/lyrictenor/node-is-nwjs/blob/master/is-nodejs.js | |
* @returns {Boolean} true or false | |
* detect Electron | |
* @returns {Boolean} true or false | |
* detect NW.js | |
* github.com/lyrictenor/node-is-nwjs/blob/master/index.js | |
* @returns {Boolean} true or false | |
*/ | |
(function (root) { | |
"use strict"; | |
var isNodejs = "undefined" !== typeof process && "undefined" !== typeof require || "", | |
isElectron = "undefined" !== typeof window && window.process && "renderer" === window.process.type || "", | |
isNwjs = function () { | |
if ("undefined" !== typeof isNodejs && isNodejs) { | |
try { | |
if ("undefined" !== typeof require("nw.gui")) { | |
return !0; | |
} | |
} catch (e) { | |
return !1; | |
} | |
} | |
return !1; | |
} | |
(), | |
openDeviceBrowser = function (url) { | |
var triggerForElectron = function () { | |
var es = isElectron ? require("electron").shell : ""; | |
return es ? es.openExternal(url) : ""; | |
}, | |
triggerForNwjs = function () { | |
var ns = isNwjs ? require("nw.gui").Shell : ""; | |
return ns ? ns.openExternal(url) : ""; | |
}, | |
triggerForHTTP = function () { | |
return !0; | |
}, | |
triggerForLocal = function () { | |
return root.open(url, "_system", "scrollbars=1,location=no"); | |
}; | |
console.log("triggered function: openDeviceBrowser"); | |
if (isElectron) { | |
triggerForElectron(); | |
} else if (isNwjs) { | |
triggerForNwjs(); | |
} else { | |
var locationProtocol = root.location.protocol || "", | |
hasHTTP = locationProtocol ? "http:" === locationProtocol ? "http" : "https:" === locationProtocol ? "https" : "" : ""; | |
if (hasHTTP) { | |
triggerForHTTP(); | |
} else { | |
triggerForLocal(); | |
} | |
} | |
}; | |
root.openDeviceBrowser = openDeviceBrowser; | |
})("undefined" !== typeof window ? window : this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment