Results 1 to 5 of 5

Thread: parsing and replacing text using javascript

  1. #1
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default parsing and replacing text using javascript

    I'm a rookie when it comes to JS, and I've been able to work with stuff that already exists, but when it comes to starting from scratch I'm a bit lost.

    What I'm trying to accomplish is this..
    I'm writing a script\plugin to be used with tampermonkey (chrome plugin).
    The script will run in the back ground when I'm on lets say website abcd.com

    When I go into that site I click on a link. for example

    http://www.abcd.com/blahA/BlahB/&tp=s

    I want my script to take the link that I click on, parse it and update it instantly so instead of directing me to the above link it takes me to

    http://www.abcd.com/blahA/BlahB/&tp=u

    Notice the bold letter that's what I'm changing in the url and redirecting myself to.
    Now in the example above it's the last character in the URL, however, in my situation that's not the case, it'll be somewhere in the middle of the URL, but basically what I need to do is a replacement of some sort that searches for "tp=s" and replaces it with "tp=u" only within the abcd.com domain.

    How could I go about doing this? Thanks in advance.

  2. #2
    Client Beta Testers Appelpitje's Avatar
    Join Date
    Jan 2012
    Location
    Belgium
    Posts
    571

    Default

    Never made a script in Tampermonkey, but here is how it should look like in javascript:
    HTML Code:
    var url = window.location.href; //the url
    var last = url.substr(url.length - 5); //&tp=s
    var replacewith = "&tp=u";
    
    var result = url.replace(last, replacewith);
    
    window.location.replace(result);
    Last edited by Appelpitje; May 25th, 2015 at 04:35 PM.

  3. #3
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    Does this do this automatically on window load() Appel? Also, I could be wrong, but
    Code:
    var last = url.substr(url.length - 5); //&tp=s
    I don't think this will work, because this is assuming that the last 5 characters will be "&tp=s" however, that was only the case in my example. In the actual link that I am modifying, it will be somewhere in the middle so I would need some sort of search or find function.

    I'll play around with your example though and see what I can do. Thank you for your help!

  4. #4
    Über Prodigy & Developer Razo[R]apiD's Avatar
    Join Date
    May 2010
    Location
    Poland, Lublin
    Posts
    3,257

    Default

    This URL is a href attribute of the link?

    If yes, you can iterate over all <a> tags on the page, check if href.indexOf('&tp=s') >= 0, and if yes href = href.replace('&tp=s', '&tp=u')

    Or


    $('a').on('click', function(e) {
    var href = $(this).attr('href');
    if (href.indexOf('&tp=s') >= 0) {
    href = href.replace('&tp=s', '&tp=u');
    $(this).attr('href', href);
    }
    return true;
    });


    I think should be enough, you need jQuery for this.

  5. #5
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    Thank you guys! I got it working. I'll expand on my idea, but it works perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •