7

I try to create a simple chrome extension to add an option in the context menu of chrome.

Here is my manifest.json

{
   "manifest_version": 2,
   "background": "background.html",
   "description": "Add a context menu item to search for selected text at Google Maps.",
   "icons": {
      "16": "icon16.png"
   },
   "name": "Google Maps Right Click",
   "permissions": [ "contextMenus", "tabs" ],
   "version": "1.0"
}

and here my background.html :

<script>

function searchgooglemaps(info)
{
 var searchstring = info.selectionText;
 chrome.tabs.create({url: "http://maps.google.com/maps?q=" + searchstring})
}

chrome.contextMenus.create({title: "Search Google Maps", contexts:["selection"], onclick: searchgooglemaps});

</script>

But when I load the extension and I right click on a selection, the "Search Google Maps" button doesn't appear, and I don't understand why...

Thank you in advance for your help.

Slot
  • 1,006
  • 2
  • 13
  • 27
  • Is your background page being loaded? [The docs](http://developer.chrome.com/extensions/background_pages.html) indicate that the correct manifest format for background pages is `"background": { "page" : "background.html" }`. Note that you may not even need a background page, and could use a background script instead. – apsillers Aug 06 '13 at 14:01
  • Effectively, the background page wasn't active, with the new format `"background": { "page" : "background.html" }`, the background page is active, but I still don't see my new context menu element anymore :'( – Slot Aug 06 '13 at 14:16
  • 1
    possible duplicate of [Port error while changing chrome extension from manifest v1 to v2](http://stackoverflow.com/questions/11913575/port-error-while-changing-chrome-extension-from-manifest-v1-to-v2) – Rob W Aug 06 '13 at 14:23
  • 1
    If you [look at the console for your background page](http://stackoverflow.com/questions/10257301/where-to-read-console-messages-from-background-js-in-a-chrome-extension), you'll see the error: `Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".` Searching for that error would lead you to http://stackoverflow.com/questions/17601615/the-chrome-extension-popup-is-not-working-click-events-are-not-handled/17612988#17612988 – apsillers Aug 06 '13 at 14:23

1 Answers1

4

Inline JavaScript in HTML files generally doesn't work anymore; put your JavaScript into its own file instead.

You can also have Chrome create a background page for you, saving space.

So, to make your extension work:

1) Create a file (eg background.js) and put your JavaScript in it:

function searchgooglemaps(info)
{
 var searchstring = info.selectionText;
 chrome.tabs.create({url: "http://maps.google.com/maps?q=" + searchstring})
}

chrome.contextMenus.create({title: "Search Google Maps", contexts:["selection"], onclick: searchgooglemaps});

2) In your manifest, replace your background line with this:

"background": {
    "scripts": ["background.js"]
},
Chris McFarland
  • 6,059
  • 5
  • 43
  • 63