2017-02-09 38 views
1

我認爲這將是一個經常問到的問題,但我無法找到答案。 有沒有一種方法可以讓Chrome瀏覽器擴展程序的瀏覽器操作彈出窗口僅在content_scripts匹配頁面上可用?僅在「content_script」上激活的瀏覽器操作匹配

例如:我爲網站做了擴展。我希望彈出窗口僅在瀏覽網站時可用(在當前選項卡上)。我該怎麼做?

對於我來說,默認情況下瀏覽器操作始終處於啓用狀態,無論我正在瀏覽哪個頁面,頁面操作都未啓用。

感謝您的幫助

編輯:這裏是清單的一部分,我沒有成功pageAction改變。我希望我的圖標僅在瀏覽LDLC的標籤上激活。

{ 
    "content_scripts": [ 
    { 
     "js": [ "jquery.min.js", "scrooge.js" ], 
     "matches": [ "*://www.ldlc.com/fiche/*", "*://ldlc.com/fiche/*" ] 
     }], 

    "manifest_version": 2, 

    "permissions": [ "storage", "activeTab","*://ldlc.com/*", "*://scroogealpha.esy.es/*" ], 

    "page_action": { 
      "default_title": "Worth buying?",  // optional; shown in tooltip 
      "default_popup": "popup.html"  // optional 
    }, 

    "web_accessible_resources": [ 
    ], 
    "background":{ 
    "scripts" : ["eventPage.js"], 
    "persistent": false 
    } 
} 
+0

請參閱[declarativeContent API](https://developer.chrome.com/extensions/declarativeContent)。 – wOxxOm

回答

0

使用頁面操作並在後臺腳本中監聽標籤更改。當標籤網址更改時,請檢查您是否要顯示網頁操作。

在你的後臺腳本做這樣的事情:

let matches = ["://www.ldlc.com/fiche/", "://ldlc.com/fiche/" ] 
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { 
    for (let i in matches) { 
     if (tab.url.includes(matches[i])) { 
      chrome.pageAction.show(tabId) 
      break 
     } 
    } 
}) 

還要添加標籤許可,您的清單。

+0

我使用了一個事件腳本頁面,而不是一個背景腳本,它似乎對此更有意義 – yhu420

相關問題