Skip to content

Commit 7afbc53

Browse files
committed
优化 代码 (使用 Tampermonkey 的 window.onurlchange 作为主选方案)
1 parent c48a723 commit 7afbc53

3 files changed

Lines changed: 35 additions & 32 deletions

File tree

Autopage.user.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// @grant GM_setValue
1919
// @grant GM_deleteValue
2020
// @grant GM_notification
21+
// @grant window.onurlchange
2122
// @grant unsafeWindow
2223
// @license GPL-3.0 License
2324
// @run-at document-end
@@ -5727,9 +5728,9 @@ function: {
57275728
// 对于使用 pjax 技术的网站,需要监听 URL 变化来重新判断翻页规则
57285729
if (locationC) {
57295730
nowLocation = location.href
5730-
addLocationchange(); // 自定义 locationChange 事件
5731+
if (window.onurlchange === undefined) {addUrlChangeEvent();} // Tampermonkey v4.11 版本添加的 onurlchange 事件 grant,可以监控 pjax 等网页的 URL 变化
57315732
if (webType === 1) {
5732-
window.addEventListener('locationChange', function(){
5733+
window.addEventListener('urlchange', function(){
57335734
lp = location.pathname;
57345735
//console.log(nowLocation, location.href)
57355736
if (curSite.history && window.top.document.xiu_nowUrl === location.href) {nowLocation = location.href; return}
@@ -5749,7 +5750,7 @@ function: {
57495750
pausePageEvent(); // 左键双击网页空白处暂停翻页
57505751
})
57515752
} else if (webType === 3) {
5752-
window.addEventListener('locationChange', function(){
5753+
window.addEventListener('urlchange', function(){
57535754
lp = location.pathname;
57545755
if (nowLocation == location.href) return
57555756
nowLocation = location.href; curSite = {SiteTypeID: 0}; pageNum.now = 1; // 重置规则+页码
@@ -7552,24 +7553,24 @@ function: {
75527553
}, false);
75537554
}, 1000)
75547555
}
7555-
// 自定义 locationChange 事件(用来监听 URL 变化)
7556-
function addLocationchange() {
7556+
// 自定义 urlchange 事件(用来监听 URL 变化)
7557+
function addUrlChangeEvent() {
75577558
history.pushState = ( f => function pushState(){
75587559
var ret = f.apply(this, arguments);
75597560
window.dispatchEvent(new Event('pushstate'));
7560-
window.dispatchEvent(new Event('locationChange'));
7561+
window.dispatchEvent(new Event('urlchange'));
75617562
return ret;
75627563
})(history.pushState);
75637564

75647565
history.replaceState = ( f => function replaceState(){
75657566
var ret = f.apply(this, arguments);
75667567
window.dispatchEvent(new Event('replacestate'));
7567-
window.dispatchEvent(new Event('locationChange'));
7568+
window.dispatchEvent(new Event('urlchange'));
75687569
return ret;
75697570
})(history.replaceState);
75707571

75717572
window.addEventListener('popstate',()=>{
7572-
window.dispatchEvent(new Event('locationChange'))
7573+
window.dispatchEvent(new Event('urlchange'))
75737574
});
75747575
}
75757576
})();

GithubEnhanced-High-Speed-Download.user.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// @grant GM_getValue
1313
// @grant GM_setValue
1414
// @grant GM_notification
15+
// @grant window.onurlchange
1516
// @license GPL-3.0 License
1617
// @run-at document-end
1718
// @namespace https://greasyfork.org/scripts/412245
@@ -120,10 +121,10 @@
120121
setTimeout(addRawDownLink, 2000); // 添加 Raw 下载链接(☁),延迟 2 秒执行,避免被 pjax 刷掉
121122
});
122123

123-
addLocationchange();
124-
window.addEventListener('locationchange', function(){
125-
addRawDownLink_(); // 在浏览器返回/前进时重新添加 Raw 下载链接(☁)事件
126-
})
124+
// 在浏览器返回/前进时重新添加 Raw 下载链接(☁)事件
125+
// Tampermonkey v4.11 版本添加的 onurlchange 事件 grant,可以监控 pjax 等网页的 URL 变化
126+
if (window.onurlchange === undefined) {addUrlChangeEvent();}
127+
window.addEventListener('urlchange', function() {addRawDownLink_();});
127128

128129

129130
// Release
@@ -337,24 +338,24 @@
337338
}
338339

339340

340-
// 自定义 locationchange 事件(用来监听 URL 变化)
341-
function addLocationchange() {
341+
// 自定义 urlchange 事件(用来监听 URL 变化)
342+
function addUrlChangeEvent() {
342343
history.pushState = ( f => function pushState(){
343344
var ret = f.apply(this, arguments);
344345
window.dispatchEvent(new Event('pushstate'));
345-
window.dispatchEvent(new Event('locationchange'));
346+
window.dispatchEvent(new Event('urlchange'));
346347
return ret;
347348
})(history.pushState);
348349

349350
history.replaceState = ( f => function replaceState(){
350351
var ret = f.apply(this, arguments);
351352
window.dispatchEvent(new Event('replacestate'));
352-
window.dispatchEvent(new Event('locationchange'));
353+
window.dispatchEvent(new Event('urlchange'));
353354
return ret;
354355
})(history.replaceState);
355356

356357
window.addEventListener('popstate',()=>{
357-
window.dispatchEvent(new Event('locationchange'))
358+
window.dispatchEvent(new Event('urlchange'))
358359
});
359360
}
360361
})();

Zhihu-Enhanced.user.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// @grant GM_setValue
1515
// @grant GM_notification
1616
// @grant GM_info
17+
// @grant window.onurlchange
1718
// @license GPL-3.0 License
1819
// @run-at document-end
1920
// @incompatible safari
@@ -169,7 +170,7 @@ function getCollapsedAnswerObserver() {
169170
}
170171
}
171172

172-
window.addEventListener('locationchange', function() {
173+
window.addEventListener('urlchange', function() {
173174
observer[location.href.indexOf('/answer/') === -1 ? 'start' : 'end']();
174175
})
175176
window._collapsedAnswerObserver = observer;
@@ -222,7 +223,7 @@ function collapsedAnswer() {
222223
const observer = getCollapsedAnswerObserver();
223224
observer.start();
224225
if (!menu_value('menu_defaultCollapsedAnswer') && !observer._disconnectListener) {
225-
window.addEventListener('locationchange', function() {
226+
window.addEventListener('urlchange', function() {
226227
observer.end();
227228
window._collapsedAnswerObserver = null;
228229
})
@@ -435,7 +436,7 @@ function blockUsers(type) {
435436
}
436437

437438
blockKeywords_now();
438-
window.addEventListener('locationchange', function(){
439+
window.addEventListener('urlchange', function(){
439440
setTimeout(blockKeywords_now, 500); // 网页 URL 变化后再次执行
440441
})
441442

@@ -543,7 +544,7 @@ function blockUsers(type) {
543544
}
544545

545546
setTimeout(blockUsers_now, 500);
546-
window.addEventListener('locationchange', function(){
547+
window.addEventListener('urlchange', function(){
547548
setTimeout(blockUsers_now, 500); // 网页 URL 变化后再次执行
548549
})
549550

@@ -753,7 +754,7 @@ function blockKeywords(type) {
753754
}
754755

755756
blockKeywords_now();
756-
window.addEventListener('locationchange', function(){
757+
window.addEventListener('urlchange', function(){
757758
setTimeout(blockKeywords_now, 500); // 网页 URL 变化后再次执行
758759
})
759760

@@ -778,7 +779,7 @@ function blockKeywords(type) {
778779
}
779780

780781
setTimeout(blockKeywords_now, 500);
781-
window.addEventListener('locationchange', function(){
782+
window.addEventListener('urlchange', function(){
782783
setTimeout(blockKeywords_now, 500); // 网页 URL 变化后再次执行
783784
})
784785

@@ -882,7 +883,7 @@ function blockType(type) {
882883
});
883884
observer.observe(document, { childList: true, subtree: true });
884885

885-
window.addEventListener('locationchange', function(){
886+
window.addEventListener('urlchange', function(){
886887
addSetInterval_(name);
887888
// 移除相关搜索
888889
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() {
10561057
});
10571058
observer.observe(document, { childList: true, subtree: true });
10581059

1059-
window.addEventListener('locationchange', function(){
1060+
window.addEventListener('urlchange', function(){
10601061
addSetInterval_('h2.ContentItem-title a:not(.zhihu_e_tips)');
10611062
})
10621063

@@ -1193,24 +1194,24 @@ function closeFloatingComments() {
11931194
}*/
11941195

11951196

1196-
// 自定义 locationchange 事件(用来监听 URL 变化)
1197-
function addLocationchange() {
1197+
// 自定义 urlchange 事件(用来监听 URL 变化)
1198+
function addUrlChangeEvent() {
11981199
history.pushState = ( f => function pushState(){
11991200
var ret = f.apply(this, arguments);
12001201
window.dispatchEvent(new Event('pushstate'));
1201-
window.dispatchEvent(new Event('locationchange'));
1202+
window.dispatchEvent(new Event('urlchange'));
12021203
return ret;
12031204
})(history.pushState);
12041205

12051206
history.replaceState = ( f => function replaceState(){
12061207
var ret = f.apply(this, arguments);
12071208
window.dispatchEvent(new Event('replacestate'));
1208-
window.dispatchEvent(new Event('locationchange'));
1209+
window.dispatchEvent(new Event('urlchange'));
12091210
return ret;
12101211
})(history.replaceState);
12111212

12121213
window.addEventListener('popstate',()=>{
1213-
window.dispatchEvent(new Event('locationchange'))
1214+
window.dispatchEvent(new Event('urlchange'))
12141215
});
12151216
}
12161217

@@ -1331,11 +1332,11 @@ function questionInvitation(){
13311332

13321333

13331334
(function() {
1334-
addLocationchange();
1335+
if (window.onurlchange === undefined) {addUrlChangeEvent();} // Tampermonkey v4.11 版本添加的 onurlchange 事件 grant,可以监控 pjax 等网页的 URL 变化
13351336
removeLogin(); // 移除登录弹窗
13361337
setInterval(originalPic,100); // 默认高清原图
13371338
setInterval(directLink, 100); // 默认站外直链
1338-
window.addEventListener('locationchange', function(){ // 针对的是从单个回答页跳转到完整回答页时
1339+
window.addEventListener('urlchange', function(){ // 针对的是从单个回答页跳转到完整回答页时
13391340
if (location.pathname.indexOf('question') > -1 && location.pathname.indexOf('waiting') === -1 && location.pathname.indexOf('/answer/') === -1) { // 回答页 //
13401341
setTimeout(function(){
13411342
collapsedNowAnswer('.QuestionPage'); // 收起当前回答 + 快捷返回顶部

0 commit comments

Comments
 (0)