This is a variation of an existing question
{Please note - this question is the opposite/inverse of an existing StackOverflow question that is too complex to answer there}
From the very beginning
I want to make some refinement to some code from this challenge:
// the new base url
var base = ' https://www.example.co.uk/gp/wine/order?ie=UTF8&asin=';
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]);
}
}
Now, instead of it acting on all links, can I get it to only look at links that are from text?
Here is an HTML snippet to show what I mean:
<a href="/shop/product?ie=UTF8&asin=Z00FDLN878&tab=UK_Default" target="_blank"><img width="125" height="125" border="0" src="http://ecx.images-amazon.com/images/I/01W9a7gwosL.jpg" alt="43453"></a>
That's an image link - I do NOT want it to act on that.
To support older browsers, call
getElementsByTagName()
instead (and check for an empty array).In straight javascript / DOM, test the links as you loop through them:
jQuery lets you preselect only the correct links:
But this requires you to load jQuery in Greasekit -- which is well worth it for any but the simplest scripts.