我如何让页面动作出现在特定页面上? [英] How do I make page_action appear for specific pages?

查看:184
本文介绍了我如何让页面动作出现在特定页面上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



一切正常,但我想创建自己的扩展,并且希望在特定网站上看到page_action图标,而不是在他们的url中使用'g' 。
所以我试着简单地改变脚本:

  // Copyright(c)2011 The Chromium Authors。版权所有。 
//使用此源代码由BSD样式的许可证管理,该许可证可以是LICENSE文件中的
//。

//当选项卡的URL更改时调用。
函数checkForValidUrl(tabId,changeInfo,tab){
//如果在标签的URL中找到字母'g'...
if(tab.url.indexOf('g' )> -1){
// ...显示页面动作。
chrome.pageAction.show(tabId);
}
};

//监听任何选项卡的URL的任何更改。
chrome.tabs.onUpdated.addListener(checkForValidUrl);

进入:

  chrome.pageAction.show(tabId); 

但现在它不起作用...
我不明白。很明显,我可以使用解决方法,但这不是重点......首先,我必须创建一个后台页面来执行此操作吗?我想是的,但我不明白为什么,为什么.show方法不能单独工作?
我试图搜索谷歌文档和东西,但我找不到任何有用的东西,我不是专家,这是我第一个下午在谷歌扩展上花费的,但是我应该怎么知道铬。 page.show(tabId)必须放在后台页面中,如果它没有被写入任何地方?没有意图批评,但你们是怎么找到的?所有的chrome方法都必须放在后台页面中?
那么,肯定有更多的问题,那么它的合法性。希望你至少能给我一个答案!

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

...说...


默认情况下,隐藏页面操作。当您显示它时,您可以指定图标应显示的
选项卡。该图标保持可见状态,直到
标签页关闭或开始显示不同的网址(例如,因为
用户点击了链接)。


所以,即使你的tabid有效,它也会很快消失,因为你只能运行 chrome.pageAction.show(tabId); 页面首先得到运行。

您需要持续检查背景中的选项卡更改,因为pageactions没有像内容脚本那样在清单中具有matches / exclude_matches设置(可惜)。所以你必须检查你自己并对变化做出反应。

如果你想让它适用于特定的网站,只需将它改成类似...

  //版权所有(c)2011 Chromium作者。版权所有。 
//使用此源代码由BSD样式的许可证管理,该许可证可以是LICENSE文件中的
//。

//当选项卡的URL更改时调用。
函数checkForValidUrl(tabId,changeInfo,tab){
//如果标签url以http://specificsite.com开头...
if(tab.url.indexOf( 'http://specificsite.com')== 0){
// ...显示页面动作。
chrome.pageAction.show(tabId);
}
};

//监听任何选项卡的URL的任何更改。
chrome.tabs.onUpdated.addListener(checkForValidUrl);


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/

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

Into this:

chrome.pageAction.show(tabId);

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
...says...

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).

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

这篇关于我如何让页面动作出现在特定页面上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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