Spaces:
Running
Running
Load both DMX + Garis products, update stats after each source
Browse files- dmx-loader.js +50 -67
dmx-loader.js
CHANGED
|
@@ -1,77 +1,60 @@
|
|
| 1 |
-
//
|
| 2 |
-
// Fixes: strip utm params, ensure OG-compatible images
|
| 3 |
(function(){
|
| 4 |
-
function cleanUrl(u){
|
| 5 |
-
// Remove all tracking params (utm_*, flashsale, etc)
|
| 6 |
-
try{ return u.split('?')[0].split('#')[0]; }catch(e){ return u; }
|
| 7 |
-
}
|
| 8 |
function fixImg(src){
|
| 9 |
-
if(!src)
|
| 10 |
-
src
|
| 11 |
-
if(src.startsWith('//')) src = 'https:' + src;
|
| 12 |
return src.split('?')[0];
|
| 13 |
}
|
| 14 |
-
function
|
| 15 |
-
var link
|
| 16 |
-
var image
|
| 17 |
-
var images
|
| 18 |
-
if(image
|
| 19 |
-
if(!image
|
| 20 |
return {
|
| 21 |
-
name:p.n,
|
| 22 |
-
cat:p.c,
|
| 23 |
-
subCat:p.sub_c||'',
|
| 24 |
-
images:images,
|
| 25 |
-
specs:p.specs||{},
|
| 26 |
-
video:p.vid||'',
|
| 27 |
-
brand:p.brand||
|
| 28 |
-
_idx:
|
| 29 |
};
|
| 30 |
}
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
if(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
clearInterval(checkInterval);
|
| 35 |
-
console.log('[
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
.then(function(
|
| 39 |
-
.then(function(
|
| 40 |
-
|
| 41 |
-
D.forEach(function(p){ if(p.slug) existingSlugs[p.slug] = true; });
|
| 42 |
-
|
| 43 |
-
var added = 0;
|
| 44 |
-
dmxData.forEach(function(p){
|
| 45 |
-
var mapped = mapDMX(p);
|
| 46 |
-
if(!existingSlugs[mapped.slug]){
|
| 47 |
-
if(typeof buildSearchIndex === 'function'){
|
| 48 |
-
mapped._idx = buildSearchIndex(mapped);
|
| 49 |
-
}
|
| 50 |
-
D.push(mapped);
|
| 51 |
-
existingSlugs[mapped.slug] = true;
|
| 52 |
-
added++;
|
| 53 |
-
}
|
| 54 |
-
});
|
| 55 |
-
|
| 56 |
-
console.log('[DMX] Added', added, 'DMX products. Total:', D.length);
|
| 57 |
-
|
| 58 |
-
var st = document.getElementById('statTotal');
|
| 59 |
-
if(st) st.textContent = D.length;
|
| 60 |
-
|
| 61 |
-
var sc = document.getElementById('statCats');
|
| 62 |
-
if(sc){
|
| 63 |
-
var cats = {};
|
| 64 |
-
D.forEach(function(p){ cats[p.catSlug] = true; });
|
| 65 |
-
sc.textContent = Object.keys(cats).length;
|
| 66 |
-
}
|
| 67 |
-
|
| 68 |
-
if(typeof init === 'function') init();
|
| 69 |
-
})
|
| 70 |
-
.catch(function(e){
|
| 71 |
-
console.warn('[DMX] Failed to load DMX products:', e);
|
| 72 |
-
});
|
| 73 |
}
|
| 74 |
-
},
|
| 75 |
-
|
| 76 |
-
setTimeout(function(){ clearInterval(checkInterval); }, 30000);
|
| 77 |
})();
|
|
|
|
| 1 |
+
// External Products Loader v3 — DMX (6273) + Garis (202)
|
|
|
|
| 2 |
(function(){
|
| 3 |
+
function cleanUrl(u){try{return u.split('?')[0].split('#')[0]}catch(e){return u}}
|
|
|
|
|
|
|
|
|
|
| 4 |
function fixImg(src){
|
| 5 |
+
if(!src)return '';src=src.trim();
|
| 6 |
+
if(src.startsWith('//'))src='https:'+src;
|
|
|
|
| 7 |
return src.split('?')[0];
|
| 8 |
}
|
| 9 |
+
function mapProduct(p,defaultBrand){
|
| 10 |
+
var link=cleanUrl(p.l||'');
|
| 11 |
+
var image=fixImg(p.i||'');
|
| 12 |
+
var images=(p.imgs||[]).map(fixImg).filter(Boolean);
|
| 13 |
+
if(image&&images.indexOf(image)===-1)images.unshift(image);
|
| 14 |
+
if(!image&&images.length)image=images[0];
|
| 15 |
return {
|
| 16 |
+
name:p.n,link:link,image:image,price:p.p,priceNum:p.pn,
|
| 17 |
+
cat:p.c,catSlug:p.cs,catIcon:p.ci,
|
| 18 |
+
subCat:p.sub_c||'',subCatSlug:p.sub_cs||'',
|
| 19 |
+
images:images,summary:p.sum||'',desc:p.desc||'',
|
| 20 |
+
specs:p.specs||{},feats:p.feats||[],sku:p.sku||'',
|
| 21 |
+
video:p.vid||'',model:p.mod||'',slug:p.slug||'',
|
| 22 |
+
brand:p.brand||defaultBrand,
|
| 23 |
+
_idx:(p.sku||'')+' '+(p.brand||'').toLowerCase()+' '+(p.n||'').toLowerCase()
|
| 24 |
};
|
| 25 |
}
|
| 26 |
+
function updateStats(){
|
| 27 |
+
var st=document.getElementById('statTotal');
|
| 28 |
+
if(st)st.textContent=D.length;
|
| 29 |
+
var sc=document.getElementById('statCats');
|
| 30 |
+
if(sc){var cats={};D.forEach(function(p){cats[p.catSlug]=true});sc.textContent=Object.keys(cats).length}
|
| 31 |
+
}
|
| 32 |
+
function loadSource(jsonFile,brand,label){
|
| 33 |
+
return fetch(jsonFile).then(function(r){return r.json()}).then(function(data){
|
| 34 |
+
var existingSlugs={};
|
| 35 |
+
D.forEach(function(p){if(p.slug)existingSlugs[p.slug]=true});
|
| 36 |
+
var added=0;
|
| 37 |
+
data.forEach(function(p){
|
| 38 |
+
var mapped=mapProduct(p,brand);
|
| 39 |
+
if(!existingSlugs[mapped.slug]){
|
| 40 |
+
if(typeof buildSearchIndex==='function')mapped._idx=buildSearchIndex(mapped);
|
| 41 |
+
D.push(mapped);existingSlugs[mapped.slug]=true;added++;
|
| 42 |
+
}
|
| 43 |
+
});
|
| 44 |
+
console.log('['+label+'] Added',added,'products. Total:',D.length);
|
| 45 |
+
return added;
|
| 46 |
+
});
|
| 47 |
+
}
|
| 48 |
+
var checkInterval=setInterval(function(){
|
| 49 |
+
if(typeof D!=='undefined'&&D.length>0&&typeof init==='function'){
|
| 50 |
clearInterval(checkInterval);
|
| 51 |
+
console.log('[Loader] Main data:',D.length,'products. Loading external sources...');
|
| 52 |
+
// Load DMX then Garis, then re-init once
|
| 53 |
+
loadSource('products_dmx_slugs.json','Điện Máy Xanh','DMX')
|
| 54 |
+
.then(function(){return loadSource('products_garis_slugs.json','Garis','Garis')})
|
| 55 |
+
.then(function(){updateStats();if(typeof init==='function')init()})
|
| 56 |
+
.catch(function(e){console.warn('[Loader] Error:',e);updateStats();if(typeof init==='function')init()});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
+
},500);
|
| 59 |
+
setTimeout(function(){clearInterval(checkInterval)},30000);
|
|
|
|
| 60 |
})();
|