0

Possible Duplicate:
Port error while changing chrome extension from manifest v1 to v2

I am trying to develop an addon for a website of mine. My users will need to be able to right-click any hyperlink on any webpage and then click a link in the Chrome context menu that will take them to my website to perform an action.

My addon is done, but everytime I try to test it, the link does not appear in the Chrome context menu when a hyperlink is reght-clicked.

Here is my files:

manifest.jason

{
"manifest_version": 2,
"background_page": "background.html",
"description": "Decrypt Short URLs.",
"icons": {
  "128": "icon-128.png",
  "16": "icon-16.png",
  "48": "icon-48.png"
 },
"minimum_chrome_version": "8.0.0.0",
"name": "xxxx.xxx",
"permissions": [ "http://*/*", "https://*/*", "tabs", "contextMenus" ],
"version": "1.0"
}    

background.html

<!DOCTYPE html>

<html>
<head>

</head>
<body>
<script>


  function handleClick() {
    return function(info, tab) {

      var url = 'http://xxx.xxx/api.php?url=' + info.linkUrl +    '&source=chromeextension'

      // Create a new tab to the results page
      chrome.tabs.create({ url: url, selected:true  });
    };
  };

  chrome.contextMenus.create({
    "title" : "Decrypt this Link",
    "type" : "normal",
    "contexts" : ["link"],
    "onclick" : handleClick()
  });
 </script>
</body>

I will appreciate any help.

Community
  • 1
  • 1
Rian
  • 1
  • 1
  • You should extract the scripts (`function handleClick()`) into a separate file when using manifest v. 2. – chaohuang Sep 02 '12 at 19:23
  • also try to use `function handleClick(info, tab)` instead of `function handleClick() {return function(info, tab){} }` – chaohuang Sep 02 '12 at 19:32
  • The parameter `selected:true` is not found in the [document](http://developer.chrome.com/extensions/tabs.html#method-create) for `chrome.tabs.create` – chaohuang Sep 02 '12 at 19:37
  • @chaohuang `handleClick()` is a factory function. It's correctly implemented. The remark about `selected` is incorrect. `selected` *is valid*, but superseded (/deprecated) by `active` [since Chrome 16](http://developer.chrome.com/extensions/whats_new.html#16). – Rob W Sep 02 '12 at 19:51
  • Hi Rob W. Thank you very much! You are right. It was indeed a port error when changing to v2. I followed your suggestions on moving the script to an external JS file and the addon is working perfectly now! Thank you! – Rian Sep 03 '12 at 05:19

1 Answers1

1

I guess the problem is in your manifest file. You use manifest version 2, but your background page is declared as in manifest version 1.

You should try to change this part of manifest.json:

"background_page": "background.html",

on this one:

  "background": {
        "scripts": ["background.js"]
    },

and put all of your background code to background.js

More info you can read here: background_pages

P.S. Sorry, Mr. Rob W already points on this problem in the comments to main question.

Community
  • 1
  • 1
Ceridan
  • 2,376
  • 3
  • 23
  • 32