>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 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.