2017-03-15 181 views
2

隨着Tampermonkey有什麼方法可以在Chrome中創建一個右鍵菜單選項?Tampermonkey - 右鍵菜單

我發現GM_registerMenuCommand但它似乎並沒有表現出在右鍵菜單中的任何新項目。

另一個問題是我用GM_openInTab在測試腳本,但它似乎無限循環出於某種原因。它應該只在菜單被點擊後觸發,爲什麼會發生這種情況?

此外,我想知道是否有辦法自定義右鍵點擊圖標等,以做到這一點更先進的方式?

有一個通用腳本的Firefox,對於菜單,但在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"); 

})(); 
+1

使用[@ run-at context-menu](https://forum.tampermonkey.net/viewtopic.php?t=1170) – wOxxOm

+0

@wOxxOm你有一個簡單的代碼示例說明這將如何工作?該線程似乎是請求某個功能的人。 – zeddex

+0

我沒有自己嘗試,但我可以告訴你一件事:該功能已實施。 – wOxxOm

回答

1

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"); 
})(); 

結果:(工作得很好:)

Userscript shown at context menu

+0

不錯的一個;我不知道「@ run-at context-menu」。 (愚蠢地使用Greasemonkey作爲我的主要引擎(前GM 4)) –

+0

是的,我也是,從評論中學習並去檢查。非常方便單行動:) – brasofilo

1

而不是GM_registerMenuCommand("hello", test(), "h")你應該有GM_registerMenuCommand("hello", test, "h")

第一個版本立即調用test函數,然後將其結果傳遞給GM_registerMenuCommand函數。第二個傳遞函數本身而不是結果。