How to launch a Chrome Extension popup from a background thread? - javascript

I want to write a Chrome Extension that will be monitoring for URI link clicks, and when it encounters that it is to launch its popup. I understand that the monitoring will be done in the background thread of the Extension, but I'm not sure how I can launch the popup from the background thread?
Think of the functionality like allowing a simple email editor in a popup - when someone clicks a mailto link, the popup appears to let user write an email and send it without taking them to a new browser window.

I don't think you can do that with registerProtocolHandler, but that doesn't mean you can't write your own content script:
window.addEventListener('click', function (e) {
if (e.target.href && e.target.href.indexOf('mailto:') != -1) {
var email = e.target.href.replace('mailto:', '');
// open your popup when an email is clicked
window.open(...);
}
}, false);

Related

Popping up facebook login dialog

I am writing a library to get short lived tokens from facebook.I am simply making an iframe and opening the dialog inside. If user is signed in before theres no problem but if user will be prompted first time facebook blocks iframe my code is below how can i solve this problem .
function Facebook (params){
this.url="https://facebook.com/dialog/oauth"+params;
this.Get=function(){
var css="position:absolute;top:50%;left:50%;\
transform:translate(-50%,-50%);width:500px;height:300px;z-index:9999",
frame=document.createElement("iframe"),
url=this.url;
frame.setAttribute("style",css);
return {
pop:function(){
document.getElementsByTagName("body")[0].appendChild(frame);
alert(url);
frame.setAttribute("src",url);
}
}
}
}
I solved this by opening popup if the popup from a trusted event such as click browsers doesnt block it while trying from console anyway they will block

detect if the useragent supports popup

Is there a way to detect if the current user agent support popup using native javascript?
for example in WEB, window.open will open a popup, and the parent will know when the popup is closed.
however in Iphone for example or Ipad it opens the popup in new tab and the parents will lose the context. so the parent's can't listen to the event when the new tab is closed.
Currently am using a work around, is by checking each device manually if it's supports popup and then in my javascript i check the user agent if it's included in the list i checked manually. but i was thinking if there are more intelligent way to implement this.
Thanks.
you can try this script:
<Script>
var windowName = 'userConsole';
var popUp = window.open('/popup-page.php', windowName, 'width=1000, height=700, left=24, top=24, scrollbars, resizable');
if (popUp == null || typeof(popUp)=='undefined') {
alert('Please disable your pop-up blocker and click the "Open" link again.');
}
else {
popUp.focus();// this will open popup if the browser allow it.you can do your implementation on popup support here
}
</script>
main source:here
What are you trying to accomplish, exactly? Are you trying to detect if the current window is a popup? If so, BornToCode's response in this thread might be your answer. He suggests to test if window.opener is defined:
if ( window.opener !== 'undefined' )

Can i Call Window.open from confirm dialog Box?

Can I call window.open from confirm dialog box using javascript?
My Requirement:
In case browser pop up blocker was enabled means ,i want to open pop up window using window.open(); but it was not happening so that i have to show the confirmation message which is "Browser popup blocker was enabled, now pop up has been open". now i click 'OK', on that time i want to call window.open() function.
<html>
<body onload="openPopup();">
<head>
onload Popup Window
</head>
</body>
<script>
function openPopup () {
var href= 'http://google.com';
popUp = window.open(href, "_blank");
if (popUp === null || typeof popUp === 'undefined') {
var ret = confirm("this alert is displayed by Blocker, Continue to Open ?");
if(ret){
window.open(href,"_new");
}
} else {
popUp.focus();
}
}
</script>
Is it possible or is there any other way to handle this scenario?
No.
Pop-ups are generally only allowed following a user's click, and as you've already seen in your code onload usually won't allow it.
However, you could use your own UI, to create a custom confirmation box that the user can click on to open the pop-up. This will work, unless the browser's pop-up blocker is overzealous enough to block even click-to-open popups without prior confirmation within the browser itself.

Open a url in Windows Metro App via Javascript

Normally, when I use in my metro application, the url is opening in the default web browser. I don't want to do this with anchor, I want to do the same behavior via Javascript and as async. But I don't know how to open the url with default browser.
Here is my code:
var $aTag = $("<a/>").click(function (event) {
showYesNoDialog(
"Do you approve?",
"The link will be opened in another window. Do you approve?",
"Yes", // Text of yes button
"No", // Text of no button
function () { // Behavior of yes button
// I tried this but nothing happened.
window.location.href = _URL_; // Should open in chrome or default web browser
},
function () { // Behavior of no button
// do nothing
}
);
});
What I also tried is that:
$("<a href='" + _URL_ + "'></a>").click();
But this didn't work, too.
Finally, I found my answer while searching on google.
Open a URL in a new tab (and not a new window) using JavaScript
I used this code to open the url out the metro app and it worked in my situation:
window.open(_URL_, '_blank');
window.focus();
You cannot launch an actual application from within Metro, but what you can do is launch a file with associated program, and that should give you the functionality you need.
Check Sample
The sample covers files and URI's - http://msdn.microsoft.com/library/windows/apps/Hh701476

Prevent pop-ups from being blocked

I am using a Facebook login method in my code on page load, but when I execute this code, the pop-up blocker closes the Facebook permission window.
How can I open this window with Javascript without needing to make an exception in the pop-up blocker?
Below is my code:
FB.login(function(response)
{
if(response.session!=null)
{
window.location.href='http://example.com';
}
},
{ perms: 'email,user_birthday,publish_stream' });
You can do something like -
var uri = encodeURI('http://example.com');
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
window.location.href=uri;
} else {
window.location = encodeURI("https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri="+uri+"&response_type=token");
}
This will just redirect directly instead of opening a pop-up
This is specifically denied in the documentation:
"You should only call this on a user event as it opens a popup. Most browsers block popups, unless they were initiated from a user event, such as a click on a button or a link."
It's also simply poor UX.
There'd be no point in popup blockers existing if you could just code around them. You'll either need to find a method that doesn't use a popup or require some user interaction with the browser to open the popup.
Yeah you need to call it with a user event, but strictly the onclick event, not any other:
Login <!-- works -->
Login <!-- doesnt work -->
Login <!-- doesnt work -->
If you try to open a popup automatically then there is a high possibility that popup blockers will become activated, as far as I know, it has to be based on some User action, for example click of a button. Try to execute this code on click of a button, it should work.

Categories