【问题标题】:How do I make page_action appear for specific pages?如何让 page_action 出现在特定页面上?
【发布时间】:2019-02-25 01:56:44
【问题描述】:

我在玩一些 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 方法都必须进入后台页面? 好吧,肯定比它的合法性要多得多。希望你能给我至少一个答案!

【问题讨论】:

  • 你给它一个有效的tabId吗?

标签: javascript google-chrome-extension


【解决方案1】:

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

默认情况下,页面操作是隐藏的。当你显示它时,你指定 应出现图标的选项卡。该图标一直可见,直到 选项卡已关闭或开始显示不同的 URL(因为 例如,用户点击一个链接)。

因此,即使您的 tabid 是有效的,它也会很快消失,因为当背景页面首次运行时,您只运行一次 chrome.pageAction.show(tabId);
您需要不断检查后台选项卡的更改,因为页面操作在清单中没有 match/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);

【讨论】:

  • 知道了,非常感谢。我读过你引用的那段话,但显然我不是很明白!
  • 嗯,您也可以回答这个问题:假设 bg.js 是包含(并且)我引用的代码(或您引用的那个)的文件.为什么如果我写这个: 在 background.html 中它确实有效,但如果我尝试直接在 background.html 中编写代码它不会?
  • 我的猜测是你的清单文件中有"manifest_version": 2,这将不允许内联
  • 你又说对了。非常感谢!很高兴您在回答我时发现了一些新的东西,所以这对您来说并不是完全浪费时间! :D
【解决方案2】:

对于那些正在寻找处理子域的方法的人,如果您有一个带有子域的站点,例如 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 中的子字符串。它还有助于计算执行空/未定义检查以避免额外的异常处理。

【讨论】:

    猜你喜欢
    • 2013-08-08
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    相关资源
    最近更新 更多