Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chrome.contextMenus.create doesn't work

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.

like image 825
Slot Avatar asked Aug 06 '13 13:08

Slot


1 Answers

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"]
},
like image 140
Chris McFarland Avatar answered Nov 04 '22 13:11

Chris McFarland