-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnicovideo-autoplay-canceler.user.js
110 lines (96 loc) · 4.24 KB
/
nicovideo-autoplay-canceler.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// ==UserScript==
// @name nicovideo-autoplay-canceler
// @namespace https://github.com/dnek
// @version 2.1
// @author dnek
// @description ニコニコ動画で動画の自動再生が開始された直後に自動的に一時停止します。停止に少しラグがあるため、停止時の再生時間が3秒未満の場合は0秒まで戻します。「nicovideo-next-video-canceler」「nicovideo-player-expander」は別のスクリプトです。
// @description:ja ニコニコ動画で動画の自動再生が開始された直後に自動的に一時停止します。停止に少しラグがあるため、停止時の再生時間が3秒未満の場合は0秒まで戻します。「nicovideo-next-video-canceler」「nicovideo-player-expander」は別のスクリプトです。
// @homepageURL https://github.com/dnek/nicovideo-autoplay-canceler
// @updateURL https://github.com/dnek/nicovideo-autoplay-canceler/raw/main/nicovideo-autoplay-canceler.user.js
// @downloadURL https://github.com/dnek/nicovideo-autoplay-canceler/raw/main/nicovideo-autoplay-canceler.user.js
// @match https://www.nicovideo.jp/watch/*
// @grant none
// @license MIT license
// ==/UserScript==
(function () {
'use strict';
let lastHref = '';
let isCanceled = false;
const observer = new MutationObserver((mutationList, observer) => {
mutationList.filter(mutation => mutation.type === 'childList').forEach(mutation => {
for (const node of mutation.addedNodes) {
if (
node.nodeType === 1 &&
node.innerHTML.includes('aria-label="再生する"')
) {
const buttonEl = node.querySelector('button[aria-label="再生する"]');
buttonEl.addEventListener('click', () => {
if (isCanceled) {
return;
}
isCanceled = true;
console.log(`Autoplay cancel won't be executed because play button was clicked.`);
});
console.log('nicovideo-autoplay-canceler listener added to button.');
}
}
});
});
const options = {
childList: true,
subtree: true,
};
observer.observe(document.body, options);
document.addEventListener('keydown', (e) => {
if (isCanceled) {
return;
}
if (e.target && ['input', 'textarea'].includes(e.target.tagName.toLowerCase())) {
return;
}
if (e.isComposing || e.altKey || e.ctrlKey || e.metaKey || e.shiftKey || e.repeat) {
return;
}
if (e.code === 'Space' || e.code === 'KeyK') {
isCanceled = true;
console.log(`Autoplay cancel won't be executed because ${e.code} was pressed.`);
}
});
setInterval(() => {
const href = location.href;
if (lastHref !== href) {
lastHref = href;
console.log(`href changed to ${href}`);
isCanceled = false;
}
const ctxEl = document.getElementById('menu::r5::ctx-trigger');
if (ctxEl !== null && !ctxEl.dataset.napcTriggered) {
ctxEl.addEventListener('click', () => {
if (isCanceled) {
return;
}
isCanceled = true;
console.log(`Autoplay cancel won't be executed because player was clicked.`);
});
ctxEl.dataset.napcTriggered = true;
console.log('nicovideo-autoplay-canceler listener added to ctx.');
}
if (isCanceled) {
return;
}
const videoEl = document.querySelector('video[data-name="video-content"]');
if (videoEl === null || videoEl.paused || videoEl.ended) {
return;
}
isCanceled = true;
videoEl.pause();
console.log('video paused.');
if (videoEl.currentTime < 3) {
const rewindButton = document.querySelector('button[aria-label$=" 秒戻る"]');
if (rewindButton !== null) {
rewindButton.click();
console.log('rewind button clicked.');
}
}
}, 0);
})();