Puo's 菜园子 学习园地 http://puo.cn
易记网址: http://wordpress.cn.com

google blogger 图片加速代码

<script type='text/javascript'>
//<![CDATA[
setTimeout(function() {
// 1. 保留:处理 <img> 标签(文章内图片)
const googleImages = document.querySelectorAll('img[src*="googleusercontent.com"], img[src*="ggpht.com"]');
googleImages.forEach(img => {
if (!img.src.includes('images.weserv.nl')) {
img.src = 'https://images.weserv.nl/?url=' + encodeURIComponent(img.src);
img.onerror = () => img.src = 'https://picsum.photos/400/300';
}
});

// 2. 保留:处理 <meta> 标签(首页预览图)
const googleMetaImages = document.querySelectorAll('meta[content*="googleusercontent.com"], meta[content*="ggpht.com"]');
googleMetaImages.forEach(meta => {
const content = meta.getAttribute('content');
if (content && !content.includes('images.weserv.nl')) {
meta.setAttribute('content', 'https://images.weserv.nl/?url=' + encodeURIComponent(content));
}
});

// 3. 新增:处理 CSS 背景图(首页轮播图等)
const elementsWithBg = document.querySelectorAll ('*'); // 遍历所有元素
elementsWithBg.forEach(el => {
const bgStyle = window.getComputedStyle(el).backgroundImage;
// 匹配包含谷歌图片链接的背景图
if (bgStyle.includes('googleusercontent.com') || bgStyle.includes('ggpht.com')) {
// 提取背景图 URL(处理 URL 引号包裹的情况)
const urlMatch = bgStyle.match(/url\(['"]?(.*?)['"]?\)/);
if (urlMatch && urlMatch[1] && !urlMatch[1].includes('images.weserv.nl')) {
const originalUrl = urlMatch[1];
const proxiedUrl = 'https://images.weserv.nl/?url=' + encodeURIComponent(originalUrl);
// 重新设置背景图
el.style.backgroundImage = `url('${proxiedUrl}')`;
}
}
});

}, 1000); // 延长延迟到 1000ms,确保 CSS 样式完全加载
//]]>
</script>

把这段代码放在谷歌博客的任意模板头部 < head > 里面,网站访问速度 图片加载速度杠杠的,模板还可以随便修改,完全免费的无限储存空间。

打赏
谢谢谅解上文的粗糙,允许转载,请注明转载地址:Puo's 菜园子 » google blogger 图片加速代码
分享到

评论 抢沙发

做一个好的个人学习园地

主要网建,域名、集装箱物流、生活方法论的学习及研究,整理等内容

我的原创博客-忆秋年Puo's菜园子-我的学习园地

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册