Forum Moderators: open

Message Too Old, No Replies

Get filename from URL

Some help on getting a filename from a URL in a Greasemonkey script..?

         

MMumpower

1:20 am on Aug 14, 2007 (gmt 0)

10+ Year Member



I'm working on a script for Greasemonkey.

What I have so far can be found here (http://userscripts.org/scripts/review/11386).

Basically, what I'm looking for is a way to show the filename instead of the full URL.

Right around in here:
# if (a.href.match(/\.(jpg¦jpeg¦gif¦png)$/i)) {
# a.innerHTML = "" + a.href + "<img src=\"" + a.href + "\" class=\"preview\">";
# }

I want to make it look something like this:
# if (a.href.match(/\.(jpg¦jpeg¦gif¦png)$/i)) {
# var filename = document.getalllinks.href.filename;
# a.innerHTML = "" + filename + "<img src=\"" + a.href + "\" class=\"preview\">";
# }

Obviously, that doesn't work: the changes in the second coding is just to show you what I'm looking for.

What I need to know is what will work?

Any ideas?

Thank you in advance!

Arno_Adams

2:29 pm on Aug 14, 2007 (gmt 0)

10+ Year Member



Hi,

Use location.href instead of document.getalllinks.href.filename;

HTH, AA

MMumpower

10:41 pm on Aug 14, 2007 (gmt 0)

10+ Year Member



Hello,

Thanks for your help, but that still shows a url, not a filename.

For example:

I have the address: [somewhere.com...]
In that web folder: 1.jpg
What it shows with my original script: [somewhere.com...]
What is shows with your suggestion: [somewhere.com...]
What I'm looking for: 1.jpg

I also tried:
# if (a.href.match(/\.(jpg¦jpeg¦gif¦png)$/i)) {
# var fileName = location.pathname.substring(location.pathname.lastIndexOf('/')+1)
# a.innerHTML = "" + fileName + "<img src=\"" + a.href + "\" class=\"preview\">";
# }
But it did not work.

[edited by: MMumpower at 10:50 pm (utc) on Aug. 14, 2007]

MMumpower

10:54 pm on Aug 14, 2007 (gmt 0)

10+ Year Member



I have solved my problem.

Thank you for the help!

This became my final coding:

1. // ==UserScript==
2. // @name Open Dir Image Viewer (Edited by Mitchell)
3. // @namespace lenzm.net
4. // @include *
5. // ==/UserScript==
6. var thequickfixbymitch = document.createElement("div");
7. thequickfixbymitch.innerHTML = '<style type="text/css">a .preview{display: none;}a:hover .preview{display: block;position: absolute;top: 0px; right:0px; z-index: 1;}.preview{border-color: #000;}</style>';
8. document.body.insertBefore(thequickfixbymitch, document.body.firstChild);
9. function DocFileNameExtract()
10. {
11. wholeurl = a.href;
12. x = wholeurl.length;
13. while((wholeurl.substring(x,x-1))!= "/"){ x--; } clipstart = x;
14. return wholeurl.substring(wholeurl.length,clipstart);
15. }
16. function xpath(query) {
17. return document.evaluate(query, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
18. }
19. var title = document.getElementsByTagName('title')[0].innerHTML;
20. if(title.match(/^Index of /)) {
21. var links = xpath("//a[@href]");
22. for (var i = 0; i < links.snapshotLength; i++) {
23. var a = links.snapshotItem(i);
24. if (a.href.match(/\.(jpg¦jpeg¦gif¦png)$/i)) {
25. a.innerHTML = "" + DocFileNameExtract() + "<img src=\"" + a.href + "\" class=\"preview\">";
26. }
27. }
28. }

[edited by: MMumpower at 10:54 pm (utc) on Aug. 14, 2007]

Arno_Adams

6:37 am on Aug 15, 2007 (gmt 0)

10+ Year Member



Hi, Good to hear you've solved the problem.

You could also use:

location.href.substring(location.href.lastIndexOf('/')+1)

HTH, AA