Created
March 24, 2025 20:37
-
-
Save leo60228/d76369d1fd863f33a3623f32d0bd8efd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// no-rufus.js | |
(function() { | |
const xhrInstances = new WeakMap(); | |
let arg1 = '{{1}}'; | |
if ( arg1 === '{{1}}' ) { arg1 = ''; } | |
const needles = []; | |
for ( const condition of arg1.split(/\s+/) ) { | |
if ( condition === '' ) { continue; } | |
const pos = condition.indexOf(':'); | |
let key, value; | |
if ( pos !== -1 ) { | |
key = condition.slice(0, pos); | |
value = condition.slice(pos + 1); | |
} else { | |
key = 'url'; | |
value = condition; | |
} | |
if ( value === '' ) { | |
value = '^'; | |
} else if ( value.startsWith('/') && value.endsWith('/') ) { | |
value = value.slice(1, -1); | |
} else { | |
value = value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | |
} | |
needles.push({ key, re: new RegExp(value) }); | |
} | |
const log = needles.length === 0 ? console.log.bind(console) : undefined; | |
self.XMLHttpRequest = class extends self.XMLHttpRequest { | |
open(...args) { | |
if ( log !== undefined ) { | |
log(`uBO: xhr.open(${args.join(', ')})`); | |
} else { | |
const argNames = [ 'method', 'url' ]; | |
const haystack = new Map(); | |
for ( let i = 0; i < args.length && i < argNames.length; i++ ) { | |
haystack.set(argNames[i], args[i]); | |
} | |
if ( haystack.size !== 0 ) { | |
let matches = true; | |
for ( const { key, re } of needles ) { | |
matches = re.test(haystack.get(key) || ''); | |
if ( matches === false ) { break; } | |
} | |
if ( matches ) { | |
xhrInstances.set(this, haystack); | |
} | |
} | |
} | |
return super.open(...args); | |
} | |
send(...args) { | |
const haystack = xhrInstances.get(this); | |
if ( haystack === undefined ) { | |
return super.send(...args); | |
} | |
Object.defineProperties(this, { | |
readyState: { value: 4, writable: false }, | |
response: { value: '', writable: false }, | |
responseText: { | |
get() { | |
return "event:CONTEXT_CHUNK\nid:0\ndata:{\"requestContext\":{},\"bsr\":null}\n\nevent:close\nid:EOF\ndata:\n\n"; | |
} | |
}, | |
responseURL: { value: haystack.get('url'), writable: false }, | |
responseXML: { value: '', writable: false }, | |
status: { value: 200, writable: false }, | |
statusText: { value: 'OK', writable: false }, | |
}); | |
this.dispatchEvent(new Event('readystatechange')); | |
this.dispatchEvent(new Event('load')); | |
this.dispatchEvent(new Event('loadend')); | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment