开发者

Chrome extensions: open link in new tab upon loading of a particular page?

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.

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.

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>
开发者_如何学Python

and my manifest:

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


In a background page:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜