List Info

Thread: Calling a function in GM from injected HTML




Calling a function in GM from injected HTML
user name
2006-08-04 17:52:52
NB: I am only an average JS coder.

I have managed to inject this piece of HTML into a webpage:

<a
href="javascript:RemoveGroup('Foo')">[-]&am
p;lt;/a>

My GM code looks like this:

function RemoveGroup(sGroupName)
{
    alert(sGroupName)
}

Why do I get a "RemoveGroup is not defined"
error when I click the link?

Is this something to do with "unsafewindow"?

My Googling has born no fruit. Any help would be
appreciated.
-- 
View this message in context: http://www.nabble.com/C
alling-a-function-in-GM-from-injected-HTML-tf2052532.html#a5
654824
Sent from the MozDev - greasemonkey forum at Nabble.com.
_______________________________________________
Greasemonkey mailing list
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
Calling a function in GM from injected HTML
user name
2006-08-04 18:07:07
On 8/4/2006 1:52 PM, IndaUK wrote:
> <a
href="javascript:RemoveGroup('Foo')">[-]&am
p;lt;/a>
>...
> Why do I get a "RemoveGroup is not defined"
error when I click the link?

The long and the short of it is that GM scripts execute in
their own 
(security related) scope.  When the (GM) script stops
running, the scope 
disappears, along with everything inside it.  *UNLESS* there
is still a 
reference to it somewhere.  A string
"javascript:RemoveGroup..." is not 
a javascript object reference.

It is the same reason this doesn't work:

(function() {
var x=10;
})();
alert(x);

Will say that x is not defined.  That (anonymous) function
is its own 
scope.  Once the function ends, the scope is gone, as is the
variable X 
inside it.  The way you do what you want above is
(greasemonkey code here):

var anchor=document.getElementById('foo'); // get a
refernence to the a
a.addEventListener('click', RemoveGroup, true);
_______________________________________________
Greasemonkey mailing list
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
Calling a function in GM from injected HTML
user name
2006-08-04 21:18:26
Thanks again but one problem leads to another...


I have injected many elements into the webpage, each with a
different ID.

  e.g. "<span
id='group_" + foo[i] +
"'>[-]</span>"

I later add the event listeners by:

  var a =
document.getElementById("group_" + foo[i]);
  a.addEventListener("click",
RemoveGroup, true);

 

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)
{
  alert(sGroupName)  &am
p;nbsp;     //r
eturns
"[object XPCNativeWrapper [object MouseEvent]]"
  alert(typeof(sGroupName)) &
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/C
alling-a-function-in-GM-from-injected-HTML-tf2052532.html#a5
657879
Sent from the MozDev - greasemonkey forum at Nabble.com.
_______________________________________________
Greasemonkey mailing list
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
Calling a function in GM from injected HTML
user name
2006-08-04 23:14:30
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 <indaukgmail.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;&am
p;nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//r
eturns
> "[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/C
alling-a-function-in-GM-from-injected-HTML-tf2052532.html#a5
657879
> Sent from the MozDev - greasemonkey forum at
Nabble.com.
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkeymozdev.org
> http:
//mozdev.org/mailman/listinfo/greasemonkey
>
_______________________________________________
Greasemonkey mailing list
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )