簡體   English   中英

如何為特定頁面顯示page_action?

[英]How do I make page_action appear for specific pages?

我正在玩一些chrome擴展,我發現了這個例子: http//src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/pageAction/pageaction_by_url/

一切正常,但我想創建自己的擴展,我想在特定網站上看到page_action圖標,而不是在網址中看到'g'的圖標。 所以我試着簡單地從這個改變腳本:

// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Called when the url of a tab changes.
function checkForValidUrl(tabId, changeInfo, tab) {
// If the letter 'g' is found in the tab's URL...
if (tab.url.indexOf('g') > -1) {
// ... show the page action.
chrome.pageAction.show(tabId);
}
};

// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);

進入:

chrome.pageAction.show(tabId);

但現在它不起作用......我不明白。 顯然我可以使用一種解決方法,但這不是重點......首先,我必須創建一個后台頁面來執行此操作嗎? 我想是的,但我不明白為什么,為什么.show方法不能單獨工作? 我試着搜索谷歌文檔和東西,但我找不到任何有用的東西,我不是專家,這是我第一個下午花在谷歌擴展,但我怎么知道“chrome.page.show( tabId)“如果沒有寫在任何地方,必須進入后台頁面? 沒有意圖批評,但你們怎么會發現? 所有chrome方法都必須放在后台頁面中? 那么,肯定有更多的問題,那么它的合法性。 希望你能給我至少一個答案!

http://code.google.com/chrome/extensions/pageAction.html
... ...說

默認情況下,隱藏頁面操作。 顯示時,指定應顯示圖標的選項卡。 圖標保持可見,直到選項卡關閉或開始顯示不同的URL(例如,因為用戶單擊鏈接)。

因此,即使你的tabid有效,它也會像你唯一運行的chrome.pageAction.show(tabId);一樣快速消失chrome.pageAction.show(tabId); 一旦背景頁面第一次運行。
您需要不斷檢查后台標簽的更改,因為pageactions在清單中沒有匹配/ exclude_matches設置,就像內容腳本一樣(遺憾)。 所以你必須檢查自己並回應變化。
如果您希望它適用於特定網站,只需將其更改為...

// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Called when the url of a tab changes.
function checkForValidUrl(tabId, changeInfo, tab) {
    // If the tabs url starts with "http://specificsite.com"...
    if (tab.url.indexOf('http://specificsite.com') == 0) {
        // ... show the page action.
        chrome.pageAction.show(tabId);
    }
};

// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);

對於那些尋找處理子域的方法的人來說,如果你的網站有一個子域名,例如blog.specificsite.com ,或者需要使用通配符,你也可以使用這種格式的正則表達式

function checkForValidUrl(tabId, changeInfo, tab) 
{
    if(typeof tab != "undefined" && typeof tab != "null" )
    {
        // If the tabs URL contains "specificsite.com"...
        //This would work in the same way as *specificsite.com*, with 0 or more characters surrounding the URL.
        if (/specificsite[.]com/.test(tab.url)) 
        {
            // ... show the page action.
            chrome.pageAction.show(tabId);
        }
    }
};

// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);

匹配URL中的子字符串。 它還有助於計算進行空/未定義檢查以避免其他異常處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM