2013-04-09 38 views
0

我的扩展使用的是页面动作(仅在twitter.com上可用),我希望在地址栏中显示图标(并且至少功能至少为空),但是我无法让它工作。 我用documentation sandwich sample和修改它,所以它看起来是这样的:检查当前标签对字符串

contentscript.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); 

background.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/ *来检查它。

+1

你的JavaScript语法是无效的。为什么添加jQuery标签? 'onRequest'已被弃用,使用'chrome.extension.onMessage' /'sendMessage',甚至是更新的'chrome.runtime.onMessage' /'sendMessage'。 – 2013-04-09 09:02:46

+0

我是Chrome扩展的初学者。感谢您注意到我的标签,我将删除它。 – Edeph 2013-04-09 09:06:04

回答

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如果你只是想查询的网址。

编辑:如果你只是想测试针对当前选项卡,那么你可以做这样的事情:

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

我想检查页面动作的“激活”url,之后我需要在页面中注入一些html,并做一些需要内容脚本的东西。但我会检查URL评估语法。 – Edeph 2013-04-10 04:15:58

+0

这是否也检查twitter.com的子页面? – Edeph 2013-04-10 04:27:14

+0

@Edeph这将显示包含字符串'twitter.com'的任何url的页面动作 – BeardFist 2013-04-10 04:48:09