获取加载图片的随机url的源代码

dzjeubhm  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(219)

我正在写一个网站,我加载这个链接(https://source.unsplash.com/random/1920x1080)为了得到一张随机的照片。链接后面的照片会定期更改,当它加载到站点时,我只看到这个链接https://source.unsplash.com/random/1920x1080,您认为有没有办法获得加载图像的真实来源,如下所示:https://images.unsplash.com/photo-1458571037713-913d8b481dc6?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=1080&ixid=eyjhchbfawqiojf9&ixlib=rb-1.2.1&q=80&w=1920”。当我打开chrome开发工具查看实际链接时,我只看到保存链接-https://source.unsplash.com/random/1920x1080.
每当我重新加载页面时,我会得到新的图像,有时我想保存加载的图像链接以备将来使用。

kb5ga3dv

kb5ga3dv1#

我唯一想到的就是给你所有的图片一个 randomImg 类,然后手动加载其源。通过这种方式,源被设置为实际的源,而不是重定向您的链接。
只需将此脚本标记添加到页面底部。还请注意,这可能会减慢图像的显示速度。

<img id="img" class="randomImg" src=""/>
<button onClick="console.log(document.getElementById('img').src)">Get src</button>

<script>
const randomImages =  document.getElementsByClassName('randomImg');
for (let i = 0; i < randomImages.length; i++) {
    fetch('https://source.unsplash.com/random/1920x1080').then(data => {
        randomImages[i].src = data.url;
    });
};
</script>

另外请注意,如果要手动保存源,可以在chrome devtools的“源”选项卡中轻松找到图像源。

相关问题