Well, I think that's because your click() function and jQuery.click() work differently. You just emit click event and let browser do the rest, when jQuery for some reason (cross-browser contingency? I don't know) tries to call existing event handlers itself - and handlers installed in page scope can't access event object jQuery makes for them in script scope.
Anyway, good to hear you solved your problem. I only hope that my attempts at coding workaroudn will be useful if you run into something else later. Good luck! 2014-07-19 16:18 GMT+04:00 Daniel Wynalda <[email protected]>: > I believe I have figured out how to make things work. I don't think this > is actually a problem with scope and script vs page permissions. I think > it's an issue with jQuery not doing something correctly. I was able to > replace my code that called $(object).click() with a function written by > myself (in script scope obviously) that actually clicks the link. That > function runs properly, no error, and I was able to get the script > functional again. I didn't have to insert anything into the page and the > script seems to be functioning well again. > > What I found odd was I was not asking to run anything inside the page that > was trying to get back to the script level. I was simply trying to click > on a link in the page and jquery couldn't click it. Perhaps jQuery tries > to call itself recursively after the click or something - as a callback?? > That might cause the click (in jQuery) to try to call back to the script > scope? > > Anyway - I was able to solve the problem I was having with the script. I > do want to gain more understanding about how to use cloneInto. > > I also found another change from GM 1.5 to GM 2.0 -- it appears that it's > either leaner or cleans things up better because executing behavior on > objects in the page in a setTimeout doesn't function the same way every > time in both versions. I had to explicitly trap all of my delayed clicks > in enclosures to insure they stay defined. The objects were still on the > page but Greasemonkey appears to whack defined variables before the > setTimeout occurs now. (the full scope of the script isn't available to > the setTImeout routine). > > On Saturday, July 19, 2014 7:05:24 AM UTC-4, Daniel Wynalda wrote: >> >> 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.
