1

I am relatively new to javascript and I want to code a javascript-bookmarklet which is able to rewrite a URL from

http://www.xyz.com/test/stuff/video129852_zc-7931f8bf_zs-2d7967f4.html

to

http://www.xyz.com/other-directory-thing/video129852.html

in the browser.

I want to replace the two directories "test/stuff" with one "other-directory-thing" and erase everything in the URL from the "_" to the .html at the end.

Can anyone help me? Thanks a lot! :)

Martin
  • 11

1 Answers1

0
var text = "http://www.xyz.com/test/stuff/video129852_zc-7931f8bf_zs-2d7967f4.html";
var new_text = text.replace("/test/stuff/", "/other-directory-thing/");
document.write(new_text);

This is to replace the URL, as for the last part of the link, it would depend if the length of 'video129852' will always be 11 characters long.

To implement it within a 'bookmark script' look at this: https://stackoverflow.com/a/3024846/2245614

Ash King
  • 1,223
  • Thanks for your answer. :) Is there any possibility to automatically detect the URL of the page and rewrite it accordingly (not only http://www.xyz.com/test/stuff/video129852_zc-7931f8bf_zs-2d7967f4.html but for example also http://www.xyz.com/test/stuff/video12345678_zc-7931f8bf_zs-2d7967f4.html)? And is it possible to erase the part from the "_" to the ".html" without guessing how long it is? – Martin Jun 18 '13 at 10:56
  • By adding:

    javascript:(function() {window.location=window.location.toString().replace(/^http://www.xyz.com/test/stuff/,'http://www.xyz.com/other-directory-thing');})()

    I can at least manipulate the first part of the URL. Any ideas how I can erase the part from the "_" to ".html"?

    – Martin Jun 18 '13 at 13:51
  • var testURL = "url/video234554353453_fG_dfg.html"; var output = testURL.substring(0, testURL.lastIndexOf('_') - 1); console.log(output); This would output up to the end of the video file in the link :) – Ash King Jun 19 '13 at 07:30