Advertisement
KunalkaushikV

Untitled

May 27th, 2025
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. // —— STEP 1: Intercept fetch() calls ——
  3. (function(){
  4.   // keep a reference to the original window.fetch
  5.   const _origFetch = window.fetch;
  6.   if (!_origFetch) return;
  7.  
  8.   window.fetch = async function(resource, init) {
  9.     // call the real fetch
  10.     const res = await _origFetch.call(this, resource, init);
  11.  
  12.     // look for the recommendation endpoint
  13.     if (typeof resource === 'string' && resource.includes('/recommendation?')) {
  14.       // clone() so the SDK can still consume the body
  15.       res.clone().json()
  16.         .then(payload => {
  17.           console.log('%c[Recs JSON - fetch interceptor]', 'color: teal', payload);
  18.         })
  19.         .catch(err => {
  20.           console.warn('[Recs JSON parse error]', err);
  21.         });
  22.     }
  23.     return res;
  24.   };
  25. })();
  26.  
  27. // —— STEP 2: Intercept XHR calls ——
  28. (function(){
  29.   const _origOpen = XMLHttpRequest.prototype.open;
  30.   XMLHttpRequest.prototype.open = function(method, url, async, user, pw) {
  31.     // whenever someone opens a recs URL…
  32.     if (typeof url === 'string' && url.includes('/recommendation?')) {
  33.       this.addEventListener('load', function() {
  34.         try {
  35.           const json = JSON.parse(this.responseText);
  36.           console.log('%c[Recs JSON - XHR interceptor]', 'color: purple', json);
  37.         } catch (e) {
  38.           console.warn('[Recs XHR parse error]', e);
  39.         }
  40.       });
  41.     }
  42.     return _origOpen.call(this, method, url, async, user, pw);
  43.   };
  44. })();
  45. </script>
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement