Lukich
Lukich

Reputation: 716

passing clicked element to the context menu action in Firefox

I am trying to write a simple extension, which would take a link that you right-clicked on, reformat the url and launch it in the external application. I have all the pieces working with one exception.

I am using "popupshowing" listener to deduce whether an element that was right-clicked is a link or not and show or hide the "Launch" command in the context menu respectively. My overlay code looks like this:

<menupopup id="contentAreaContextMenu">
  <menuitem id="LinkOpener" label="LAUNCH" oncommand="LaunchIt(document.popupNode);" />
</menupopup>

Works like a charm, but according to the docs, document.popupNode is being phased out and I should use menupopup's triggerNode instead. However, I can't simply pass it into the inline "oncommand" handler any more.

What's the best way to do that? The only one I can think of right now is go to the function that handles the "popupshowing" event, remove the old "command" listener with previous element reference and add a new one with a new reference, but it feels there might be a better way to do this. Any help would be greatly appreciated. Thanks! Luka

Upvotes: 2

Views: 596

Answers (3)

DSHCS
DSHCS

Reputation: 1

I was looking for how to get the DOM Element that a firefox (35.0) context menu extension menu item was used on and the above technique worked for me. I passed

<menuitem oncommand="myFunc(this.parentNode.triggerNode);">

and accessed the DOM Element with

function(triggerNode) { window.alert("triggerNode.tagName=" + triggerNode.tagName)

Upvotes: 0

Lukich
Lukich

Reputation: 716

Solved: since menuitem is a child of menupopup, I can do this:

<menuitem oncommand="myFunc(this.parentNode.triggerNode);">

Upvotes: 1

Noitidart
Noitidart

Reputation: 37228

your function LaunchIt pass the event parameter and from this get event.relatedTarget or something I forget but event.SOMEtarget is the same as document.popupNode. To explore it, in your launchIt function put console.log(event) and then look in browser console and then click on that link and it will open variable viewer, explore the targets there.

be sure to share what you discover i dont remember how i did it so it would be a nice reminder

Upvotes: 0

Related Questions