17

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!

abraham
  • 46,583
  • 10
  • 100
  • 152
Treferwynd
  • 173
  • 1
  • 1
  • 5

2 Answers2

30

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);
oxfn
  • 6,590
  • 2
  • 26
  • 34
PAEz
  • 8,366
  • 2
  • 34
  • 27
  • Got it, thank you very much. I read that passage you quoted, but apparently I didn't understand it that well! – Treferwynd Feb 12 '12 at 09:59
  • Mmh, you can answer to this question too: Assuming bg.js is the file which contains (and contains **only**) the code I quoted (or the one you quoted). Why if I write this: in the background.html it does work, but if I try to write the code directly in background.html it doesn't? – Treferwynd Feb 14 '12 at 00:19
  • 4
    My guess is that you have `"manifest_version": 2` in your manifest file, which will disallow inline ` in a background.html, or remove the `"manifest_version": 2` in your manifest, or change the background part of the manifest too `"background": { "scripts": ["bg.js"] }`...didnt know you could do the last one till just now. – PAEz Feb 14 '12 at 07:07
  • And again you're damn right. Thank you very much! I'm glad you found out something new while answering to me, so it wasn't a complete waste of time for you! :D – Treferwynd Feb 14 '12 at 12:46
3

For those looking for a way to handle subdomains, if you have a site with a subdomain such as blog.specificsite.com, or need to use wildcards, you can also use regex in this format

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

to match the substring within the URL. It also helps with computation to do a null/undefined check to avoid additional exception handling.

Chris - Jr
  • 398
  • 4
  • 11