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

Firefox插件自动跳转YouTube Shorts至传统播放器问题求助

解决YouTube Shorts自动跳转问题

你的代码仅在页面首次刷新加载时生效,核心原因是YouTube采用单页应用(SPA)架构:从普通页面导航到Shorts页面时,浏览器不会重新加载整个页面,仅通过前端路由修改URL,你的初始代码不会再次执行。

修正方案

将跳转逻辑封装为独立函数,同时监听所有可能的URL变化场景(初始加载、用户手动导航、脚本触发的页面跳转):

// 封装跳转逻辑,避免循环跳转
function redirectToNormalPlayer() {
  const currUrl = window.location.href;
  // 仅处理Shorts页面,且未跳转到watch页面时执行
  if (currUrl.includes("shorts") && !currUrl.includes("watch?v=")) {
    const vidId = currUrl.split("shorts/")[1].substring(0, 11);
    location.replace(`https://www.youtube.com/watch?v=${vidId}`);
  }
}

// 页面初始加载时执行一次
redirectToNormalPlayer();

// 监听用户手动前进/后退触发的URL变化
window.addEventListener('popstate', redirectToNormalPlayer);

// 重写History API方法,捕获YouTube内部脚本触发的页面跳转
const originalPushState = history.pushState;
history.pushState = function(...args) {
  originalPushState.apply(this, args);
  redirectToNormalPlayer();
};

const originalReplaceState = history.replaceState;
history.replaceState = function(...args) {
  originalReplaceState.apply(this, args);
  redirectToNormalPlayer();
};

关键说明

  1. 避免循环跳转:增加!currUrl.includes("watch?v=")判断,防止跳转到watch页面后重复触发逻辑。
  2. 覆盖所有URL变化场景:
    • 初始加载:处理页面首次打开的情况
    • popstate事件:响应用户点击浏览器前进/后退按钮的操作
    • 重写pushState/replaceState:捕获YouTube内部脚本触发的SPA跳转

内容的提问来源于stack exchange,提问作者Nohahhex

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 18:01:19