Greasy Fork is available in English.

Discussions » Development

How do I add toggle switches with/to the GM_registerMenuCommand?

§
Posted: 13-11-2023

I've seen and used userscripts before where the options were toggleable instead of having the static gear icon. I've searched, but I can't seem to find any resources on how this is done. Does anyone have any pointers or know of any documentation on this? Thanks for your time.

- G e m e d e t A d e p t

§
Posted: 13-11-2023

any documentation on this

https://www.tampermonkey.net/documentation.php?locale=en

// ==UserScript==
// @name          Toggleable menu command
// @description   Toggleable GM_registerMenuCommand example
// @version       0.1
// @match         https://www.example.com/*
// @grant         GM_registerMenuCommand
// @grant         GM_unregisterMenuCommand
// ==/UserScript==

let isToggleOn = false;
let menuCommandId = null;

function toggleFeature() {
  isToggleOn = !isToggleOn;
  console.log(`Feature is now ${isToggleOn ? 'enabled' : 'disabled'}`);
  updateMenuCommand();
}

function updateMenuCommand() {
  if (menuCommandId !== null) GM_unregisterMenuCommand(menuCommandId);

  menuCommandId = GM_registerMenuCommand(`Toggle feature ${isToggleOn ? 'off' : 'on'}`, toggleFeature, 't');
}

updateMenuCommand();
§
Posted: 13-11-2023

Thank you, kindly.

Post reply

Sign in to post a reply.