1

I have an HTML page with loads of entries like this:

<a href="https://www.example.co.uk/gp/wine/product?ie=UTF8&amp;asin=123456789&amp;tab=UK_Default" class="PrmryBtnMed"

I want to replace all this links so they instead are:

https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=1233456789

So, it's quite a complicated search and replace. These are the instructions for a human:

  1. Look at the URL. Only make a note of the number after 'asin='. (Forget everything before that and everything after that)
  2. Then, form a new URL, using this ASIN. It will ALWAYS start like this: https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=

With the number stuck on the end to form:

https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=123456789

Kindly note

  • rather than modifying the existing buttons, it would also be acceptable to add a new button [or link] near the original buttons
  • both the original and new links point to the same domain
  • I'm using Greasekit on a SSB called FluidApp, but I can switch to Greasemonkey on FireFox.

I've just watched 40 JavaScript tutorial videos - man this language is hard! This seems extremely difficult. I would hugely appreciate any help/pointers.

Marcel Gwerder
  • 8,353
  • 5
  • 35
  • 60
Hiya
  • 175
  • 1
  • 2
  • 9

1 Answers1

1

Something like this might work:

// the new base url
var base = ' https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=';
// all the links with className 'PrmryBtnMed'
var links  = document.getElementsByTagName('a');

for(var i = 0;i < links.length;i++){
    // check each link for the 'asin' value
    var result = /asin=([\d\w]+)/.exec(links[i].getAttribute('href'));
    if(result){
        // make a new url using the 'base' and the 'asin' value
        links[i].setAttribute('href', base+result[1]);
    }
}

Demo: http://jsfiddle.net/louisbros/L8ePL/

louisbros
  • 865
  • 5
  • 10
  • **Thank you, wonderful person**! we are off to a running start, but there are two problems. (I shall post the two problems as **separate comments** to make it more readable) – Hiya Apr 19 '13 at 19:30
  • **Problem 1** It only seems to modify **one button** on the page - the first one. Huh, maybe we should try a new way of finding the buttons? We could approach this from a fresh angle: How about looking for buttons that have the link `Send me this item` (instead of looking by the class `PrmryBtnMed`). Come to think of it, _even better_ would be to modify _every_ link on the page, perhaps narrowing it down to only links that contains the text `asin` in it. (If this is feasible, it would also catch regular links and image links on the page, which would be icing on the cake) – Hiya Apr 19 '13 at 19:32
  • **problem 2 - the link has some extra characters in it:** `https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=123456789` It should display as : `http://www.example.co.uk/gp/wine/order?ie=UTF8&asin=123456789` In other words, it says `&amp` when I only want it to say `&` – Hiya Apr 19 '13 at 19:33
  • Ok I've updated the demo to look for every link and not just the ones with the class name `PrmryBtnMed`. Also I've removed the `&`. Let me know if that works. – louisbros Apr 19 '13 at 21:10
  • Thanks, **problem 2 is solved** - hooray! (Thank you so much!) However, problem 1 still remains in that it only works for the **first row of the table**. (It does work on all the links within that row). In other words after `` it stops working. – Hiya Apr 24 '13 at 11:47
  • It should work. If you give me the address of the site you're working on I could help further. – louisbros Apr 24 '13 at 21:32
  • The site is password protected,perhaps I should mirror the HTML on another site. I'll just get some hosting and do that. :) – Hiya Apr 25 '13 at 05:45
  • This is the URL of the site: http://albany.net23.net/newsletter.html (as the actual site is password protected etc, i've mirrored it) – Hiya Apr 25 '13 at 07:43
  • ok updated. Problem was that the values of `asin` are a mix of numbers and letters and not just numbers as in your example :) – louisbros Apr 25 '13 at 08:29
  • Ah, I see. Yes, sometimes, it's a mix of letters and numbers. More examples of the `asin` are `B006MOS60Y` and `B003EM8008`. Did you update the code? (it says last edited on 19 April...) – Hiya Apr 25 '13 at 09:14
  • Updated. I've tested it on your site and it worked for me. Let me know if it works and if it does, consider accepting the answer. – louisbros Apr 25 '13 at 09:18
  • it's perfect; you are a gentleman and a scholar. Answer accepted, with thanks. ( I shall also give you upvotes as soon as I am able to.) – Hiya Apr 25 '13 at 09:43
  • Ha ha glad we got there in the end. – louisbros Apr 25 '13 at 09:45
  • Oh, is there a tweak I can make so that link opens **in a new window**, when clicked? [if not, I can just right-click] – Hiya May 22 '13 at 14:51
  • Hey man, I seem to be locked out of yahoo mail. I keep getting locket out, for 12 hours at a time. (I set a security question back in 2010, it asked me for it out of the blue). Hopefully, I'll be back in within a few days. – Hiya Jun 10 '13 at 21:13