开发者

How to replace image links with img src url in Greasemonkey

开发者 https://www.devze.com 2023-02-04 05:57 出处:网络
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\" in the src url, replaces

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" in the src url, replaces "thumbnails" with "images" but then putting the new url into the href (target) url.

What I have so far is:

    for(var iImage=0;iIma开发者_如何学Cge<document.images.length;iImage++){
    var imageUrl = document.images[iImage].src;

    if (imageUrl.indexOf("thumbnails") != -1) {
        imageUrl = imageUrl.replace("thumbnails","images")
        document.images[iImage].href = imageUrl;
    }
}

Any help would be appreciated.


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>';

    }
}


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号