// 监听 URL 变化
const originalPushState = history.pushState;
const originalReplaceState = history.replaceState;
let lastUrl = window.location.href;

function handleUrlChange() {
  if (lastUrl !== window.location.href) {
    console.log("URL 发生变化:", window.location.href);
    window.location.reload();
  }
}

// 重写 pushState 和 replaceState 以监听 URL 改变
history.pushState = function (...args) {
  originalPushState.apply(history, args);
  handleUrlChange();
};

history.replaceState = function (...args) {
  originalReplaceState.apply(history, args);
  handleUrlChange();
};

// 监听浏览器的 popstate 事件(即用户通过浏览器的后退/前进按钮改变 URL)
window.addEventListener("popstate", handleUrlChange);

标签: js, chrome, url

仅有一条评论

  1. 真棒!

添加新评论