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

倖福魔咒の 提交于 2020-01-06 05:05:32

问题


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>

and my manifest:

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

回答1:


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"});
    }
});


来源:https://stackoverflow.com/questions/6642084/chrome-extensions-open-link-in-new-tab-upon-loading-of-a-particular-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!