Discussions » Development

Multiple instances of a script (iframes) generate multiple instances of variables?

§
Posted: 2017-07-23
Edited: 2017-07-23

Multiple instances of a script (iframes) generate multiple instances of variables?

I'm writing a script to add a keyboard shortcut to download whatever the mouse cursor is pointing at, specifically for tumblr.

I noticed that I have several instances of the script running for each(?) iframe. I have a variable that holds the URL to the last image pointed at by the cursor.
When I try to update the variable (by pressing shift, or whatever key in the "keydown" event listener), it updates only in one instance, the other one stays undefined.

my current source code here (warning, not clean, some testing function and lots of debug messages):
https://gist.github.com/glubsy/d41252e0883fbf61f6914aad722ed45c

Here's a page to test it on: https://yahoonews.tumblr.com/post/116159782521/photos-of-the-day-april-11-2015-a-visitor-takes

The function in question is the updateLink() one, which updates the currentLink variable.

I doubt there's a way to have this variable common for all instances of the script, unless I deactivate the script for iframes. But if I do that, I can't scrape the entire node tree for images. :(

Any idea on how to work around that?

§
Posted: 2017-07-24
Edited: 2017-07-24

I've made progress I think, I changed to @run-at document-idle and tweaked some functions a bit.

However, now I get the following error too. Still working though. Error: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "https://www.tumblr.com" from accessing a cross-origin frame.

Using img_links_iframe[i].onmouseenter= function (){ updateLink(this.src); pointed_obj = this; }; Doesn't work, but it works with onmousemove, albeit a bit more expensive I guess. Weird.

§
Posted: 2017-07-24
Edited: 2017-07-24

Nevermind, it's working now. It was just the testing "IMGmatches" function causing the error.

Post reply

Sign in to post a reply.