2011-06-22 85 views
1

可能重複:
How to replace image links with img src url in Greasemonkey的GreaseMonkey腳本改寫圖像鏈接

我是新來的javacript並想編寫一個腳本的GreaseMonkey重寫網站上的某些圖像鏈接顯示原始大小的圖像,而不是縮略圖。

的圖像鏈接在下面的形式:

<div class="feed_img"><a onclick="App.scaleImg(this,'7e87e5d5tw1difluxvqlzj');" href="javascript:;"><img src="http://ww3.sinaimg.cn/thumbnail/7e87e5d5tw1difluxvqlzj.jpg" class="imgicon" vimg="1"></a></div> 

我想要做的就是更換

http://*/thumbnail/*.jpg 

http://*/large/*.jpg 

誰能給我一個帶頭作用?

+0

你到目前爲止嘗試過什麼?搏一搏。如果您是JavaScript新手,這是一個很好的練習,如果您自己將解決方案組合在一起,您將學到更多。 –

回答

2

你不得不遍歷所有<img>標籤,只是與large他們src屬性替換thumbnail

// This fetches all of the <img> tags and stores them in "tags". 
var tags = document.getElementsByTagName('img'); 

// This loops over all of the <img> tags. 
for (var i = 0; i < tags.length; i++) { 

    // This replaces the src attribute of the tag with the modified one 
    tags[i].src = tags[i].src.replace('thumbnail', 'large'); 
} 

希望此代碼的工作。我使用的是基本的replace(),所以如果你想嘗試正則表達式,我認爲它也應該起作用。

+0

對我來說看起來不錯,我不覺得在這種情況下使用正則表達式有什麼好處。 – Jonathon

+0

這將會把頁面上的所有圖像都燒掉!我想象OP只是想篡改「有效載荷」圖像。 –

+0

@Brock:您可以添加一些檢查來篩選不需要的鏈接。我不寫*全部* OP的代碼... – Blender

相關問題