How to replace image links with img src url in Greasemonkey

前端 未结 2 477
南方客
南方客 2021-01-23 23:28

From the title this may sound like a duplicate question. But what I am asking for is help writing a Greasemonkey script that takes all images containing the word \"thumbnails\"

相关标签:
2条回答
  • 2021-01-24 00:18

    img tags cant have href, however you can append them into an anchor tag with href attribute:

    for(var iImage=0;iImage<document.images.length;iImage++){
        var imageUrl = document.images[iImage].src;
    
        if (imageUrl.indexOf("thumbnails") != -1) {
            imageUrl = imageUrl.replace("thumbnails","images");
            document.images[iImage].outerHTML = '<a href ="' +
                                  + imageUrl + '" >' 
                                  + document.images[iImage].outerHTML + '</a>';
    
        }
    }
    
    0 讨论(0)
  • 2021-01-24 00:19

    Psuedo-code:

    var thumblinks=new Array();
    for(x=0;x<links.length;x++){
        if(links[x].href.test('thumbnails'))thumblinks[thunblinks.length]=links[x];
    }
    for(x=0;x<thumblinks.length;x++){
        thumblinks[x].href=thumblinks[x].firstChild.src;
    }
    

    Does this work?

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