On-the-fly modifications of the links according to a set of rules (Greasekit / Javascript)

后端 未结 1 1200
囚心锁ツ
囚心锁ツ 2021-01-07 08:07

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



        
1条回答
  •  再見小時候
    2021-01-07 08:14

    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/

    0 讨论(0)
提交回复
热议问题