Chrome扩展程序:加载特定页面后,在新标签页中打开链接吗? [英] Chrome extensions: open link in new tab upon loading of a particular page?

查看:77
本文介绍了Chrome扩展程序:加载特定页面后,在新标签页中打开链接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Chrome扩展程序,在其中打开特定URL时,另一个URL在新标签页中自动打开.请注意,我希望新标签会在第一个URL开始加载后立即打开,而不是在加载完成后立即打开.

I'd like to create a Chrome extension where, when a particular URL opens, another URL opens automatically in a new tab. Note that I'd like the new tab to open as soon as the first URL begins to load, not once it's finished loading.

因此,基本上,您将单击指向第一个URL的链接,然后将打开两个选项卡.一个包含第一个URL,一个包含第二个URL.

So basically, you'd click on a link to the first URL, and two tabs would open; one containing the first URL, one containing the second URL.

但是,我真的不知道从哪里开始.我认为这不能成为内容脚本,因为它们无法访问chrome.tabs.因此,我尝试使用背景页面,但它不起作用.这是我的bg.html:

However, I really don't know where to begin. I don't think it can be a content script because they can't access chrome.tabs. So I'm trying to use a background page but it's not working. Here's my bg.html:

<html>
<body>
<script type="text/javascript">
var o = new Object;
chrome.tabs.onCreated.addListener(
    function(Tab tab)
    {
        chrome.tabs.create(o);
    }
);
</script>
</body>
</html>

和我的清单:

{
  "name": "My First Extension",
  "version": "1.0",
  "description": "The first extension that I made.",
  "background_page": "bg.html",
  "permissions": [
    "tabs"
  ]
}

推荐答案

在后台页面中:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if(changeInfo.status == "loading" && tab.url == "http://old/url") {
        chrome.tabs.create({url: "http://new/url"});
    }
});

这篇关于Chrome扩展程序:加载特定页面后,在新标签页中打开链接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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