如何从Chrome扩展程序打开mailto:链接? [英] How to open a mailto: link from a Chrome Extension?

查看:906
本文介绍了如何从Chrome扩展程序打开mailto:链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个缩短了Chrome扩展程序的URL,名为 Shrr 。现在,它允许用户将缩短的URL复制到剪贴板,但在下一个版本中,我添加了通过电子邮件发送缩短的URL的功能,使用 mailto:链接(即 mailto:?subject =< original page title>& body =< short URL> )。

问题是,你不能只从一个扩展名中分配 document.location.href ='mailto ...'; 。以下两种方法适用于我,但是对于这两种方法,我最终在浏览器中打开了一个空白选项卡:

方法1: window.open

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

注意在关闭窗口之前需要等待。这个工程(即邮件客户端新消息对话框出现,预填充),但新标签保持打开状态。



方法2:使用 chrome.tabs

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

同样,作品 - 但标签保持打开状态。任何想法?

解决方案

我意识到这是一个老问题,但我自己也有同样的问题,我想出了如何解决问题,所以我想我会分享。



这个问题源于(我相信)您从扩展弹出页面调用下面的代码。

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

问题在于,只要创建新选项卡,弹出页面就会死掉,回调代码永远不会执行。



我们可以通过将代码移动到背景页面中的一个函数来补救这个问题,该页面的生命周期并不依赖于弹出页面:

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

并通过 chrome.extension.getBackgroundPage( ).sendEmail()从您的弹出页面。



使用上述方法,默认的电子邮件客户端将被打开,新的选项卡将在500毫秒后自动关闭。

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?

解决方案

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.

这篇关于如何从Chrome扩展程序打开mailto:链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆