iframe and javascript scope

47 views
Skip to first unread message

Mortimer

unread,
May 11, 2007, 10:05:39 AM5/11/07
to greasemonkey-users
Hello,

I have been doing a "infinite scrolling" script (already posted about
it a couple of weeks ago) for flickr:
http://6v8.gamboni.org/IMG/js/flickrautopage.user.js

When the user scrolls down to the bottom of the page, I use an iframe
to fetch the next page in the series and append it to the current
page, this works fine and is quite simple to implement.

After my earlier discussion, I managed to get the iframe correctly so
that javascript and greasemonkey existing in the following
(automatically loaded in the iframe) pages get executed. However, I
have a problem for scripts (starting with flickr own javascripts)
using event listeners.

First, let me explain how I do it: I add an iframe to my document,
with dimension 1px, 1px so it's hidden but script in it are still able
to execute (having the iframe with display=none messes with scripts in
the frame apparently). The iframe has a onload event listener that
will copy all elements from the new page in my existing page, using
xpath and dom: foreach elt with a particular xpath in the iframe, I do
a insert.appendChild(elt) on an insert point in my current page.
The element, and all their properties (including event listeners
etc...) added by javascript executed in the iframe are copied into my
current page. Up to there everything is great.

However, some of the events copied while copying the dom elements from
the iframe to the page refer to javascript functions in the iframe
scope, and when moved, they are lost and the script cannot execute
correctly.

For example, on flickr discussion thread, I copy the posts from the
next page to the current page, the posts are preceeded by small image
with buddy icons. These buddy icons have a onmouseover listener that
will call a function refering to a flickr function: _get_eventtarget
This function is declared in a .js loaded in every flickr page (so
it's availlable in my current page, and in my iframe page), however,
because when the event listener is created the scope is the javascript
in the iframe, when the element is moved, the _get_eventtarget
reference is broken.

I am not sure if it's clear, but this problem also arises with
greasemonkey scripts executing inside the iframe and I really have no
idea how to fix that.

Does anyone have an idea?

Pierre

RodMcguire

unread,
May 11, 2007, 12:37:55 PM5/11/07
to greasemonkey-users
On May 11, 10:05 am, Mortimer <pierre.andr...@gmail.com> wrote:

> the iframe to the page refer to javascript functions in the iframe
> scope, and when moved, they are lost and the script cannot execute
> correctly.
>
> For example, on flickr discussion thread, I copy the posts from the
> next page to the current page, the posts are preceeded by small image
> with buddy icons. These buddy icons have a onmouseover listener that
> will call a function refering to a flickr function: _get_eventtarget

Is this what you want? Say img is one of those IMG nodes

img.addEventListener('mouseover', unsafeWindow._get_eventtarget,
false);
or
img.addEventListener('mouseover', function (e)
{doSomething(unsafeWindow._get_eventtarget());}, false);

Mortimer

unread,
May 11, 2007, 1:18:06 PM5/11/07
to greasemonkey-users

On May 11, 5:37 pm, RodMcguire <mcgu...@telerama.com> wrote:
> Is this what you want? Say img is one of those IMG nodes
>
> img.addEventListener('mouseover', unsafeWindow._get_eventtarget,
> false);
> or
> img.addEventListener('mouseover', function (e)
> {doSomething(unsafeWindow._get_eventtarget());}, false);


well, it was only an example. I know what script flickr are executing,
and I can go and reapply the events correctly for that, so what you
are saying is a partial solution I thought about.

But this does not scale, the fact is that I don't know all the scripts
that will be applied, in particular, the user might have other
greasemonkey script applying to the pages where the auto pagination
script applies.
I know of some of these scripts and could implement all of them in my
script, but that's not practical, I can't predict all the GM
scripts ;). So I would need some automatic way to keep these scripts
from breaking.

Pierre

RodMcguire

unread,
May 11, 2007, 1:44:24 PM5/11/07
to greasemonkey-users

There is currently no way to tell what event listeners have been added
to an object with addEventListener. Interacting with other GM scripts
is always tricky, and sometimes you have to rewrite the scripts you
want to interact with.

For listeners stored as attributes, you could probably write a general
function that goes through the list ['onclick', 'mousover', ....] and
massages the contents of these attributes so the functions are called
in the right scope.

Reply all
Reply to author
Forward
0 new messages