0

我的扩展程序正在使用页面操作(仅在 twitter.com 上可用),我希望在地址栏中使图标可见(并且现在至少为空),但我无法让它工作。我使用了文档三明治示例并对其进行了修改,使其看起来像这样:

内容脚本.js:

// Called when the url of a tab changes.
if(chrome.tabs.query(active(true),function{
  // If the url is twitter
  ( {'url':'https://google.com/google-results-string'}
  if (chrome.tabs.query({'url':'*://twitter.com/*'} , function(tabs){console.log(tabs)}){
    // ... show the page action.
    //chrome.pageAction.show(tabId);
    chrome.extension.sendRequest({}, function(response) {});
  }
};
// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);

背景.js

function onRequest(request, sender, sendResponse) {
// Show the page action for the tab that the sender (content script)
// was on.
    chrome.pageAction.show(sender.tab.id);

// Return nothing to let the connection be cleaned up.
    sendResponse({});
};

// Listen for the content script to send a message to the background page.
chrome.extension.onRequest.addListener(onRequest);

我不确定为什么它不起作用,我不确定如何使用 chrome.tabs.query() url 属性以及如何对照 * ://twitter.com/ * 进行检查。

4

1 回答 1

1

Page Action by Content当您应该查看示例时,您链接并使用了Page Action by Url示例。您所需要的只是background页面中的类似内容:

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

content script如果您只想检查 url,则无需使用 a 。

编辑:如果您只想针对当前选项卡进行测试,那么您可以执行以下操作:

chrome.tabs.onUpdated.addListener(function(tabId,info,tab){
  if(tab.active){
    if (tab.url.indexOf('twitter.com') > -1)
      chrome.pageAction.show(tabId);
  }
});
于 2013-04-09T20:58:27.330 回答