From 7afbc533cd45bebb3397830dc1b368c0e93bab70 Mon Sep 17 00:00:00 2001 From: xiu2 <54703944+XIU2@users.noreply.github.com> Date: Wed, 12 Jan 2022 09:56:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E4=BB=A3=E7=A0=81=20(?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20Tampermonkey=20=E7=9A=84=20window.onurlcha?= =?UTF-8?q?nge=20=E4=BD=9C=E4=B8=BA=E4=B8=BB=E9=80=89=E6=96=B9=E6=A1=88)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Autopage.user.js | 17 ++++++------ GithubEnhanced-High-Speed-Download.user.js | 19 ++++++------- Zhihu-Enhanced.user.js | 31 +++++++++++----------- 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/Autopage.user.js b/Autopage.user.js index 876317a..76aff1c 100644 --- a/Autopage.user.js +++ b/Autopage.user.js @@ -18,6 +18,7 @@ // @grant GM_setValue // @grant GM_deleteValue // @grant GM_notification +// @grant window.onurlchange // @grant unsafeWindow // @license GPL-3.0 License // @run-at document-end @@ -5727,9 +5728,9 @@ function: { // 对于使用 pjax 技术的网站,需要监听 URL 变化来重新判断翻页规则 if (locationC) { nowLocation = location.href - addLocationchange(); // 自定义 locationChange 事件 + if (window.onurlchange === undefined) {addUrlChangeEvent();} // Tampermonkey v4.11 版本添加的 onurlchange 事件 grant,可以监控 pjax 等网页的 URL 变化 if (webType === 1) { - window.addEventListener('locationChange', function(){ + window.addEventListener('urlchange', function(){ lp = location.pathname; //console.log(nowLocation, location.href) if (curSite.history && window.top.document.xiu_nowUrl === location.href) {nowLocation = location.href; return} @@ -5749,7 +5750,7 @@ function: { pausePageEvent(); // 左键双击网页空白处暂停翻页 }) } else if (webType === 3) { - window.addEventListener('locationChange', function(){ + window.addEventListener('urlchange', function(){ lp = location.pathname; if (nowLocation == location.href) return nowLocation = location.href; curSite = {SiteTypeID: 0}; pageNum.now = 1; // 重置规则+页码 @@ -7552,24 +7553,24 @@ function: { }, false); }, 1000) } - // 自定义 locationChange 事件(用来监听 URL 变化) - function addLocationchange() { + // 自定义 urlchange 事件(用来监听 URL 变化) + function addUrlChangeEvent() { history.pushState = ( f => function pushState(){ var ret = f.apply(this, arguments); window.dispatchEvent(new Event('pushstate')); - window.dispatchEvent(new Event('locationChange')); + window.dispatchEvent(new Event('urlchange')); return ret; })(history.pushState); history.replaceState = ( f => function replaceState(){ var ret = f.apply(this, arguments); window.dispatchEvent(new Event('replacestate')); - window.dispatchEvent(new Event('locationChange')); + window.dispatchEvent(new Event('urlchange')); return ret; })(history.replaceState); window.addEventListener('popstate',()=>{ - window.dispatchEvent(new Event('locationChange')) + window.dispatchEvent(new Event('urlchange')) }); } })(); \ No newline at end of file diff --git a/GithubEnhanced-High-Speed-Download.user.js b/GithubEnhanced-High-Speed-Download.user.js index ec0df76..e61530c 100644 --- a/GithubEnhanced-High-Speed-Download.user.js +++ b/GithubEnhanced-High-Speed-Download.user.js @@ -12,6 +12,7 @@ // @grant GM_getValue // @grant GM_setValue // @grant GM_notification +// @grant window.onurlchange // @license GPL-3.0 License // @run-at document-end // @namespace https://greasyfork.org/scripts/412245 @@ -120,10 +121,10 @@ setTimeout(addRawDownLink, 2000); // 添加 Raw 下载链接(☁),延迟 2 秒执行,避免被 pjax 刷掉 }); - addLocationchange(); - window.addEventListener('locationchange', function(){ - addRawDownLink_(); // 在浏览器返回/前进时重新添加 Raw 下载链接(☁)事件 - }) + // 在浏览器返回/前进时重新添加 Raw 下载链接(☁)事件 + // Tampermonkey v4.11 版本添加的 onurlchange 事件 grant,可以监控 pjax 等网页的 URL 变化 + if (window.onurlchange === undefined) {addUrlChangeEvent();} + window.addEventListener('urlchange', function() {addRawDownLink_();}); // Release @@ -337,24 +338,24 @@ } - // 自定义 locationchange 事件(用来监听 URL 变化) - function addLocationchange() { + // 自定义 urlchange 事件(用来监听 URL 变化) + function addUrlChangeEvent() { history.pushState = ( f => function pushState(){ var ret = f.apply(this, arguments); window.dispatchEvent(new Event('pushstate')); - window.dispatchEvent(new Event('locationchange')); + window.dispatchEvent(new Event('urlchange')); return ret; })(history.pushState); history.replaceState = ( f => function replaceState(){ var ret = f.apply(this, arguments); window.dispatchEvent(new Event('replacestate')); - window.dispatchEvent(new Event('locationchange')); + window.dispatchEvent(new Event('urlchange')); return ret; })(history.replaceState); window.addEventListener('popstate',()=>{ - window.dispatchEvent(new Event('locationchange')) + window.dispatchEvent(new Event('urlchange')) }); } })(); \ No newline at end of file diff --git a/Zhihu-Enhanced.user.js b/Zhihu-Enhanced.user.js index 7d78bc8..c11149f 100644 --- a/Zhihu-Enhanced.user.js +++ b/Zhihu-Enhanced.user.js @@ -14,6 +14,7 @@ // @grant GM_setValue // @grant GM_notification // @grant GM_info +// @grant window.onurlchange // @license GPL-3.0 License // @run-at document-end // @incompatible safari @@ -169,7 +170,7 @@ function getCollapsedAnswerObserver() { } } - window.addEventListener('locationchange', function() { + window.addEventListener('urlchange', function() { observer[location.href.indexOf('/answer/') === -1 ? 'start' : 'end'](); }) window._collapsedAnswerObserver = observer; @@ -222,7 +223,7 @@ function collapsedAnswer() { const observer = getCollapsedAnswerObserver(); observer.start(); if (!menu_value('menu_defaultCollapsedAnswer') && !observer._disconnectListener) { - window.addEventListener('locationchange', function() { + window.addEventListener('urlchange', function() { observer.end(); window._collapsedAnswerObserver = null; }) @@ -435,7 +436,7 @@ function blockUsers(type) { } blockKeywords_now(); - window.addEventListener('locationchange', function(){ + window.addEventListener('urlchange', function(){ setTimeout(blockKeywords_now, 500); // 网页 URL 变化后再次执行 }) @@ -543,7 +544,7 @@ function blockUsers(type) { } setTimeout(blockUsers_now, 500); - window.addEventListener('locationchange', function(){ + window.addEventListener('urlchange', function(){ setTimeout(blockUsers_now, 500); // 网页 URL 变化后再次执行 }) @@ -753,7 +754,7 @@ function blockKeywords(type) { } blockKeywords_now(); - window.addEventListener('locationchange', function(){ + window.addEventListener('urlchange', function(){ setTimeout(blockKeywords_now, 500); // 网页 URL 变化后再次执行 }) @@ -778,7 +779,7 @@ function blockKeywords(type) { } setTimeout(blockKeywords_now, 500); - window.addEventListener('locationchange', function(){ + window.addEventListener('urlchange', function(){ setTimeout(blockKeywords_now, 500); // 网页 URL 变化后再次执行 }) @@ -882,7 +883,7 @@ function blockType(type) { }); observer.observe(document, { childList: true, subtree: true }); - window.addEventListener('locationchange', function(){ + window.addEventListener('urlchange', function(){ addSetInterval_(name); // 移除相关搜索 if (menu_value('menu_blockTypeSearch') && location.pathname === '/search' && location.search.indexOf('type=content') > -1) setTimeout(function(){document.querySelector('.RelevantQuery').parentElement.parentElement.hidden = true;}, 1500) @@ -1056,7 +1057,7 @@ function addToQuestion() { }); observer.observe(document, { childList: true, subtree: true }); - window.addEventListener('locationchange', function(){ + window.addEventListener('urlchange', function(){ addSetInterval_('h2.ContentItem-title a:not(.zhihu_e_tips)'); }) @@ -1193,24 +1194,24 @@ function closeFloatingComments() { }*/ -// 自定义 locationchange 事件(用来监听 URL 变化) -function addLocationchange() { +// 自定义 urlchange 事件(用来监听 URL 变化) +function addUrlChangeEvent() { history.pushState = ( f => function pushState(){ var ret = f.apply(this, arguments); window.dispatchEvent(new Event('pushstate')); - window.dispatchEvent(new Event('locationchange')); + window.dispatchEvent(new Event('urlchange')); return ret; })(history.pushState); history.replaceState = ( f => function replaceState(){ var ret = f.apply(this, arguments); window.dispatchEvent(new Event('replacestate')); - window.dispatchEvent(new Event('locationchange')); + window.dispatchEvent(new Event('urlchange')); return ret; })(history.replaceState); window.addEventListener('popstate',()=>{ - window.dispatchEvent(new Event('locationchange')) + window.dispatchEvent(new Event('urlchange')) }); } @@ -1331,11 +1332,11 @@ function questionInvitation(){ (function() { - addLocationchange(); + if (window.onurlchange === undefined) {addUrlChangeEvent();} // Tampermonkey v4.11 版本添加的 onurlchange 事件 grant,可以监控 pjax 等网页的 URL 变化 removeLogin(); // 移除登录弹窗 setInterval(originalPic,100); // 默认高清原图 setInterval(directLink, 100); // 默认站外直链 - window.addEventListener('locationchange', function(){ // 针对的是从单个回答页跳转到完整回答页时 + window.addEventListener('urlchange', function(){ // 针对的是从单个回答页跳转到完整回答页时 if (location.pathname.indexOf('question') > -1 && location.pathname.indexOf('waiting') === -1 && location.pathname.indexOf('/answer/') === -1) { // 回答页 // setTimeout(function(){ collapsedNowAnswer('.QuestionPage'); // 收起当前回答 + 快捷返回顶部