使用iMacros批量替换HTML图片src属性的技术求助
Got it, let's figure out how to update those image sources using iMacros! There are two solid approaches depending on whether you know the exact number of images in the outerd3 div or need a flexible, dynamic solution.
Approach 1: Fixed Number of Images (3 in your case)
If you're certain there will always be 3 images inside the div, this straightforward script will get the job done quickly:
VERSION BUILD=1002 RECORDER=CR ' Replace src of the first image in outerd3 TAG POS=1 TYPE=IMG ATTR=PARENT:DIV@ID:outerd3 CONTENT=EVENT:SETATTRIBUTE(src,"avatar.jpg") ' Replace src of the second image TAG POS=2 TYPE=IMG ATTR=PARENT:DIV@ID:outerd3 CONTENT=EVENT:SETATTRIBUTE(src,"avatar.jpg") ' Replace src of the third image TAG POS=3 TYPE=IMG ATTR=PARENT:DIV@ID:outerd3 CONTENT=EVENT:SETATTRIBUTE(src,"avatar.jpg")
Each TAG line targets an image by its position within the outerd3 div, then uses SETATTRIBUTE to overwrite the src value to avatar.jpg. Super simple and reliable for fixed counts.
Approach 2: Dynamic Solution (Works for Any Number of Images)
If the number of images might change later, use this script that automatically counts and updates all images in the div:
VERSION BUILD=1002 RECORDER=CR SET !ERRORIGNORE YES SET !LOOP 1 ' Extract the HTML of the outerd3 div and count how many img tags it has TAG POS=1 TYPE=DIV ATTR=ID:outerd3 EXTRACT=HTM SET imgCount EVAL("var html = '{{!EXTRACT}}'; html.match(/<img/g) ? html.match(/<img/g).length : 0;") ' Loop through each image and update its src TAG POS={{!LOOP}} TYPE=IMG ATTR=PARENT:DIV@ID:outerd3 CONTENT=EVENT:SETATTRIBUTE(src,"avatar.jpg") ' Stop the loop once all images are processed IF {{!LOOP}} EQUAL {{imgCount}} SET !LOOP {{imgCount}}
Here's a quick breakdown of how this works:
EXTRACT=HTMpulls the full HTML content of theouterd3div.- The
EVALcommand uses a regex match to count how many<img>tags are present in that HTML. - The loop runs once for each image, updating the
srcattribute every time. - The final
IFstatement ensures the loop stops after processing all images, so you don't waste extra iterations.
Just tweak the VERSION line to match your iMacros build (this example uses the Chrome version), then run the script—your images will all point to avatar.jpg in no time!
内容的提问来源于stack exchange,提问作者CherylAN

