Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Forked from Noitidart/bootstrap.js
Last active August 29, 2015 13:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noitidart/8777224 to your computer and use it in GitHub Desktop.
Save Noitidart/8777224 to your computer and use it in GitHub Desktop.
This bootstrap addon shows how to add a menu item to the content context menu. FORK: This fork is different in that it shows how to make that menu item hidden or shown based on if the user opens the popup from a link that contains 'mozilla' in its href.
const {interfaces: Ci,utils: Cu} = Components;
Cu.import('resource://gre/modules/Services.jsm');
/*start - windowlistener*/
var windowListener = {
//DO NOT EDIT HERE
onOpenWindow: function (aXULWindow) {
// Wait for the window to finish loading
let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
aDOMWindow.addEventListener("load", function () {
aDOMWindow.removeEventListener("load", arguments.callee, false);
windowListener.loadIntoWindow(aDOMWindow, aXULWindow);
}, false);
},
onCloseWindow: function (aXULWindow) {},
onWindowTitleChange: function (aXULWindow, aNewTitle) {},
register: function () {
// Load into any existing windows
let XULWindows = Services.wm.getXULWindowEnumerator(null);
while (XULWindows.hasMoreElements()) {
let aXULWindow = XULWindows.getNext();
let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
windowListener.loadIntoWindow(aDOMWindow, aXULWindow);
}
// Listen to new windows
Services.wm.addListener(windowListener);
},
unregister: function () {
// Unload from any existing windows
let XULWindows = Services.wm.getXULWindowEnumerator(null);
while (XULWindows.hasMoreElements()) {
let aXULWindow = XULWindows.getNext();
let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
windowListener.unloadFromWindow(aDOMWindow, aXULWindow);
}
//Stop listening so future added windows dont get this attached
Services.wm.removeListener(windowListener);
},
//END - DO NOT EDIT HERE
loadIntoWindow: function (aDOMWindow, aXULWindow) {
if (!aDOMWindow) {
return;
}
var contentAreaContextMenu = aDOMWindow.document.getElementById('contentAreaContextMenu');
if (contentAreaContextMenu) {
var menuItem = aDOMWindow.document.createElement('menuitem');
menuItem.setAttribute('label', 'You clicked on a link that contains mozilla in the href');
menuItem.setAttribute('oncommand', 'alert("hi")');
menuItem.setAttribute('id', 'myMenuItem');
menuItem.setAttribute('hidden', 'true');
contentAreaContextMenu.appendChild(menuItem);
contentAreaContextMenu.addEventListener('popupshowing',decideToShowMyMenuItem,false);
}
},
unloadFromWindow: function (aDOMWindow, aXULWindow) {
if (!aDOMWindow) {
return;
}
var contentAreaContextMenu = aDOMWindow.document.getElementById('contentAreaContextMenu');
if (contentAreaContextMenu) {
var myMenuItem = aDOMWindow.document.getElementById('myMenuItem');
contentAreaContextMenu.removeChild(myMenuItem);
contentAreaContextMenu.removeEventListener('popupshowing',decideToShowMyMenuItem,false);
}
}
};
/*end - windowlistener*/
function decideToShowMyMenuItem(event) {
var window = event.target.ownerDocument.defaultView;
var document = window.document;
var myMenuItem = document.getElementById('myMenuItem');
if (myMenuItem) {
var popupNode = document.popupNode; //this is the target clicked on but its prefered to use triggerNode; //note: very different from event.target //see here: https://developer.mozilla.org/en-US/docs/Web/API/document.popupNode?redirectlocale=en-US&redirectslug=DOM%2Fdocument.popupNode //event.target is the menupopup itself
if (popupNode.parentNode.nodeName == 'A') {
//sometimes people put elements between the a tags like: <a href="blah"><b>bolded link</b></a>
//so we check to see if parentNode is link
popupNode = popupNode.parentNode;
}
//cDump(popupNode);
var href = popupNode.href; //dont do popupNode.getAttribute() because if href is relative path, it wont have base name in it
Cu.reportError('href = ' + href)
if (href) { //test if clicked on an element that has href attribute, or you can do popupNode.nodeName == 'A' to test if clicked on link
if (href.toLowerCase().indexOf('mozilla') > -1) {
myMenuItem.hidden = false; //show the menu item
} else {
myMenuItem.hidden = true; //keep the menu item hidden because
}
}
}
}
function startup(aData, aReason) {
windowListener.register();
}
function shutdown(aData, aReason) {
if (aReason == APP_SHUTDOWN) return;
windowListener.unregister();
}
function install() {}
function uninstall() {}
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#">
<Description about="urn:mozilla:install-manifest">
<em:id>demo-contextmenu-menuitem-shown-when-link-with-mozilla@jetpack</em:id>
<em:version>initial</em:version>
<em:type>2</em:type>
<em:bootstrap>true</em:bootstrap>
<em:unpack>false</em:unpack>
<!-- Firefox -->
<em:targetApplication>
<Description>
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
<em:minVersion>7.0</em:minVersion>
<em:maxVersion>10.0a1</em:maxVersion>
</Description>
</em:targetApplication>
<!-- Front End MetaData -->
<em:name>demo contextmenu menuitem shown when link with mozilla</em:name>
<em:description>This bootstrap addon shows how to add a menu item to the content context menu. FORK: This fork is different in that it shows how to make that menu item hidden or shown based on if the user opens the popup from a link that contains 'mozilla' in its href.</em:description>
<em:creator>Noitidart</em:creator>
<em:iconURL/>
<em:icon64URL/>
<em:optionsType>2</em:optionsType>
</Description>
</RDF>
@Noitidart
Copy link
Author

This is a copy paste bootstrap addon that shows how to add menu items to the content area context menu and only show the menu item if the user opened popup menu from a link that contains the word mozilla.

To change this behavior edit the decideToShowMyMenuItem function.

Edit in loadIntoWindow and unloadFromWindow to add more menu items.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment