-
Notifications
You must be signed in to change notification settings - Fork 30
/
prevent-refresh.js
140 lines (134 loc) · 4.26 KB
/
prevent-refresh.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
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
import {
hit,
getNumberFromString,
logMessage,
nativeIsNaN,
} from '../helpers';
/* eslint-disable max-len */
/**
* @scriptlet prevent-refresh
*
* @description
* Prevents reloading of a document through a meta "refresh" tag.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/wiki/Resources-Library#refresh-defuserjs-
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('prevent-refresh'[, delay])
* ```
*
* - `delay` — optional, number of seconds for delay that indicates when scriptlet should run.
* If not set, source tag value will be applied.
*
* ### Examples
*
* 1. Prevent reloading of a document through a meta "refresh" tag
*
* ```adblock
* example.org#%#//scriptlet('prevent-refresh')
* ```
*
* 1. Prevent reloading of a document with delay
*
* ```adblock
* example.com#%#//scriptlet('prevent-refresh', 3)
* ```
*
* @added v1.6.2.
*/
/* eslint-enable max-len */
export function preventRefresh(source, delaySec) {
const getMetaElements = () => {
let metaNodes = [];
try {
metaNodes = document.querySelectorAll('meta[http-equiv="refresh" i][content]');
} catch (e) {
// 'i' attribute flag is problematic in Edge 15
try {
metaNodes = document.querySelectorAll('meta[http-equiv="refresh"][content]');
} catch (e) {
logMessage(source, e);
}
}
return Array.from(metaNodes);
};
const getMetaContentDelay = (metaElements) => {
const delays = metaElements
.map((meta) => {
const contentString = meta.getAttribute('content');
if (contentString.length === 0) {
return null;
}
let contentDelay;
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta#attr-http-equiv
const limiterIndex = contentString.indexOf(';');
if (limiterIndex !== -1) {
const delaySubstring = contentString.substring(0, limiterIndex);
contentDelay = getNumberFromString(delaySubstring);
} else {
contentDelay = getNumberFromString(contentString);
}
return contentDelay;
})
.filter((delay) => delay !== null);
// Check if "delays" array is empty, may happens when meta's content is invalid
// and reduce() method cannot be used with empty arrays without initial value
if (!delays.length) {
return null;
}
// Get smallest delay of all metas on the page
const minDelay = delays.reduce((a, b) => Math.min(a, b));
// eslint-disable-next-line consistent-return
return minDelay;
};
const stop = () => {
const metaElements = getMetaElements();
if (metaElements.length === 0) {
return;
}
let secondsToRun = getNumberFromString(delaySec);
// Check if argument is provided
if (secondsToRun === null) {
secondsToRun = getMetaContentDelay(metaElements);
}
// Check if meta tag has delay
if (secondsToRun === null) {
return;
}
const delayMs = secondsToRun * 1000;
setTimeout(() => {
window.stop();
hit(source);
}, delayMs);
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', stop, { once: true });
} else {
stop();
}
}
export const preventRefreshNames = [
'prevent-refresh',
// Aliases are needed for matching the related scriptlet converted into our syntax
// These are used by UBO rules syntax
// https://github.com/gorhill/uBlock/wiki/Resources-Library#general-purpose-scriptlets
'prevent-refresh.js',
'refresh-defuser.js',
'refresh-defuser',
// Prefix 'ubo-' is required to run converted rules
'ubo-prevent-refresh.js',
'ubo-prevent-refresh',
'ubo-refresh-defuser.js',
'ubo-refresh-defuser',
];
// eslint-disable-next-line prefer-destructuring
preventRefresh.primaryName = preventRefreshNames[0];
preventRefresh.injections = [
hit,
getNumberFromString,
logMessage,
nativeIsNaN,
];