-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglobal.js
More file actions
46 lines (37 loc) · 1.51 KB
/
global.js
File metadata and controls
46 lines (37 loc) · 1.51 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
NodeList.prototype.forEach = Array.prototype.forEach;
NodeList.prototype.map = Array.prototype.map;
NodeList.prototype.reduce = Array.prototype.reduce;
Element.prototype.addDelegateEventListener = function ( eventType, childSelector, callback ) {
this.addEventListener( eventType, function ( e ) {
if ( e.target.matches( childSelector ) ) {
callback.call( e.target, ...arguments );
}
} );
};
( function () {
if ( !window.location.href.includes( 'localhost' ) ) return;
const resources = { [ window.location.href ]: null };
document.querySelectorAll( 'link[href], script[src]' ).forEach( resource => {
resources[ resource.href || resource.src ] = null;
} );
const urls = Object.keys( resources );
function detectFileChanges ( index = 0 ) {
if ( index < urls.length ) {
fetch( urls[ index ], { method: 'HEAD', cache: 'no-cache' } ).then( xhr => {
const etag = xhr.headers.get( 'ETag' );
let previousTag = resources[ urls[ index ] ];
if ( !previousTag ) {
previousTag = resources[ urls[ index ] ] = etag;
}
if ( previousTag !== etag ) {
window.location.reload();
} else {
setTimeout( () => detectFileChanges( ++index ), 500 );
}
} );
} else {
setTimeout( detectFileChanges, 500 );
}
}
detectFileChanges();
} )();