How to access elements of iframe created in greasemonkey?

3,069 views
Skip to first unread message

Tim

unread,
Jul 27, 2010, 7:59:09 AM7/27/10
to greasemonkey-users
Hi there,

I've written a greasemonkey script that creates an 'iframe', and sets
it source to a different domain of the original webpage. This works
fine.

How do I access elements in this iframe? Specifically how do I
use .getElementById("")?

The actual element itself is defined (in a XPCNativeWrapper) and
its .body etc, but I cannot use .getElementById or tagName.


I've tried:
var frameDoc =
document.wrappedJSObject.getElementById("frameID").contentDocument;
GM_log( frameDoc.getElementById("idname") );

And without the wrappedJSObject. Both cases return 'null' or
'undefined'.

I've tried setting timeout manually with: window.setTimeout(test,
3000);
Where test was a function holding the above.

And I've tried a timer with
frameDoc.addEventListener("DOMFrameContentLoaded", test, false);

With the timers I got errors either: "Security Manager vetoed
action...." if used without unwrapping first with wrappedJSObject, or
"permission denied.... site xyz to load html.document from site 123".

Any help would be greatly appreciated!

I may consider using GM_XmlHttpRequest instead, however this would not
work on chrome.

AClone Iam

unread,
Jul 27, 2010, 9:21:54 AM7/27/10
to greasemon...@googlegroups.com
1) Include the domain of the iframe in the scripts includes
2) the script will now run once for the normal window and again for the iframe content.  You will need to detect which domain the script is running in, and action accordingly.

You can exchange data between the two running instances of your script, by either using
a) GM_set and GM_get, or
b) messaging.

Das Shrubber


--
You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
To post to this group, send email to greasemon...@googlegroups.com.
To unsubscribe from this group, send email to greasemonkey-us...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/greasemonkey-users?hl=en.


Tim

unread,
Jul 27, 2010, 6:05:47 PM7/27/10
to greasemonkey-users
Thank you so much!

That worked perfectly, I cannot believe I did not come across this in
all my searching for a solution.

I suppose I did learn a fair bit about browsers & greasemonkeys
methods of security.

And cheers for the tips using GM_get & GM_set.



For anyone else that stumbles across this thread, heres how I made the
script run on the different frames:

@include both urls

if(document.location.href.indexOf("URL1"))
{
GM_setValue("createdInUrl1", true);

//Code for url 1
}

else if(document.location.href.indexOf("URL2") != -1 &&
GM_getValue("createdInUrl1"))
{
GM_setValue("createdInUrl1", false);

//Code for url 2, i.e. the iframe.
}


Because the browser will block communication between different
domains, parent.etc will not work nor will window.top.etc. So as
suggested above I used GM get/set value.
Setting it to false in the iframe branch ensures it is not set if the
user visits just the iframe url on its own.



On Jul 28, 1:21 am, AClone Iam <aclone....@gmail.com> wrote:
> 1) Include the domain of the iframe in the scripts includes
> 2) the script will now run once for the normal window and again for the
> iframe content.  You will need to detect which domain the script is running
> in, and action accordingly.
>
> You can exchange data between the two running instances of your script, by
> either using
> a) GM_set and GM_get, or
> b) messaging.
>
> Das Shrubber
>
> > greasemonkey-us...@googlegroups.com<greasemonkey-users%2Bunsu...@googlegroups.com>
> > .

Brian L. Matthews

unread,
Jul 27, 2010, 9:02:25 PM7/27/10
to greasemon...@googlegroups.com, AClone Iam
On 7/27/10 6:21 AM, AClone Iam wrote:
> 1) Include the domain of the iframe in the scripts includes
> 2) the script will now run once for the normal window and again for
> the iframe content. You will need to detect which domain the script
> is running in, and action accordingly.
>
> You can exchange data between the two running instances of your
> script, by either using
> a) GM_set and GM_get, or
> b) messaging.

Messaging? What's that in this context? A search for messaging on
wiki.greasespot.net doesn't find anything.

Thanks,
Brian

Reply all
Reply to author
Forward
0 new messages