如何为特定页面显示 page_action? [英] How do I make page_action appear for specific pages?

查看:8
本文介绍了如何为特定页面显示 page_action?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩一些 chrome 扩展,我发现了这个例子:http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/pageAction/pageaction_by_url/

I was playing around with some chrome extensions and I found this example:http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/pageAction/pageaction_by_url/

一切正常,但我想创建自己的扩展程序,并且我想在特定站点上查看 page_action 图标,而不是在其 url 中带有g"的那些.所以我试着简单地改变脚本:

Everything works fine, but I want to create my own extension and I want to see the page_action Icon on a specific site, not ones with 'g' in their urls. So I tried simply to change the script from this:

// 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 方法都必须放在后台页面中?那么,肯定有更多的问题那么它的合法性.希望你能给我至少一个答案!

But now it doesn't work... I don't get it. Obviously I can use a workaround, but that's not the point... First of all, must I create a background page to do this? I think yes but I can't see why, and why the .show method doesn't work alone? I tried to search in the google documentation and stuff, but I couldn't find anything useful I'm no expert and this has been my first afternoon spent on google extension, but how should I know that the "chrome.page.show(tabId)" must go in a background page if it's not written anywhere? No intent to criticize, but how the hell did you guys find out? All chrome methods must go in a background page? Well, definitely much more questions then what its legit. Hope you can give me at least one answer!

推荐答案

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

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

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

By default, a page action is hidden. When you show it, you specify the tab in which the icon should appear. The icon remains visible until the tab is closed or starts displaying a different URL (because the user clicks a link, for example).

因此,即使您的 tabid 有效,当后台页面首次运行时,它也会很快消失,因为您只运行 chrome.pageAction.show(tabId); 一次.
您需要不断检查后台选项卡的更改,因为页面操作在清单中没有matches/exclude_matches 设置,就像内容脚本那样(可惜).所以你必须检查自己并响应变化.
如果您希望它适用于特定站点,只需将其更改为...

So even if your tabid was valid it would dissapear pretty quick as your only running chrome.pageAction.show(tabId); once when the background page first gets run.
You need to check for changes to tabs in the background constantly because pageactions dont have matches/exclude_matches settings in the manifest like content scripts do (pity). So you have to check yourself and respond to changes.
If you want it to work for a specific site just change it to something like...

// 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);

这篇关于如何为特定页面显示 page_action?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆