2

I have the following fairly basic greasemonkey script:

var newloc = location.href.replace(/^(.*)-xyz-(.*)$/i, "$1$2");
if (newloc != location.href)
    location.href = newloc;

That is, it basically strips out "-xyz-" from the URL and loads the page again. So if you navigate to "www.example.com/a-xyz-b/" it'll reload the page at "www.example.com/ab/".

Now, the script work fine if the page is an HTML page. But if I open a .jpg file or something that's not HTML then the script does not run at all.

Is this just a limitation of greasemonkey? That it only works if the page is actually text/html? What is an alternative way this functionality could be done?

Dean Harding
  • 71,468
  • 13
  • 145
  • 180

1 Answers1

2

Yes, Greasemoney fires on the DOMContentLoaded event, which doesn't seem to trigger on media objects (no DOM).

Get around this by firing on the parent/referrer pages and changing the links there.

Or, if the file names are on the local machine, use a text editor or batch job to rename/rewrite the links/names.

If neither of these workarounds is viable, post the specific details of how you are feeding these URLS to FireFox (name the browser in use if it's not FF).

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Hmm, interesting. I was really only doing it as a favour for a client and I don't know all of the details, but thanks... you've given me some ideas to go on with. If I get any more, I'll update the question :) – Dean Harding Oct 01 '10 at 00:02
  • As of Firefox 27.0.1, at least for `application/json` content the greasemonkey script perfectly fires when JSON has finished loading, with no further action required. Also, if it does not fire, a thing to try can be to [change `@run-at` to `document-start`](http://stackoverflow.com/q/4978736/521032), then watch for `readyState` or, as a last resort, use a timer. – Septagram Mar 27 '14 at 09:10