Advertisement
KunalkaushikV

Untitled

May 20th, 2025
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!-- 1) URL rewrite + adaptive SDK caller -->
  2. <script>
  3. (function() {
  4.   'use strict';
  5.  
  6.   // ---- 1A) fetch() override ONLY ----
  7.   try {
  8.     const origFetch = window.fetch;
  9.     if (origFetch) {
  10.       window.fetch = async function(resource, init) {
  11.         try {
  12.           // Only rewrite the wrong /items? → /recommendation?
  13.           if (typeof resource === 'string' && resource.includes('/items?')) {
  14.             resource = resource.replace(/\/items\?/, '/recommendation?');
  15.           }
  16.           return await origFetch.call(this, resource, init);
  17.         } catch (e) {
  18.           console.error('Fetch override failed:', e);
  19.           // Fallback to native behavior
  20.           return await origFetch.call(this, resource, init);
  21.         }
  22.       };
  23.     }
  24.   } catch (e) {
  25.     console.warn('Could not patch window.fetch:', e);
  26.   }
  27.  
  28.   // ---- 1B) Single invocation of the correct SDK method ----
  29.   function initUnbxdRecs() {
  30.     try {
  31.       const cfg = {
  32.         widgets: {
  33.           widget1: { name: 'recommendations1' }
  34.         },
  35.         userInfo: {
  36.           userId: 'uid-12345',
  37.           siteKey: 'site-key',
  38.           apiKey: 'api-key'
  39.         },
  40.         pageInfo: {
  41.           pageType: 'HOME'
  42.         },
  43.         itemClickHandler: p => console.log('clicked', p),
  44.         dataParser: d => d
  45.       };
  46.       // v3: use the ES6 class
  47.       if (typeof window.getUnbxdRecommendations === 'function') {
  48.         new window.getUnbxdRecommendations(cfg);
  49.       }
  50.       // v2: use the underscore helper
  51.       else if (typeof window._unbxd_getRecommendations === 'function') {
  52.         window._unbxd_getRecommendations(cfg);
  53.       }
  54.       else {
  55.         console.error('No Unbxd recommendation entrypoint found.');
  56.       }
  57.     } catch (err) {
  58.       console.error('Failed to initialize Unbxd Recs SDK:', err);
  59.     }
  60.   }
  61.  
  62.   // Defer until after SDK script loads
  63.   window.addEventListener('unbxd-sdk-ready', initUnbxdRecs);
  64. })();
  65. </script>
  66.  
  67. <!-- 2) Unbxd Recs SDK v3 -->
  68. <script
  69.   src="https://libraries.unbxdapi.com/recs-sdk/v3.1.0/unbxd_recs_template_sdk.js"
  70.   async
  71.   onload="window.dispatchEvent(new Event('unbxd-sdk-ready'))">
  72. </script>
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement