Run script on image load

38 views
Skip to first unread message

GmUser

unread,
Oct 12, 2012, 9:30:45 AM10/12/12
to greasemon...@googlegroups.com
I know we used to discuss this issue quite a bit back when
Greasemonkey was in its infancy.

It sure would be nice if it could be implemented, though.

I know (assume it's still true) that images don't have the
early load event, and maybe they (still?) don't have an
onload or similar event, but if they do, or if something
can be done, it would be really nice to have this ability
so those of us who want to write scripts for images
can do so. (like, as merely one example, a semi-intelligent
image zoomer to have images displayed in a certain
size, or any number of other possibilities)

LWChris@LyricWiki

unread,
Oct 12, 2012, 2:36:14 PM10/12/12
to greasemon...@googlegroups.com
You can append a "load" event listener to elements, too. On images, this
event is fired as soon as the image ressource was loaded.

Testfile "gmtest.html"
> <!DOCTYPE html>
> <html>
> <head>
> <title>Test</title>
> </head>
> <body>
> <!-- Some large pic that'll take a second to load -->
> <img id="pic"
> src="http://www.geeknaut.com/images/windows-wallpapers/Winodws-7-aurora-wallpaper.jpg"
> width="1920" height="1200" />
> </body>
> </html>

Note that the following test script will only work if you set the
"greasemonkey.fileIsGreaseable" option in about:config to "true" during
the test. Don't forget to disable it later.
> // ==UserScript==
> // @name Test
> // @namespace LWChris
> // @include file://*gmtest.html
> // @version 1
> // @grant none
> // ==/UserScript==
>
> picLoad = function(evt) {
> document.title += " pic";
> }
>
> document.getElementById("pic").addEventListener("load", picLoad, false);
> document.title += " append";

You will see the title is "Test" from the HTML file, it's "Test append"
while the pic is loading, and "Test append pic" as soon as the image is
ready.

Chris

Anthony Lieuallen

unread,
Oct 12, 2012, 5:04:29 PM10/12/12
to greasemon...@googlegroups.com
I believe OP was referring to navigating to an image.

GmUser

unread,
Oct 12, 2012, 6:08:29 PM10/12/12
to greasemon...@googlegroups.com
Yes, and then have a GmScript executed because of it.

Maybe you could check to see if it's an image and make the script be called
using one of the events mentioned. (instead of the HTML doc early load)

It would be nice if you could do the same with plain text files, too.

It would be a whole lot better than nothing.
Reply all
Reply to author
Forward
0 new messages