Hi Anthony

May I suggest that an existing menu is extended instead of (potentially) 
replaced by GM_registerMenuCommand() in greasemonkey4-polyfill.js? There 
could be an existing menu created by the page or another (user)script 
having an id different from 'gm-registered-menu'! An updated 
GM_registerMenuCommand() extending such menus could look something like 
below...

Below code will also create a sub-menu for each userscript [ 
GMC.getScriptName() is scriptname and GMC.getScriptIdentifier() is an 
unique identifier based on script namespace and name ]. That is how I would 
prefer it, but of course opinions on that part probably varies :-)
  
function GM_registerMenuCommand(caption, commandFunc, accessKey) {
  if (!document.body) {
    console.error('GM_registerMenuCommand got no body.');
    return;
  }
  let menuContainer = null;
  if (document.body.getAttribute('contextmenu')) {
      // If existing context menu on body, don't replace but use it...
      menuContainer = 
document.querySelector('menu#'+document.body.getAttribute('contextmenu'));
  }
  if (!menuContainer) {
      // if not already exist, create the "top menu container"
      menuContainer = document.createElement("menu");
      menuContainer.setAttribute('type', 'context');
      menuContainer.id = 'gm-registered-menu';
      document.body.appendChild(menuContainer);
  }
  document.body.setAttribute('contextmenu', menuContainer.id);
  let scriptMenu = 
document.querySelector('menu#menu'+GMC.getScriptIdentifier());
  if (!scriptMenu) {
      // if not already exist, create "sub-menu" for current userscript
      scriptMenu = document.createElement("menu");
      scriptMenu.setAttribute('label', GMC.getScriptName());
      scriptMenu.id = 'menu'+GMC.getScriptIdentifier();
      menuContainer.appendChild(scriptMenu);
  }
  // create menu item
  let menuItem = document.createElement("menuitem");
  menuItem.setAttribute('label', caption);
  scriptMenu.appendChild(menuItem);
  menuItem.addEventListener('click',commandFunc, false);
}

/Stig

-- 
You received this message because you are subscribed to the Google Groups 
"greasemonkey-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/greasemonkey-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to