Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- 1) URL rewrite + adaptive SDK caller -->
- <script>
- (function() {
- 'use strict';
- // ---- 1A) fetch() override ONLY ----
- try {
- const origFetch = window.fetch;
- if (origFetch) {
- window.fetch = async function(resource, init) {
- try {
- // Only rewrite the wrong /items? → /recommendation?
- if (typeof resource === 'string' && resource.includes('/items?')) {
- resource = resource.replace(/\/items\?/, '/recommendation?');
- }
- return await origFetch.call(this, resource, init);
- } catch (e) {
- console.error('Fetch override failed:', e);
- // Fallback to native behavior
- return await origFetch.call(this, resource, init);
- }
- };
- }
- } catch (e) {
- console.warn('Could not patch window.fetch:', e);
- }
- // ---- 1B) Single invocation of the correct SDK method ----
- function initUnbxdRecs() {
- try {
- const cfg = {
- widgets: {
- widget1: { name: 'recommendations1' }
- },
- userInfo: {
- userId: 'uid-12345',
- siteKey: 'site-key',
- apiKey: 'api-key'
- },
- pageInfo: {
- pageType: 'HOME'
- },
- itemClickHandler: p => console.log('clicked', p),
- dataParser: d => d
- };
- // v3: use the ES6 class
- if (typeof window.getUnbxdRecommendations === 'function') {
- new window.getUnbxdRecommendations(cfg);
- }
- // v2: use the underscore helper
- else if (typeof window._unbxd_getRecommendations === 'function') {
- window._unbxd_getRecommendations(cfg);
- }
- else {
- console.error('No Unbxd recommendation entrypoint found.');
- }
- } catch (err) {
- console.error('Failed to initialize Unbxd Recs SDK:', err);
- }
- }
- // Defer until after SDK script loads
- window.addEventListener('unbxd-sdk-ready', initUnbxdRecs);
- })();
- </script>
- <!-- 2) Unbxd Recs SDK v3 -->
- <script
- src="https://libraries.unbxdapi.com/recs-sdk/v3.1.0/unbxd_recs_template_sdk.js"
- async
- onload="window.dispatchEvent(new Event('unbxd-sdk-ready'))">
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement