9

I have a URL shortening Chrome extension called Shrtr. Right now, it allows users to copy the shortened URL to clipboard, but in the next version, I've added the ability to email the shortened URL, using a mailto: link (i.e. mailto:?subject=<original page title>&body=<short URL>).

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

Method 1: window.open

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

Notice the need to wait before closing the window. This works (i.e. mail client new message dialog appears, pre-populated), but the new tab remains open.

Method 2: using chrome.tabs

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

Again, works - but tab remains open. Any ideas?

Mike Causer
  • 8,196
  • 2
  • 43
  • 63
Traveling Tech Guy
  • 27,194
  • 23
  • 111
  • 159
  • are you using just a background script currently? – 1337holiday Feb 15 '14 at 03:07
  • no, this is a popup html. I have "tabs" permissions. – Traveling Tech Guy Feb 15 '14 at 03:31
  • hmm what email client are you trying to bring up? Because I use Gmail and the way your method 2 works seems to be fine, I can see the new compose in the newly created tab. Unless you are launching something like outlook or some other native mail client, only then you will have that issue. – 1337holiday Feb 15 '14 at 04:32
  • The default mail client for the machine – Traveling Tech Guy Feb 15 '14 at 05:28
  • Yes but if you were to close that window/tab you would break your app for people who use browser based email clients like Gmail for instance. You "could" potentially track the URL of the newly created tab and see if it opens gmail, if not then you can try removing it. – 1337holiday Feb 15 '14 at 19:12
  • Not necessarily - for people who use Gmail as their default mail client, the `mailto:` protocol simply opens a new 'compose' tab in their Gmail tab. The new tab created is a side effect and should be closed. – Traveling Tech Guy Feb 15 '14 at 19:23
  • hmm right well i found this, maybe this can help http://stackoverflow.com/questions/3202613/mailto-link-not-working-in-chrome-extension-popup – 1337holiday Feb 15 '14 at 19:37
  • Thanks @1337holiday - same question - no good answer (in fact, the guy tried just what I did in solution 1). – Traveling Tech Guy Feb 17 '14 at 03:50
  • This code would work fine (except from the popup page) but it is setTimeout not setTimeOut. no capital O. Still have the problem though that you don't want to close the page if Chrome is using a mailto: handler for something like Gmail but you do want to close it if it is using a standalone client. – Michael Updike Mar 09 '17 at 16:47

4 Answers4

7
var emailUrl = "mailto:blah@blah.com";

    chrome.tabs.update({
        url: emailUrl
    });
Yiping
  • 971
  • 10
  • 31
6

I realize this is an old question, but I myself had the same issue, and I figured out how to solve the problem so I thought I would share.

The issue stems from the fact that (I believe) you are calling the below code from your extension popup page.

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

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

We can remedy this by moving that code into a function within the background page whose lifetime is not tied to the 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 via chrome.extension.getBackgroundPage().sendEmail() from your popup page.

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

Traveling Tech Guy
  • 27,194
  • 23
  • 111
  • 159
Gabe O'Leary
  • 2,010
  • 4
  • 24
  • 41
  • Thank you, it works somehow. But after closing the chrome will focus to the right most tab (May not be the previous tab) – Yiping Dec 18 '16 at 05:19
  • 1
    @Yiping you could get around this by storing the active tab id and setting it to active again when after your tab is closed. – Gabe O'Leary Jan 18 '17 at 00:33
0

wouldnt u need a close statement like this

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

credits go to Daniel How to close current tab in a 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 tab that works in IE, Firefox and Chrome?

Community
  • 1
  • 1
Tariq Khalaf
  • 89
  • 1
  • 1
  • 10
  • Thanks for trying, but that's not what I'm looking for: all I need is to open the default mail client, with the subject and body populated. Another open tab is a side-effect. I do not want/need to open another HTML page, only to close it later. Also, you'd notice, your solution's URL uses an `http://` protocol. This would not work with `mailto:`. – Traveling Tech Guy Feb 15 '14 at 05:30
0

I have solved the problem. Please check if it fits your solution space.

I have used the following code to open a 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 the element was :

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

This worked for me.

Hope it will help others.

Regards,
Karan Ratra

Ram Narayan
  • 171
  • 1
  • 5