2013-04-09 37 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