Script Request: Put Alt with images

in Ideas and script requests
Subscribe to Script Request: Put Alt with images 13 posts, 4 voices



flip Scriptwright

Maybe there is another (better?) way to do this, but i thought greasemonkey is the way to go.
Since i know absolutely nothing about java(script) i post here.

My request:
A script that put's alt decription with images.

Example:

becomes: image1 description

I would like to specify for certain images of other sides an alternate description. The purpose of this: if you copy a page and paste it in notepad the images are lost.

I don't think this will need a big script. If anyone can do it, would be very appreciated !

P.S. If you know another way to achieve this, not necessarily with greasemonkey, please let me know!

 
pojo Scriptwright

i made it -> http://userscripts.org/scripts/show/11364

 
flip Scriptwright

Thx a lot! Unfortunately it's not quiet doing what i wanted.
Like it is now (i think): each image get's the description "image1 description"
where the 1 counts up.

I would like it to be:

something like
if (image = cow.jpg) description = "a cow!"
if (image = dog.png) description = "a doggy :)"

I would like to specify in that list which image get's which alt description. So that i can adjust it later.

just that i don't know the right syntax to implement this because i don't know java at all.
If you could make this little change you make me very happy ;)

 
LouCypher Scriptwright

What if (image = 32768.gif) or (image = iVBORw0KGgoAAAA.png) or (image = 8qGAyu7vPqfwFqGc.jpg)?

 
Descriptor Scriptwright

var description = "";
for(var i=0; document.images.length > i; i++){
  if(document.images[i].alt && document.images[i].alt != " ") continue;
  description = ( document.images[i].title || document.images[i].src );
  document.images[i].alt = description;
}

 
Descriptor Scriptwright

Or to get everything

var description = "";
for(var i=0; document.images.length > i; i++){
  description = " - " + document.images[i].title + " - " + document.images[i].src;
  document.images[i].alt += description;
}

 
flip Scriptwright

@LouCypher. i know the image names so there won't be any 'random name' images.

@Descriptor. Looks more like i need. Do you think this would work ?

var description = "";
for(var i=0; document.images.length > i; i++){
if(document.images[i].alt && document.images[i].alt != " ") continue;
if(document.images[i].title == "cow") description = "a cow";
if(document.images[i].title == "dog") description = "a dog";
document.images[i].alt = description;
}

P.S. since i'm kinda new to greasemoney aswell can i couldn't find a HELP or a FAQ. How do i change a script? Adding it is not a problem.

 
Descriptor Scriptwright

Yes, flip, that looks like it will work fine. But the title attribute isn't often used, it shows in a tool-tip when you mouse over an image. It used to be that alt was used for that which is improper, alt is meant for when no image is displayed, and is also used in place of the image when you Save as text or copy-all paste as text.
For example you might see an image with alt-text that says "Click here to login", which should really be in title and alt should be "Login button image" or whatever the button says, just as an example. The title attribute isn't often used, though I use it if I think the image needs a description.
A good example is Wikipedia where they use the title attribute to describe the images, but actually the alt text there is the same as the title. I think that alt is also read by aural browsers used by the visually impaired. Note that title can be used in other tags such as links, as in the link I made above.

Now if you want to get "cow" out of the image name, well that's a more complex script that would probably require some regular expression to search the image src string, which is why I added it to "description". I actually find both scripts I wrote above useful because I sometimes want the image name, and without the script I have to go the extra step to Copy image to get the name.

If you want to change someone else's script, you can Edit the script, but it would be better to save it under a new name and change the metadata, otherwise if you install the script again that will over-write all your changes.

Also see the GreaseSpot Wiki

 
Descriptor Scriptwright

To get just the name part of the image, you can add a regexp like this in the above code...

imgSrcName = document.images[i].src.match(/\/([^\/]+)\.\w+$/)[1];

that's just off the top of my head, and assumes you know that the image has an extension such as ".jpg", and nothing after, no "?".

Edited, think I made a mistake.

 
flip Scriptwright

Thx for your reply descriptor. Well actually i put in my code title because i thought it was the title for the filename. And i meant the filename. I can make some code but not java(script) so there is my little error. What would i have to put to look at the FILENAME rather then at the TITLE ?

If i can grab the filename as a whole, im not sure if it needs regexp. But if you say it does then i believe you ;)

Just to clarify once more

Filename: XYZ.PNG -> ALT : something1
filename: ABC.GIF -> ALT: something2

i'm not searching for a particular word in the filename.
And also it is not needed to grab only the name part of the filename. (Get Cow out of Cow.png or something)
Thx for your weblinks i'll have a look.

P.S. The thing that confused me at first when i tried to edit a script i got an 'open file' dialog so i clicked cancel. but i didn't look at the window description that said: 'please select a text editor'

 
flip Scriptwright

So i figured the filename would be src. I tried to make it like this but it doesn't work

var description = "";
for(var i=0; document.images.length > i; i++){
if(document.images[i].alt && document.images[i].alt != " ") continue;
if(document.images[i].src == "cow.gif") description = "a cow";
document.images[i].alt = description;
}

Also i tried to do it with your suggestion with regexp:

var description = "";
for(var i=0; document.images.length > i; i++) {
if(document.images[i].alt && document.images[i].alt != " ") continue;
if(document.images[i].src.match(/\/([^\/]+)\.\w+$/)[1]; == "cow") description = "a cow";
document.images[i].alt = description;
}

still no go. i know i'm getting close :P

 
Descriptor Scriptwright

Yea, real close. I've been kinda busy and just popped in, but just remove the semi-colon (that is for end of command line)...

if(document.images[i].src.match(/\/([^\/]+)\.\w+$/)[1] == "cow") description = "a cow";

I think that'll work.

 
flip Scriptwright

Ah that's so sweet, just what i wanted. Thx a lot !