MCPcopy Create free account
hub / github.com/XIU2/UserScript / addUrlChangeEvent

Function addUrlChangeEvent

GithubEnhanced-High-Speed-Download.user.js:489–507  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

487
488 // 自定义 urlchange 事件(用来监听 URL 变化),针对非 Tampermonkey 油猴管理器
489 function addUrlChangeEvent() {
490 history.pushState = ( f => function pushState(){
491 var ret = f.apply(this, arguments);
492 window.dispatchEvent(new Event('pushstate'));
493 window.dispatchEvent(new Event('urlchange'));
494 return ret;
495 })(history.pushState);
496
497 history.replaceState = ( f => function replaceState(){
498 var ret = f.apply(this, arguments);
499 window.dispatchEvent(new Event('replacestate'));
500 window.dispatchEvent(new Event('urlchange'));
501 return ret;
502 })(history.replaceState);
503
504 window.addEventListener('popstate',()=>{ // 点击浏览器的前进/后退按钮时触发 urlchange 事件
505 window.dispatchEvent(new Event('urlchange'))
506 });
507 }
508})();

Calls

no outgoing calls

Tested by

no test coverage detected