Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context menus in Chrome extensions

I've searched and searched and searched for a solution to this but every source I come across seems to assume I already have profound knowledge of Chrome extensions, even Google's help pages

I know the very basics of Chrome extensions and I made one with some basic content scripts. However, now I'm looking to make one that involves context menus.

Let's say when you highlight words and right-click them, you see the option Search '<highlighted words>' on Google and when clicked, it opens http://www.google.com/search?q=<highlighted words> in a new tab. I know this exists in Chrome and I'm sure there have been a billion extensions replicating it, but this is only an example for me to build off of.

How can I do this?

like image 929
UserIsCorrupt Avatar asked Dec 09 '12 00:12

UserIsCorrupt


People also ask

Where do I find context menu?

In Microsoft Windows, pressing the Application key or Shift+F10 opens a context menu for the region that has focus.

How do I display context menu?

The context menu (right-hand mouse button or SHIFT+F10 ) allows you to display a context-sensitive menu. The context is defined by the position of the mouse pointer when the user requests the menu. A context menu allows the user to choose functions that are relevant to the current context.


2 Answers

Script should look like this:

function getword(info,tab) {   console.log("Word " + info.selectionText + " was clicked.");   chrome.tabs.create({       url: "http://www.google.com/search?q=" + info.selectionText   }); } chrome.contextMenus.create({   title: "Search: %s",    contexts:["selection"],    onclick: getword }); 

And manifest.json:

{     "name": "App name",     "version": "1.0",     "manifest_version": 2,     "description": "Your description",     "permissions": [       "contextMenus"      ],     "background": {        "scripts": ["script.js"]     } } 

Here you have how to load extension: http://developer.chrome.com/extensions/getstarted.html

like image 67
Bartłomiej Szałach Avatar answered Sep 18 '22 14:09

Bartłomiej Szałach


The answer from Bartlomiej Szalach is too old. It will not work on Chrome Version 80.0.3987.163 (April 2020).

According to the documentation,

onclick: A function that is called back when the menu item is clicked. Event pages cannot use this; instead, they should register a listener for contextMenus.onClicked.

The background.js should be modified as follows:

const CONTEXT_MENU_ID = "MY_CONTEXT_MENU";  function getword(info,tab) {    if (info.menuItemId !== CONTEXT_MENU_ID) {      return;    }    console.log("Word " + info.selectionText + " was clicked.");    chrome.tabs.create({        url: "http://www.google.com/search?q=" + info.selectionText    });  }  chrome.contextMenus.create({    title: "Search: %s",     contexts:["selection"],     id: CONTEXT_MENU_ID  });  chrome.contextMenus.onClicked.addListener(getword)
like image 24
anhquan Avatar answered Sep 17 '22 14:09

anhquan