简体   繁体   English

在鼠标悬停时替换部分URL,以获取原始大小的图像

[英]Replace part of URL during mouse hover to get original size image

For Violentmonkey or Greasemonkey. 对于Violentmonkey或Greasemonkey。

I want to replace part of a URL during mouse hover , so that I could copy to memory the original size of the image instead of the provided one. 我想在鼠标悬停时替换URL的一部分,以便可以将图像的原始大小(而不是提供的大小)复制到内存中。 The image URL patterns are constant. 图片网址格式是恒定的。 The desired texts to be replaced are always on the same locations, perhaps like a template. 所需替换的文本始终位于相同的位置,例如模板。

Example of a blogspot.com page: blogspot.com页面的示例:

https://hopedogrescue.blogspot.com/2019/06/what-you-need-to-know-about-canine.html https://hopedogrescue.blogspot.com/2019/06/what-you-need-to-know-about-canine.html

Example of an image URL from the above page: 上一页中的图片网址示例:

https://1.bp.blogspot.com/-k-J3eJs1nH0/XQg9ZTO9i2I/AAAAAAAAVXI/MJijXqZxrLQt9xmoK-mHlAJPrtFe7SM4ACLcBGAs/s1600/matthieu%2Bserious.jpg https://1.bp.blogspot.com/-k-J3eJs1nH0/XQg9ZTO9i2I/AAAAAAAAVXI/MJijXqZxrLQt9xmoK-mHlAJPrtFe7SM4ACLcBGAs/s1600/matthieu%2Bserious.jpg

Example of the image URL from above, now s1600 is changed to s0 : 上面的图片网址示例,现在s1600更改为s0:

https://1.bp.blogspot.com/-k-J3eJs1nH0/XQg9ZTO9i2I/AAAAAAAAVXI/MJijXqZxrLQt9xmoK-mHlAJPrtFe7SM4ACLcBGAs/s0/matthieu%2Bserious.jpg https://1.bp.blogspot.com/-k-J3eJs1nH0/XQg9ZTO9i2I/AAAAAAAAVXI/MJijXqZxrLQt9xmoK-mHlAJPrtFe7SM4ACLcBGAs/s0/matthieu%2Bserious.jpg

If I am in a forum or whatever page, I want whatever existing image posted there by a User as it was. 如果我在论坛中或任何页面中,我希望用户在此张贴任何现有图像。 I want the URL for the image's original size during mouse hover, so that I could save internet data. 我想要鼠标悬停时图像原始大小的URL,以便可以保存互联网数据。 My downloader will intercept the image's URL from memory when copied. 复制后,我的下载器将从内存中拦截图像的URL。 Hence, I could repeatedly revisit the forum/page with its existing images as they were. 因此,我可以使用其现有图像反复访问论坛/页面。

So far, the s1600 is not changed to s0 during mouse hover. 到目前为止,鼠标悬停时s1600尚未更改为s0。

// ==UserScript==
// @name        My orig image sizes
// @description Get orig image sizes.
// @namespace   My monkey scripts
// @version     1.0
// @grant       none
// @match       *://*.blogspot.com/*
// @run-at      document-start
// ==/UserScript==

document.addEventListener("mouseover", myFunction);

function myFunction() {
  var url = 'https:\/\/.\.bp.blogspot.com\/.\/.\/.\/.\/s1600\/.\.jpg'
  url = url.replace('s1600','s0')
}

You could handle this by DOM references, which is actually how JavaScript works when it has to modify or reference the DOM. 您可以通过DOM引用来处理此问题,这实际上是JavaScript在必须修改或引用DOM时的工作方式。

 const images = document.querySelectorAll("img"); let imagesData = []; images.forEach((i,x)=>{ imagesData.push(i.src); //Saves every DOM reference of an img tag i.addEventListener("mouseover", ()=>{toggleImage(x,true);}); i.addEventListener("mouseout", ()=>{toggleImage(x,false);}); }); function toggleImage(e,h) { images[e].src = h ? "//placehold.it/150/00F/FFF/?text=Hover" : imagesData[e]; } 
 <img id="img1" src="//placehold.it/150/600/FFF/?text=Image 1"> <img id="img2" src="//placehold.it/150/A00/FFF/?text=Image 2"> <img id="img3" src="//placehold.it/150/F00/FFF/?text=Image 3"> <img id="img4" src="//placehold.it/150/C00/FFF/?text=Image 4"> 

Note : If you want to replace a specific url depending on the previous one, you can get it with images[e].src inside toggleImage function. 注意 :如果要根据上一个URL替换特定的URL,可以使用toggleImage函数中的images[e].src来获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM