Launch a custom protocol handler using javascript in Chrome without a popup? - javascript

I am trying to start a custom protocol handler in Chrome via Javascript. I can get the app to start but to do so creates a popup window which then triggers a pop-up blocker. Is there anyway to start the app without a pop-up window? The window closes but it is still considered a pop-up.
This is my current code:
function setBrowser() {
var userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf("chrome") > -1) {
//If Chrome launch without ActiveX involved
var url = 'ABCProcessLaunch:-Domain mydomain -Args http://google.com -requirelogin true -method "chrome"';
setTimeout(window.close, 10);
window.open(url, '_blank');
}
}

I'm inferring from your call to window.close that this is likely why you need to open the link in a new window in the first place.
You may have some luck with opening the custom URL scheme in an <iframe> element instead. That way, your setTimeout call will still be triggered after 10 seconds:
function setBrowser() {
var userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf("chrome") > -1) {
//If Chrome launch without ActiveX involved
var url = 'ABCProcessLaunch:-Domain mydomain -Args http://google.com -requirelogin true -method "chrome"';
var iframe = document.createElement("iframe");
iframe.src = url;
setTimeout(window.close, 10);
document.querySelector("body").appendChild(iframe);
}
}

Related

How to stop window.open from refreshing the window in javascript?

Lets say you have code like this.
function openNewTab(url) {
const newTab = window.open(url, 'windowNameHehe');
};
Now every time you call this function it will open a new tab. However, if the window is already opened it will go to it and refresh/reload it. How do I stop it from reloading/refreshing and just bringing it to view?
Doing something like newTab.focus() or adding the boolean (true/false) in the 4th parameter of window.open isn't working for me either. Any help?
function openNewTab(url) {
var newTab = window.open(url, 'windowNameHehe', , false);
newTab.focus();
}
https://www.w3schools.com/jsref/met_win_open.asp
Open a URL in a new tab (and not a new window) using JavaScript
function openNewTab(url) {
const newTab = window.open('', 'windowNameHehe');
newTab.focus();
if (!newTab.location.hostname) {
newTab.location.href = url;
}
};

Opening a URL in new tabs generates 404 when URL isn't complete

I want to open a new tab when a user clicks on a link. The link is user generated and when users enter a url like www.google.com the tab doesn't work as expected. This is a jsFiddle to show the problem and this is the code:
open new tab
How can I fix this? Telling users to enter a well-formed URL is not an option.
Don't Forget The Protocol
The window.open() function expects the appropriate protocol to be present when requesting the URL (i.e. http://, ftp://, https://, etc.), so you'll need to ensure that you include this to work properly :
window.open('http://www.google.com');
Manually Prepending The Protocol
If you need to explicitly omit the protocol for whatever reason, you could consider writing a function to handle opening your window for you :
function openWindowAndPrependProtocol(url){
if(/^https?:\/\//i.test(url)){
// If it doesn't start with http:// or https://, then append it
return window.open('http://' + url);
}
}
which would change your code to use :
var a = openWindowAndPrependProtocol('www.google.com');
Note
According to Mozilla, they generally don't recommend using links in this manner to open new windows via window.open() if at all possible, as it can bring about some usability concerns. Their best practices recommends using an external function similar to the second approach to handle opening the window.
If you want to add a protocol automatically to an URL, which a user of your page provided, you can do this with the following function:
JavaScript (needs be added somewhere on your page):
<script type="text/javascript">
function openURL(url) {
var allowedProtocols = ["http://", "https://", "ftp://", "ftps://"];
var hasPrefix = false;
for(var i = 0; i < allowedProtocols.length; i++) {
if(url.substring(0, allowedProtocols[i].length) === allowedProtocols[i]) {
hasPrefix = true;
break;
}
}
if(!hasPrefix) {
url = "http://" + url;
}
window.open(url);
}
</script>
HTML:
<a onclick="openURL('www.google.com')">Google</a>
Try this:
<a onclick="window.open('https://www.google.com', 'Google', 'width=800,height=800')">
It opens in a new tab and 800×800 are the dimensions of the window.
You need to add a protocol like http:// or https://
open new tab`

IE Popup targeting issue with Javascript referrer hack

I've run across some code that is using the following type of referrer hack to pass along referrer information to a popup window via Javascript in IE:
var targetName = "linkTarget";
_win = window.open("", targetName, 'width=800,height=480,resizable=yes,status=yes,location=yes,toolbar=no');
var _link;
if (!document.getElementById("referrerHackLink")) {
_link = document.createElement('a');
_link.id = "referrerHackLink";
document.body.appendChild(_link);
}
else {
_link = document.getElementById("referrerHackLink");
}
_link.target = targetName;
_link.href = url;
_link.click();
HTTP Referrer and IE7 and IE8
http://webbugtrack.blogspot.com/2008/11/bug-421-ie-fails-to-pass-http-referer.html
In an interesting twist, when I attempt to use that method with certain URIs (including localhost), IE9 and IE10 blur the popup window and navigate to the target URL in the main window.
You can find an example here:
http://jsfiddle.net/geoffreymoller/JKXzk/
Any ideas on why the localhost version might change the popup and target information like that?
_win = window.open("", targetName, 'width=800,height=480,resizable=yes,status=yes,location=yes,toolbar=no');
i think the window name in IE is limited to names like _self, _top, _blank, etc... as a result any name not like the expected list might be treated like _self

Check if window is already open window.open

I have a html page. In the body of the page I am calling onload event which calls javascript function to open a pop up window. here is the code:
var newWindow = null;
function launchApplication()
{
if ((newWindow == null) || (newWindow.closed))
{
newWindow = window.open('abc.html','','height=960px,width=940px');
}
}
when I move to another page, and come back to that page again, popup reopens, although it is already opened. Please guide me to proper direction so that if pop up is already open then it should not open again. I tried document.referred but it requires the site online, currently I am working offline.
newWindow = window.open('abc.html','com_MyDomain_myWindowForThisPurpose','height=960px,width=940px');
Give the window a name. Basing the name on your domain like this, prevents the chances of you picking a name someone else happened to choose.
Never make up a name that begins with _, those are reserved for special names the browser treats differently (same as with the "target" attribute of anchor elements).
Note that if the window of that name was opened with different options (e.g. different height), then it'll keep those options. The options here will only take effect if there is no window of that name, so you do create a new one.
Edit:
Note that the "name" is of the window, not of the content. It doesn't affect the title (newWindow.document.title will affect that, as of course will code in abc.html). It does affect other attempts to do stuff across windows. Hence another window.open with the same name will reuse this window. Also a link like clicky! will re-use it. Normal caveats about browsers resisting window-opening in various scenarios (popup-blocking) apply.
To open a window and keep a reference to it between page refresh.
var winref = window.open('', 'MyWindowName', '');
if(winref.location.href === 'about:blank'){
winref.location.href = 'http://example.com';
}
or in function format
function openOnce(url, target){
// open a blank "target" window
// or get the reference to the existing "target" window
var winref = window.open('', target, '');
// if the "target" window was just opened, change its url
if(winref.location.href === 'about:blank'){
winref.location.href = url;
}
return winref;
}
openOnce('http://example.com', 'MyWindowName');
You can check if the window is open or closed by re-assigning a reference to it when it closes. Example:
var newWindow;
var openWindow = function(){
newWindow = newWindow || window.open('newpage.html');
newWindow.focus();
newWindow.onbeforeunload = function(){
newWindow = null;
};
};
Use the "closed" property: if a window has been closed its closed property will be true.
https://developer.mozilla.org/en-US/docs/Web/API/Window/closed
When you move on another page (on the same domain), you can re-set the window.open variable with popup page like this :
https://jsfiddle.net/u5w9v4gf/
Step to try :
Click on Run (on jsfiddle editor).
Click on Try me (on preview).
Click on Run to move on another page, the variable will be re-set.
Code :
window.currentChild = false;
$("#tryme").click(function() {
if (currentChild) currentChild.close();
const child = window.open("about:blank", "lmao", 'width=250,height=300');
currentChild = child;
//Scrope script in child windows
child.frames.eval(`
setInterval(function () {
if (!window.opener.currentChild)
window.opener.currentChild = window;
}, 500);
`);
});
setInterval(function() {
console.log(currentChild)
if (!currentChild || (currentChild && currentChild.closed))
$("p").text("No popup/child. :(")
else
$("p").text("Child detected !")
}, 500);

Opening my page in firefox after installing the addon

HI,
My am trying to open my home page after the firefox restarts for the first time after installation.
For this i am adding the event handler on load page and checks where this event is executed for the first time or not.
window.addEventListener("load", initializeOverlay, false);
But my problem is how to open the page in new tab when the firefox get started. I use
`window.open("https://www.xyz.com/");`
but that opens the page in new window that might even be open in internet explorer.
So is there any way to open the page in new tab in same window which is going to be open.
Thanks
BHAVIK GOYAL
I manage to do something similar using preferences, rather than creating files, etc.
Inside of /defaults/preferences/default.js:
pref("extensions.extension_name.just_installed", true);
pref("extensions.extension_name.post_install_url", "http://www.google.com");
Then inside the main JS file for the add-on:
// Retrieve the preferences.
var prefs;
prefs = Components.classes["#mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.extension_name.");
prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
// If we just installed, open the post-install page and update the preferences.
var just_installed = prefs.getBoolPref("just_installed");
var post_install_url = prefs.getCharPref("post_install_url");
if (just_installed) {
prefs.setBoolPref("just_installed", false);
gBrowser.selectedTab = gBrowser.addTab(prefs.getCharPref("post_install_url"));
}
Only problem is Firefox doesn't reset the preferences saved by an extension after that extension is uninstalled.
I got the answer. We can add the gbrowser.open("http://www.xyz.com/") to open in new tab and this statement has to be executed in new function that is by calling the other event handler function loadedoverlay which is defined as follow:
function loadedOverlay() {
try{
var file = Components.classes["#mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(Components.classes["#mozilla.org/file/directory_service;1"].getService( Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile).path+"\\initialstart.txt");
if ( file.exists() == true )
{
}
else
{
file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
var Website="http://www.emailstationery.com/";
gBrowser.addTab(Website);//This is for new Tab
}
} catch(e) {}
}
The call to this function has to be add in the load event function by adding the code of lines as below:-
var appcontent = document.getElementById("appcontent"); // browser
if(appcontent)
appcontent.addEventListener("DOMContentLoaded", loadedOverlay, true);

Categories

Resources