If my understanding of how jQuery works is correct, you should try using
jQuery from page scope.

        var $ = unsafeWindow.jQuery;
        //export function is needed so page-scope jQuery can call
script-scope callback we pass to .each()
        $(".formInputButton").each( exportFunction(function(index)
                {
                    var thisvalue=$(this).attr('value');
                    if (thisvalue=="CLICK ME")
                    {
                        alert('found button !: '+thisvalue);
                        $(this).click();
                    }

                }, unsafeWindow) );

Could you please include jQuery into your test page so I could test it?


2014-07-19 15:05 GMT+04:00 Daniel Wynalda <[email protected]>:

> You obviously understand this way better than I do.  I usually think of
> myself as a fairly competent programmer but the talk about scope loses me I
> guess.  I am simply trying to click something in a page.  There is jQuery
> *in the page* and there is jQuery in my script (different instances).  I'm
> simply looking to find out how I can click the button.
>
> In my really basic example I have set up a webpage:
> http://hithuntheal.com/x.php
>
> and I have set up a script to click on the button
> http://hithuntheal.com/x.user.js
>
> I'm trying to get the script to click the button - and allow for
> GM_setValue/GM_getValue.
>
> How can I click an element in the page?   I find it odd that I can get the
> onClick attribute just fine but I can't click the element.
>
>
> On Friday, July 18, 2014 10:56:38 AM UTC-4, TheVindicar wrote:
>
>> >So if jQuery isn't allowed to touch things in the page
>> Not quite... script scope jQuery CAN touch things in the page scope just
>> fine, but if it tries to call functions from the page scope (callback,
>> event handlers, whatever!) and feed them objects in the script scope (since
>> it doesn't know about scopes), then error happens.
>>
>> >Interestingly if I use jQuery to modify elements in the page I don't
>> have any issue.   I change text, I rearrange things, etc.
>> Regular DOM manipulations are fine, since all objects involved belong to
>> page scope. So it's script scope function accessing page scope (which works
>> since script scope is more privileged). Problems arise when it's the other
>> way around - page scope function trying to access script scope.
>>
>> >I could call the jQuery in the page - if I knew how to do it.
>>     var $ = unsafeWindow.jQuery;
>> That's all! You can work with that jQuery, but again, once you give it
>> object, array, or function created in script scope, it will choke.
>>
>> >My basic problem remains the same - I don't understand how to call
>> something in the page when I have @grant set.
>> What do you mean by "call"?
>> You can call a function from the page just fine, as long as you give it
>> only primitive values (strings and numbers) and objects from page scope
>> (either exported or created there) to work with. So:
>>
>>   unsafeWindow.PageFunction( {data:1} ); //error, object is in the script
>> scope
>>
>>   var myobj = new unsafeWindow.Object(); //creating object in page scope
>>   myobj['data'] = 1; //if value was an object/array, you'd have to export
>> it too, or create in page scope as above
>>   unsafeWindow.PageFunction(myobj); //should work
>>
>>   unsafeWindow.PageFunction( cloneInto({data:1}, unsafeWindow) );
>> //should work
>>
>> If you mean "execute your code with privileges of page scope", though...
>> On #greasemonkey at FreeNode I've been told that the most practical way to
>> actually run code in page scope is to inject script tag into the page. Let
>> me quote myself:
>>
>>   function RunInPage(func) {
>>       var s = document.createElement("script");
>>       s.textContent = "(" + func + ")();";
>>       document.body.appendChild(s);
>>       setTimeout(function(){document.body.removeChild(s)}, 0);
>>   }
>>   RunInPage(function(){
>>       //you can use page-scope code (like jQuery if the page uses it)
>> here freely - entirety of this script is run in page scope.
>>       var $ = window.jQuery;
>>       $.each([], function(){});
>>   });
>>
>> Exporting homebrew jQuery into the page would be hard, though...
>> exportFunction() only lets you call function from page scope, but it
>> doesn't seem to open access to any defined properties on that function. Of
>> course, you could just inject another script tag to load your jQuery into
>> page scope (don't forget to back up and restore original jQuery if page
>> needs it), and then (if necessary) run some code to apply your patches to
>> it, but I'm not sure how well it would work.
>>
>>
>>
>>
>> 2014-07-18 17:24 GMT+04:00 Anthony Lieuallen <[email protected]>:
>>
>>> On Fri, Jul 18, 2014 at 7:39 AM, Daniel Wynalda <[email protected]>
>>> wrote:
>>> > I don't understand how to call something in the page when I have
>>> @grant set.
>>>
>>> http://wiki.greasespot.net/Location_hack ?
>>>
>>> --
>>> You received this message because you are subscribed to a topic in the
>>> Google Groups "greasemonkey-users" group.
>>> To unsubscribe from this topic, visit https://groups.google.com/d/
>>> topic/greasemonkey-users/SXfpyvcQofQ/unsubscribe.
>>> To unsubscribe from this group and all its topics, send an email to
>>> [email protected].
>>> To post to this group, send email to [email protected].
>>>
>>> Visit this group at http://groups.google.com/group/greasemonkey-users.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>  --
> You received this message because you are subscribed to a topic in the
> Google Groups "greasemonkey-users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/greasemonkey-users/SXfpyvcQofQ/unsubscribe
> .
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/greasemonkey-users.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"greasemonkey-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/greasemonkey-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to