2
votes

This is probably me doing something dumb, but I can't figure out what. I'm trying to get a sample Google Chrome page extension demo working. The source for the demo can be found here: http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/pageAction/

It's a simple program with 2 code files - the manifest and a background.js file. Here's the background.js:

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

And here's the manifest.json file:

{
  "name": "Page action by URL",
  "version": "1.0",
  "description": "Shows a page action for urls which have the letter 'g' in them.",
  "background": { "scripts": ["background.js"] },
  "page_action" :
  {
    "default_icon" : "icon-19.png",
    "default_title" : "There's a 'G' in this URL!"
  },
  "permissions" : [
    "tabs"
  ],
  "icons" : {
    "48" : "icon-48.png",
    "128" : "icon-128.png"
  },
  "manifest_version": 2
}

As written, this code doesn't work for me. Chrome loads the extension just fine but when i navigate to a page with a g in the URL, no icon shows up.

I found this answer: How do I make page_action appear for specific pages?

So I tried the following-

  • created a background.html file :

    <html><head><script> ... cut and pasted contents of background.js above </script></head></html>
    
  • changed manifest.json to have manifest_version: 1 instead of manifest_version: 2

  • changed the background property in manifest.json to be background_page: "background.html"

This worked perfectly.

But I absolutely am not able to get this thing running with a background.js file and manifest_version = 2.

So I'm wondering how to get things working with manifest_version = 2 and a background.js file. Also, is this even important - i.e. is everyone just using manifest_version = 1 and not worrying about this kind of stuff?

2

2 Answers

3
votes

What version of Chrome are you using? as Im on 19 and its working fine. Those new background options came in at 18 and as such that sample really should use the minimum_chrome_version key to avoid this problem. Also would be good if the non trunk docs didnt point to samples that dont work in the version of Chrome those docs are for.

http://code.google.com/chrome/extensions/trunk/manifestVersion.html
This page says....

Setting manifest_version 2 in Chrome 17 or lower is not recommended. If your extension needs to work in older versions of Chrome, stick with version 1 for the moment. We'll give you ample warning before version 1 stops working.

Bit sucky tho aye? Why cant we have background and background_page to fall back on if their Chrome version is pre manifest version 2?...I tried that and it complained that background_page doesnt work with manifest version 2 and didnt install it. So you might want to put off using manifest version 2 for a while yet.
Although it would prolly be a good idea to read those docs for version 2 and start using some of the practices its going to enforce (such as no inline scripts) getting ready for it.

1
votes

Another thing that might cause this problem:

I had this issue because I forgot to include the declarativeContent permission in my manifest as per the sample.

Hope this helps someone...