如何让页面动作出现在特定页面上?

问题描述:

我玩弄了一些镀铬的扩展,我发现这个例子:http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/pageAction/pageaction_by_url/如何让页面动作出现在特定页面上?

一切工作正常,但我想创建自己的扩展,我想看看在特定网站上的page_action图标,而不是那些以'克'在他们的网站。 所以,我想简单地从该改剧本:

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

+0

你给它一个有效的'tabId'? – abraham 2012-02-12 00:19:24

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

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

因此,即使您的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); 
+0

明白了,非常感谢。我读过你引用的那段话,但显然我并没有明白这一点! – Treferwynd 2012-02-12 09:59:48

+0

Mmh的,你能回答这个问题太: 假设bg.js是包含文件(包含** **只),我引用的代码(或你所引用的一个)。为什么,如果我这样写:在它的工作的background.html,但如果我尝试写的代码直接在background.html它不? – Treferwynd 2012-02-14 00:19:56

+4

我的猜测是你的清单文件中有''manifest_version“:2',它将不允许嵌入',要么在清单中删除'“manifest_version”:2',或者将清单的背景部分改为''background“: {“scripts”:[“bg.js”]} ...... ......直到现在才知道你可以做最后一个。 – PAEz 2012-02-14 07:07:13

对于那些正在寻找一种方式来处理子域,如果你有一个子域,例如博客网站.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内子。它还有助于计算执行空/未定义检查以避免额外的异常处理。