[Greasemonkey] Calling a function in GM from injected HTML

Arvid Jakobsson arvid.jakobsson at gmail.com
Sat Aug 5 02:14:30 EDT 2006


function RemoveGroup(event)
{
alert(event.target.id) //event.target is the clicked element, this
will alert 'foo'
}

var anchor=document.getElementById('foo'); // get a refernence to the a
anchor.addEventListener('click', RemoveGroup, true);

In conclusion: the object passed to the handler (RemoveGroup) is an
event object. Most intresting property of the event object is target,
which is the node that fired the event.

On 8/4/06, IndaUK <indauk at gmail.com> wrote:
>
> Thanks again but one problem leads to another...
>
>
> I have injected many elements into the webpage, each with a different ID.
>
> &nbsp;&nbsp;e.g. "&lt;span id='group_" + foo[i] + "'>[-]&lt;/span>"
>
> I later add the event listeners by:
>
> &nbsp;&nbsp;var a = document.getElementById("group_" + foo[i]);
> &nbsp;&nbsp;a.addEventListener("click", RemoveGroup, true);
>
> &nbsp;
>
> I need to pass a string (or the element ID or the element object) to my
> original function.
>
> "a.addEventListener('click', RemoveGroup, true);" seems to pass an object to
> my function but I don't know what to do with it.
>
> function RemoveGroup(sGroupName)
> {
> &nbsp;&nbsp;alert(sGroupName)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//returns
> "[object XPCNativeWrapper [object MouseEvent]]"
> &nbsp;&nbsp;alert(typeof(sGroupName))&nbsp;&nbsp;&nbsp;&nbsp;//returns
> "object"
> }
>
>
> The docs on XPCNativeWrapper confuse me. How can I reference the element
> that was clicked?
>
> Any ideas?
> --
> View this message in context: http://www.nabble.com/Calling-a-function-in-GM-from-injected-HTML-tf2052532.html#a5657879
> Sent from the MozDev - greasemonkey forum at Nabble.com.
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
>


More information about the Greasemonkey mailing list