1

I'm trying to update a simple extension to Manifest V3. I must be doing something wrong, because simple tags such as permissions don't work right...I know Chrome is reading the stanza, because I get an error if I put a bogus permission in, but the about://extensions doesn't show permission contextMenus for my extension, even though it's there. And chrome.contextMenus is undefined, so I can't create my menu.

Is Manifest V3 still half-baked and not-ready-for-primetime, or am I doing something stupid that is silently ignored?

{
    "manifest_version": 3,
    "name": "TESS+",
    "version": "0.043",

    "description": "TESS+ -- My browser extension",

    "icons": {
        "48": "TESS+Icon.png"
    },

    "content_scripts": [
        {
            "matches": ["*://tmsearch.uspto.gov/bin/showfield*" ],
            "js": ["jquery-3.6.0.min.js", "tess+.js", "FH.js", "DateTime.js"]
        }
    ],

    "background": {
        "service_worker": "tess-bg.js"
    },

    "web_accessible_resources": [
        {
            "resources": [ "TESS+Button.png" ],
            "matches": [ "https://tsdr.uspto.gov/*" ]
        }
    ],

    "permissions": [
        "contextMenus"
    ],

    "host_permissions": [
        "http://tsdr.uspto.gov/",
        "https://tsdr.uspto.gov/"
    ]

}

2 Answers 2

8

As @wOxxOm guessed, I was trying to create my context menu from the foreground content-script, not from the background service worker.

For future reference, here's a simple contextMenu example using Manifest V3:

manifest.js:

{
    "manifest_version": 3,
    "name": "ManifestV3Test",
    "version": "0",

    "background": {
        "service_worker": "bg.js"
    },

    "content_scripts": [
        {
            "matches": [ "<all_urls>" ],
            "js": [ "fg.js" ]
        }
    ],

    "permissions": [
        "contextMenus"
    ]
}

bg.js:

/*
 * bg.js -- a ManifestV3 service_worker that installs a context menu
 *          plus minimal framework for messaging between here and
 *          a content script.
 */
chrome.runtime.onInstalled.addListener( function( ) {
    chrome.contextMenus.create( {
        id: 'a unique id',
        title: 'My Context Menu',
        contexts: [ 'all' ]
    } );
} );

chrome.contextMenus.onClicked.addListener( (info,tabs) => {
    console.log( 'context menu clicked' );
    console.log( info );
    console.log( tabs );
    chrome.tabs.sendMessage( tabs.id, 'request-object',
                             (rsp)=> { console.log( "content script replies:" );
                                       console.log( rsp ); } );
} );

fg.js:

/*
 * fg.js -- a content script for a minimal ManifestV3 test extension.
 */

console.log( "Activating content script...try context menu!" );

chrome.runtime.onMessage.addListener( (req,snd,rsp) => {
    console.log( snd.tab ? "another content script says:" : "the extension says:" );
    console.log( req );
    rsp( 'a-response-object' );
} );

This works as of 11-Apr-2021 on Chrome 89.0.4389.114 but Firefox-87.0 (MacOS) rejects it as "unsupported manifest version."

2
  • onClicked listener must be added outside of onInstalled, more info.
    – woxxom
    Commented Apr 11, 2021 at 15:08
  • Oof. Thanks, you probably saved me another hour thrashing around to figure out why things work the first time, but not the next time.
    – Dave M.
    Commented Apr 11, 2021 at 22:06
0

While it's true that MV3 is still semi-broken, but the permission thing is WAI (working as intended).

Chrome's list of permissions in chrome://extensions page shows only those entries that produce a warning during installation of an extension. See the list in the documentation. The contextMenus permission doesn't produce such a warning so it's not shown in the list.

Such behavior may seem stupid to a developer or a knowledgeable user, but Chrome has always been simplifying its UI/UX to accommodate the majority of people.

3
  • Well, that (maybe?) explains why "contextMenus" doesn't appear in Permissions (but another random one I tried, "geolocation", does) but still... why is chrome.contextMenus undefined in my first JS function? chrome is defined, chrome.contextMenus is not.
    – Dave M.
    Commented Apr 11, 2021 at 3:52
  • You didn't show how and where you tried to use chrome.contextMenus so I can only guess you did it in the content script (this is impossible both in MV2 and MV3) or you didn't reload the extension on chrome://extensions page after editing the code. Note that files/functions injected via executeScript are also content scripts.
    – woxxom
    Commented Apr 11, 2021 at 4:52
  • Ah, you're right. I thought I'd gotten a context menu working in a content script before, but it must have been from the background. So now I have to figure out the new service_worker stuff. Thanks!
    – Dave M.
    Commented Apr 11, 2021 at 6:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.