Tampermonkey - 右键菜单

zed*_*dex 8 javascript tampermonkey

使用Tampermonkey可以在Chrome中创建右键菜单选项吗?

我发现GM_registerMenuCommand但它似乎没有在右键菜单中显示任何新项目.

另一个问题是我GM_openInTab在测试脚本中使用但似乎由于某种原因无限循环.它应该只在单击菜单后触发,为什么会发生这种情况?

另外我想知道有没有办法以更高级的方式使用自定义右键单击图标等?

有一个用于Firefox的GM脚本适用于菜单,但在Chrome中似乎没有任何显示,所以有一种方法可以让它工作.

// ==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");

})();
Run Code Online (Sandbox Code Playgroud)

小智 8

而不是GM_registerMenuCommand("hello", test(), "h")你应该有GM_registerMenuCommand("hello", test, "h")

第一个版本test立即调用该函数,然后将其结果传递给GM_registerMenuCommand函数。第二个传递函数本身而不是它的结果。


bra*_*ilo 7

根据wOxxOm注释,可以使用@run-at context-menu

范例

// ==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");
})();
Run Code Online (Sandbox Code Playgroud)

结果:(效果很好:)

Userscript shown at context menu