You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用JavaScript批量替换Blogspot博客中的旧URL为新URL

解决Blogger批量替换旧域名资源URL的JavaScript脚本

脚本功能

自动遍历博客页面内的图片、视频、嵌入框架等元素,将旧域名123456.com替换为新域名67890.com,无需手动修改每篇帖子。

完整脚本代码

// 配置旧域名和新域名(不要带 http/https)
const oldDomain = '123456.com';
const newDomain = '67890.com';

// 创建正则表达式,匹配包含旧域名的URL(兼容http/https/相对路径)
const domainRegex = new RegExp(oldDomain, 'g');

// 要处理的元素和属性列表
const elementsToProcess = [
  { selector: 'img', attributes: ['src', 'data-src', 'data-lazy-src'] },
  { selector: 'video', attributes: ['src'] },
  { selector: 'iframe', attributes: ['src'] },
  { selector: 'audio', attributes: ['src'] }
];

// 遍历处理每个元素类型
elementsToProcess.forEach(item => {
  const elements = document.querySelectorAll(item.selector);
  elements.forEach(el => {
    item.attributes.forEach(attr => {
      const currentValue = el.getAttribute(attr);
      if (currentValue && currentValue.includes(oldDomain)) {
        const newValue = currentValue.replace(domainRegex, newDomain);
        el.setAttribute(attr, newValue);
      }
    });
  });
});

使用步骤

  1. 登录Blogger后台,进入「主题」页面
  2. 点击「自定义」下拉菜单,选择「编辑HTML」
  3. 在代码编辑器中找到</body>标签
  4. 将上述脚本粘贴到</body>标签的上方
  5. 修改脚本里的oldDomain和newDomain为你的实际旧、新域名
  6. 点击「保存」即可生效

注意事项

  • 脚本会在页面加载时自动执行,对当前页面的所有匹配资源URL进行替换
  • 兼容http和https协议,无需单独处理
  • 如果你的博客使用了其他懒加载属性(比如data-original),可以自行添加到attributes数组中
  • 建议先在测试环境或单篇帖子中验证效果,再应用到整个博客

内容的提问来源于stack exchange,提问作者緑の声

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 06:04:54