[Greasemonkey] Event Listener - Context Menu Cut/Paste

Matt Sargent matt.sargent at earthlink.net
Mon Feb 13 23:26:45 EST 2006

I had already considered using a timer, but really wanted to avoid it. It seems 
really odd that editing through the context menu doesn't trigger a change 

Anyway, for the curious, what I eventually did is this: I keep track of whether 
or not the text box currently has focus. I added a contextmenu event listener 
to the document. When that triggers, I check to see if the text box has focus. 
If not, I ignore it. If it does, I start a timer loop, which updates the character 
count in the text box (the main purpose of the script). The loop is controlled 
by a boolean which can be turned off by a number of situation that would 
indicate the context menu is no longer active, such as a keypress, change of 
focus, or a change of text.

About the only situation where the timer would keep running is if the user 
selected some text, and pasted the exact same text in its place. But as soon as 
they do anything else on the page, it would be detected and the the timer loop 
would be terminated..

The main trick to this is just using the document-level contextmenu event, and 
checking that the text box has focus when it triggers.

On 13 Feb 2006 at 19:04, Bill Donnelly wrote:

> Then that is the answer.
> Not a great answer, necessarily, but probably the best you will get.
> (unless there is a better way) I just tested it myself to see why it
> wasn't working. And it does work. You get an event for "onMouseUp",
> which occurs when the person lets up on the mouse click. I think you
> can even check whether it was a left or right click. Then, what I
> meant by 'timing involved' is that you will need to perform some
> function for some amount of time every, say, 10 milleseconds, or so,
> looking for the paste-change, and then perform whatever you want when
> you find it. You will need to reset the timed-check at some point.
> Either after so many iterations, or until you get a keypress, or
> another click, or you get a blur event, or when you think you have
> given the user enough time to paste, which is a questionable thing,
> but it doesn't hurt to iterate through the check loop for awhile. At
> some point, they have either pasted, or decided not to paste. It's not
> a terribly hard routine to write, and kind of a sucky work-around, but
> it would work. You could also just run an infinite loop checking for a
> paste-change rather than wait for a click. Checking every 10
> milleseconds probably won't kill anyone's computer. And should give
> sufficient reaction time. You could just do that and not use any other
> event checks, like keypress.
> Matt Sargent wrote:
> >Tried both of those. Thanks, though.
> >
> >On 13 Feb 2006 at 14:56, Bill Donnelly wrote:
> >
> >  
> >
> >>maybe check for an onclick (and/or onmouseup) and use that
> >>to check to see if anything is pasted?
> >>there might be some timing involved
> >>
> >>-- 
> >>Where is the OUTRAGE???
> >>   What is WRONG with 'We The People'???
> >>      pResidential 'elections' 2000 and 2004 (and 2008??.....)
> >>
> >>_______________________________________________
> >>Greasemonkey mailing list
> >>Greasemonkey at mozdev.org
> >>http://mozdev.org/mailman/listinfo/greasemonkey
> >>
> >>    
> >>
> >
> >
> >_______________________________________________
> >Greasemonkey mailing list
> >Greasemonkey at mozdev.org
> >http://mozdev.org/mailman/listinfo/greasemonkey
> >
> >  
> >
> -- 
> Where is the OUTRAGE???
>    What is WRONG with 'We The People'???
>       pResidential 'elections' 2000 and 2004 (and 2008??.....)
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey

More information about the Greasemonkey mailing list