-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurable-Auto-Refresh.user.js
More file actions
294 lines (261 loc) · 9.97 KB
/
Copy pathConfigurable-Auto-Refresh.user.js
File metadata and controls
294 lines (261 loc) · 9.97 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// ==UserScript==
// @name Configurable Auto Refresh
// @namespace https://github.com/Bandit/userscripts
// @version 1.0.0
// @description Adds a menu option to auto-refresh the current site and optionally notify when its rendered content changes.
// @author Bandit
// @homepageURL https://github.com/Bandit/userscripts/blob/main/Configurable-Auto-Refresh.user.js
// @supportURL https://github.com/Bandit/userscripts/issues
// @updateURL https://raw.githubusercontent.com/Bandit/userscripts/main/Configurable-Auto-Refresh.user.js
// @downloadURL https://raw.githubusercontent.com/Bandit/userscripts/main/Configurable-Auto-Refresh.user.js
// @match http://*/*
// @match https://*/*
// @noframes
// @grant GM_addStyle
// @grant GM_notification
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// ==/UserScript==
(function () {
'use strict';
const DEFAULT_REFRESH_SECONDS = 60;
const SESSION_KEY = 'configurableAutoRefresh.intervalSeconds';
const NOTIFICATION_KEY = 'configurableAutoRefresh.notifyOnVisualChange';
const SNAPSHOT_KEY = 'configurableAutoRefresh.visualSnapshot';
let intervalSeconds = readInterval();
let notificationEnabled = sessionStorage.getItem(NOTIFICATION_KEY) === 'true';
let refreshTimerId = null;
let countdownTimerId = null;
let deadline = 0;
let remainingMs = 0;
let paused = false;
let widget = null;
let menuCommandIds = [];
GM_addStyle(`
#car-widget {
position: fixed;
right: 12px;
bottom: 12px;
z-index: 2147483647;
display: flex;
align-items: center;
gap: 6px;
box-sizing: border-box;
min-height: 34px;
padding: 5px 6px 5px 9px;
color: #f5f5f2;
background: rgba(22, 26, 23, 0.5);
backdrop-filter: blur(10px) saturate(120%);
-webkit-backdrop-filter: blur(10px) saturate(120%);
border: 1px solid #59615b;
border-radius: 6px;
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.28);
font: 12px/1.2 ui-monospace, "Cascadia Mono", Consolas, monospace;
}
#car-widget label {
display: flex;
align-items: center;
gap: 4px;
white-space: nowrap;
cursor: pointer;
}
#car-widget input[type="number"] {
width: 54px;
height: 24px;
box-sizing: border-box;
padding: 2px 4px;
color: #f5f5f2;
background: #303631;
border: 1px solid #737d76;
border-radius: 4px;
font: inherit;
}
#car-widget input[type="checkbox"] {
margin: 0;
}
#car-widget button {
min-width: 30px;
height: 24px;
padding: 0 6px;
color: inherit;
background: transparent;
border: 1px solid #737d76;
border-radius: 4px;
font: 11px/1 sans-serif;
cursor: pointer;
}
#car-widget button:hover { background: #3a423d; }
#car-widget button:focus-visible { outline: 2px solid #9cc7a5; outline-offset: 1px; }
#car-widget .car-refresh { min-width: 68px; }
#car-widget .car-cancel:hover { background: #703a36; border-color: #b97770; }
`);
registerMenuCommands();
if (intervalSeconds !== null) {
createWidget();
scheduleRefresh();
window.setTimeout(checkForVisualChange, 750);
}
function registerMenuCommands() {
menuCommandIds.forEach((commandId) => GM_unregisterMenuCommand(commandId));
menuCommandIds = [];
if (intervalSeconds === null) {
menuCommandIds.push(GM_registerMenuCommand('Enable auto refresh', () => startRefreshing(DEFAULT_REFRESH_SECONDS)));
} else {
menuCommandIds.push(GM_registerMenuCommand('Disable auto refresh', stopRefreshing));
}
}
function readInterval() {
const value = Number(sessionStorage.getItem(SESSION_KEY));
return Number.isFinite(value) && value >= 1 ? value : null;
}
function startRefreshing(seconds) {
intervalSeconds = seconds;
sessionStorage.setItem(SESSION_KEY, String(seconds));
registerMenuCommands();
createWidget();
scheduleRefresh();
}
function stopRefreshing() {
intervalSeconds = null;
notificationEnabled = false;
sessionStorage.removeItem(SESSION_KEY);
sessionStorage.removeItem(NOTIFICATION_KEY);
sessionStorage.removeItem(SNAPSHOT_KEY);
clearTimers();
widget?.remove();
widget = null;
paused = false;
remainingMs = 0;
registerMenuCommands();
}
function scheduleRefresh(delayMs = intervalSeconds * 1000) {
clearTimers();
paused = false;
remainingMs = delayMs;
deadline = Date.now() + delayMs;
refreshTimerId = window.setTimeout(reloadWithSnapshot, delayMs);
countdownTimerId = window.setInterval(updateWidget, 250);
updateWidget();
}
function clearTimers() {
window.clearTimeout(refreshTimerId);
window.clearInterval(countdownTimerId);
refreshTimerId = null;
countdownTimerId = null;
}
function setNotifications(enabled) {
notificationEnabled = enabled;
sessionStorage.setItem(NOTIFICATION_KEY, String(notificationEnabled));
sessionStorage.removeItem(SNAPSHOT_KEY);
}
function reloadWithSnapshot() {
if (notificationEnabled) {
sessionStorage.setItem(SNAPSHOT_KEY, createVisualFingerprint());
}
location.reload();
}
function checkForVisualChange() {
if (!notificationEnabled || intervalSeconds === null) return;
const previousFingerprint = sessionStorage.getItem(SNAPSHOT_KEY);
sessionStorage.removeItem(SNAPSHOT_KEY);
if (previousFingerprint === null) return;
const currentFingerprint = createVisualFingerprint();
if (currentFingerprint !== previousFingerprint) {
GM_notification({
title: 'Page changed',
text: `${document.title || location.hostname} looks different after refreshing.`,
timeout: 10000,
onclick: () => window.focus(),
});
}
}
function createVisualFingerprint() {
let hash = 2166136261;
const elements = [document.body, ...document.body.querySelectorAll('*')];
for (const element of elements) {
if (!element || element.closest('#car-widget') || ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEMPLATE'].includes(element.tagName)) continue;
const style = getComputedStyle(element);
const rect = element.getBoundingClientRect();
if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0' || rect.width === 0 || rect.height === 0) continue;
const directText = Array.from(element.childNodes)
.filter((node) => node.nodeType === Node.TEXT_NODE)
.map((node) => node.textContent.trim())
.filter(Boolean)
.join(' ');
const mediaSource = element.currentSrc || element.src || element.getAttribute?.('href') || '';
const formState = 'value' in element ? `${element.value}|${element.checked ?? ''}` : '';
const visualState = [
element.tagName,
directText,
mediaSource,
formState,
Math.round(rect.left),
Math.round(rect.top),
Math.round(rect.width),
Math.round(rect.height),
style.color,
style.backgroundColor,
style.backgroundImage,
style.borderColor,
style.font,
style.textDecoration,
].join('|');
for (let index = 0; index < visualState.length; index += 1) {
hash ^= visualState.charCodeAt(index);
hash = Math.imul(hash, 16777619);
}
}
return (hash >>> 0).toString(16);
}
function togglePause() {
if (paused) {
scheduleRefresh(remainingMs);
return;
}
remainingMs = Math.max(0, deadline - Date.now());
paused = true;
clearTimers();
updateWidget();
}
function createWidget() {
if (widget) return;
widget = document.createElement('div');
widget.id = 'car-widget';
widget.innerHTML = '<label><input type="number" class="car-interval" min="1" step="1" aria-label="Refresh interval in seconds">s</label><label><input type="checkbox" class="car-notify">Notify on change</label><button type="button" class="car-refresh" title="Refresh now" aria-label="Refresh now">↻ <span class="car-countdown"></span></button><button type="button" class="car-pause" title="Pause auto refresh" aria-label="Pause auto refresh">⏸</button><button type="button" class="car-cancel" title="Cancel auto refresh" aria-label="Cancel auto refresh">✕</button>';
widget.querySelector('.car-interval').value = String(intervalSeconds);
widget.querySelector('.car-notify').checked = notificationEnabled;
widget.querySelector('.car-interval').addEventListener('change', updateRefreshInterval);
widget.querySelector('.car-notify').addEventListener('change', (event) => setNotifications(event.currentTarget.checked));
widget.querySelector('.car-pause').addEventListener('click', togglePause);
widget.querySelector('.car-refresh').addEventListener('click', reloadWithSnapshot);
widget.querySelector('.car-cancel').addEventListener('click', stopRefreshing);
document.documentElement.append(widget);
}
function updateWidget() {
if (!widget) return;
if (!paused) {
remainingMs = Math.max(0, deadline - Date.now());
}
widget.querySelector('.car-countdown').textContent = formatDuration(remainingMs);
const pauseButton = widget.querySelector('.car-pause');
pauseButton.innerHTML = paused ? '▶' : '⏸';
pauseButton.title = paused ? 'Resume auto refresh' : 'Pause auto refresh';
pauseButton.setAttribute('aria-label', pauseButton.title);
}
function updateRefreshInterval(event) {
const seconds = Number(event.currentTarget.value);
if (!Number.isFinite(seconds) || seconds < 1) {
event.currentTarget.value = String(intervalSeconds);
return;
}
intervalSeconds = seconds;
sessionStorage.setItem(SESSION_KEY, String(seconds));
scheduleRefresh();
}
function formatDuration(milliseconds) {
const totalSeconds = Math.max(0, Math.ceil(milliseconds / 1000));
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes}:${String(seconds).padStart(2, '0')}`;
}
})();