-
Notifications
You must be signed in to change notification settings - Fork 2
/
xhrp5.js
86 lines (86 loc) · 3.41 KB
/
xhrp5.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
// replace parts of a xhr file - scriptlet for uBlock Origin
// example filter:
// telsu.fi##+js(xhrp, /^\/.*/, /^<div.*\/div>$/, )
//
// example.com##+js(xhrp, param1, param2, param3)
// param1 (regex): matching xhr files
// param2 regular expression, to search for
// param3 The new value (to replace with)
/// xhrp.js
(function() {
'use strict';
let urlXHR = '{{1}}';
if ( urlXHR === '' || urlXHR === '{{1}}' ) {
urlXHR = '';
} else if ( urlXHR.startsWith('/') && urlXHR.endsWith('/') ) {
urlXHR = urlXHR.slice(1,-1);
} else {
urlXHR = urlXHR.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
urlXHR = new RegExp(urlXHR);
let what = '{{2}}';
if ( what === '' || what === '{{2}}' ) {
what = '';
} else if ( what.startsWith('/') && what.endsWith('/') ) {
what = what.slice(1,-1);
} else {
what = what.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
what = new RegExp(what, "gms");
let by = '{{3}}';
if ( by === '' || by === '{{3}}' ) {
by = '';
}
//console.log('uBO:'+'urlXHR:'+urlXHR);
//console.log('uBO:'+'what:'+what);
//console.log('uBO:'+'by:'+by);
const pruner = text => {
text = text.replace(what, by);
return text;
};
const urlFromArg = arg => {
if ( typeof arg === 'string' ) { return arg; }
if ( arg instanceof Request ) { return arg.url; }
return String(arg);
};
const realFetch = self.fetch;
self.fetch = new Proxy(self.fetch, {
apply: function(target, thisArg, args) {
if ( urlXHR.test(urlFromArg(args[0])) === false ) {
return Reflect.apply(target, thisArg, args);
}
return realFetch(...args).then(realResponse =>
realResponse.text().then(text =>
new Response(pruner(text), {
status: realResponse.status,
statusText: realResponse.statusText,
headers: realResponse.headers,
})
)
);
}
});
self.XMLHttpRequest.prototype.open = new Proxy(self.XMLHttpRequest.prototype.open, {
apply: async (target, thisArg, args) => {
if ( urlXHR.test(urlFromArg(args[1])) === false ) {
return Reflect.apply(target, thisArg, args);
}
thisArg.addEventListener('readystatechange', function() {
if ( thisArg.readyState !== 4 ) { return; }
console.log('uBO:'+'file:'+args[1]);
//console.log('uBO:'+'state:'+thisArg.readyState);
const type = thisArg.responseType;
//console.log('uBO:'+'type:'+type);
if ( type !== '' && type !== 'text' ) { return; }
const textin = thisArg.responseText;
console.log('uBO:'+'textin:'+textin);
const textout = pruner(textin);
console.log('uBO:'+'textout:'+textout);
if ( textout === textin ) { return; }
Object.defineProperty(thisArg, 'response', { value: textout });
Object.defineProperty(thisArg, 'responseText', { value: textout });
});
return Reflect.apply(target, thisArg, args);
}
});
})();