How to open mailto: link from Chrome extension?

I have a shortcut for the Chrome extension url called Shrtr . It currently allows users to copy the shortened URL to the clipboard, but in the next version I added the ability to send the shortened URL using the mailto: link (i.e. mailto:?subject=<original page title>&body=<short URL> ).

The problem is that you cannot just assign document.location.href = 'mailto...'; from the extension. The following two methods worked for me, but with both ends with an open, blank tab in the browser:

Method 1: window.open

 var wnd = window.open(emailUrl); setTimeOut(function() { wnd.close(); }, 500); 

Pay attention to the need to wait until the window closes. This works (for example, a dialog box appears for a new mail client message, pre-populated), but the new tab remains open.

Method 2: use chrome.tabs

 chrome.tabs.create({ url: emailUrl }, function(tab) { setTimeOut(function() { chrome.tabs.remove(tab.id); }, 500); }); 

Again, work - but the tab remains open. Any ideas?

+6
source share
4 answers

I understand that this is an old question, but I also had the same problem, and I figured out how to solve the problem, so I decided to share it.

The problem is that (I believe) you are calling the code below from your popup page.

 chrome.tabs.create({ url: emailUrl }, function(tab) { setTimeout(function() { chrome.tabs.remove(tab.id); }, 500); }); 

The problem is that as soon as a new tab is created, the popup page dies and the callback code never executes.

We can fix this by moving this code to a function on a background page whose life time is not tied to a popup page:

 function sendEmail() { var emailUrl = "mailto: blah@blah.com "; chrome.tabs.create({ url: emailUrl }, function(tab) { setTimeout(function() { chrome.tabs.remove(tab.id); }, 500); }); } 

and calling it through chrome.extension.getBackgroundPage().sendEmail() from its popup page.

Using the above method, the email client will be opened by default, and the new tab will be automatically closed after 500 milliseconds.

+5
source
 var emailUrl = "mailto: blah@blah.com "; chrome.tabs.update({ url: emailUrl }); 
+2
source

You do not need to close an operator like this

 <a href="javascript:window.open('','_self').close();">close</a> 

loans go to Daniel How to close the current tab in the browser window?

or even something like this

 //keep a handle to the window you open. var newWin = window.open('my window', 'http://.../'); ... //some point later in the code newWin.close(); 

Credits go to Tracker1 Javascript Code to close a tab that works in IE, Firefox and Chrome?

0
source

I solved the problem. Please check if it suits your decision.

I used the following code to open the mailto client in chrome extension.

 $('.email').click(function(){ var hrefString = $(this).attr('href'); var myWindow = window.open(hrefString, "Opening mail client", "width=200, height=100"); myWindow.document.write("<p>Opening mail client.Please wait!!</p>"); setTimeout(function(){ myWindow.close() }, 2000); }); 

where element:

  <a style="padding: 0 0 0 5px;" href="mailto: abc@xyz.com " class="email"> 

It worked for me.

Hope this helps others.

Yours faithfully,
Karan Ratra

0
source

All Articles