Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tampermonkey - Right click menu

With Tampermonkey is there any way to create a right click menu option in Chrome?

I found GM_registerMenuCommand but it does not seem to show any new items in the right click menu.

Another problem is I use GM_openInTab in the test script but it seems to loop infinitely for some reason. It should only trigger after the menu is clicked, why would this happen?

Also I am wondering is there a way to do this in a more advanced way with custom right click icons etc?

There was a GM script for Firefox that worked for menus but in Chrome nothing seems to show so it would be good to have a way to have this working.

// ==UserScript==
// @name            Context Menu
// @namespace       http://tampermonkey.net/
// @description     Test
// @version         0.1
// @author          author
// @include         *
// @exclude         file://*
// @grant           GM_openInTab
// @grant           GM_registerMenuCommand
// ==/UserScript==]


(function() {
    'use strict';

function test() {
    GM_openInTab("https://website.net");
}

GM_registerMenuCommand("hello", test(), "h");

})();
like image 572
zeddex Avatar asked Mar 15 '17 03:03

zeddex


People also ask

How do I create a right click menu?

To make this HTML menu visible when right-clicking in the browser, we need to listen for contextmenu events. We can bind the event listener directly on the document or we can limit the area that a user can right-click to see the custom context menu. In our example, the area for right-click is the entire body element.

How do I change the right click menu in Chrome?

Right-click your Google Chrome shortcut and click Properties. Note: You have to put a space between chrome.exe before the code. It's two hyphens, and not a long em dash (–). Click OK, launch Chrome, and you'll see the right-click context menu is back to normal.


2 Answers

As per wOxxOm comment, it is possible using @run-at context-menu.

Example:

// ==UserScript==
// @name            Go to Website.Net
// @namespace       http://tampermonkey.net/
// @description     Context menu to execute UserScript
// @version         0.1
// @author          author
// @include         *
// @grant           GM_openInTab
// @run-at          context-menu
// ==/UserScript==]


(function() {
    'use strict';
    GM_openInTab("https://website.net");
})();

Result: (works nicely :)

Userscript shown at context menu

like image 63
brasofilo Avatar answered Sep 21 '22 18:09

brasofilo


Instead of GM_registerMenuCommand("hello", test(), "h") you should have GM_registerMenuCommand("hello", test, "h")

The first version calls the test function immediately, and then passes its result to GM_registerMenuCommand function. The second passes the function itself instead of its result.

like image 30
user9069302 Avatar answered Sep 17 '22 18:09

user9069302