Paul Colpitts
Paul Colpitts

Reputation: 51

Right-click context menu for Chrome

I'm trying to create a right-click context menu for Chrome. I want to be able to highlight text, right click, then click to open a new tab using a URL determined by the highlighted text.

I have a manifest (though I'm not sure I've done the background part right):

  {
    "manifest_version": 2,
    "name": "ClickMenu",
    "version": "1.0",

    "description": "A right-click context menu",
    "icons": {
        "16": "icons/icon-16.png",
        "48": "icons/icon-48.png",
        "128": "icons/icon-128.png"
    },

    "permissions": [ "contextMenus" ],

    "background": {
        "scripts": ["contextMenu.js"],
        "persistent": false
        }
    }

And a contextMenu.js:

    function MenuContext(contextInfo, tab) {
    chrome.tabs.create({
    'url' : 'https://myurl.com/' + contextInfo.selectionText + '/launch'
    });
}

    var contextId = chrome.contextMenus.create({
        'title' : 'Launch this',
        'contexts' : [
          'selection'
        ],
        'onclick' : MenuContext
    });

It's not working. I can load the extension, but there's no context menu or functionality.

Can you see what I've done wrong?

Upvotes: 1

Views: 2518

Answers (1)

Elad
Elad

Reputation: 1144

You create an event page (bacause in your manifest you define "persistent": false).

In event page you to do three changes when you create context menu item.

  1. Pass a string id parameter.
  2. Use the chrome.contextMenus.onClicked callback instead of an onclick parameter.
  3. Create them in chrome.runtime.onInstalled event.

For more information - Event pages best practices.

(By the way - in event page don't save data in variables).

(I'm sorry about my english...)

Upvotes: 1

Related Questions