6

第一次延期——请善待。

我的目标是在用户访问来自特定域的页面时提供一个选项,以提供启动另一个页面的选项,该页面使用访问页面的部分域名作为变量。

下面的代码做了我想要的,但它没有提供作为选项的操作——它只是执行。

当访问与域匹配的页面时,它会在地址栏中添加一个图标。我希望仅在用户单击该图标时才加载调用的新页面。如果这不可能,请提出替代方案。

谢谢!

function checkForValidUrl(tabId, changeInfo, tab) {

  if (tab.url.indexOf('.foo.com') > -1) {

    chrome.pageAction.show(tabId);
    var myName = tab.url.split(".")[0].slice(7);

    if (myName != "www"){ //ignore main site

    chrome.tabs.update(tab.id, {url: "http://foo.com/foo.html?t=" + myName});
    }

  }
};

chrome.tabs.onUpdated.addListener(checkForValidUrl);
4

2 回答 2

12

你只需要使用chrome.pageAction.onClicked. 例如:

function checkForValidUrl(tabId, changeInfo, tab) {
  if (tab.url.indexOf('.foo.com') > -1) 
    chrome.pageAction.show(tabId);
};

chrome.tabs.onUpdated.addListener(checkForValidUrl);

chrome.pageAction.onClicked.addListener(function(tab){
  var myName = tab.url.split(".")[0].slice(7);
  if (myName != "www") //ignore main site
    chrome.tabs.update(tab.id, {url: "http://foo.com/foo.html?t=" + myName});
});
于 2013-03-29T21:12:31.117 回答
3

上面的答案是正确的,但请注意,您需要在清单中拥有正确的权限才能使其正常工作,否则tab.url将是未定义的。

From the docs The URL the tab is displaying. This property is only present if the extension's manifest includes the "tabs" permission.

Link - https://developer.chrome.com/extensions/tabs#type-Tab

于 2015-06-03T13:32:39.657 回答