bep40 commited on
Commit
70acd0d
·
verified ·
1 Parent(s): 2b12d0e

Load both DMX + Garis products, update stats after each source

Browse files
Files changed (1) hide show
  1. dmx-loader.js +50 -67
dmx-loader.js CHANGED
@@ -1,77 +1,60 @@
1
- // DMX Products Loader v2 - 6273 products from Điện Máy Xanh
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) return '';
10
- src = src.trim();
11
- if(src.startsWith('//')) src = 'https:' + src;
12
  return src.split('?')[0];
13
  }
14
- function mapDMX(p){
15
- var link = cleanUrl(p.l||'');
16
- var image = fixImg(p.i||'');
17
- var images = (p.imgs||[]).map(fixImg).filter(Boolean);
18
- if(image && images.indexOf(image)===-1) images.unshift(image);
19
- if(!image && images.length) image = images[0];
20
  return {
21
- name:p.n, link:link, image:image, price:p.p, priceNum:p.pn,
22
- cat:p.c, catSlug:p.cs, catIcon:p.ci,
23
- subCat:p.sub_c||'', subCatSlug:p.sub_cs||'',
24
- images:images, summary:p.sum||'', desc:p.desc||'',
25
- specs:p.specs||{}, feats:p.feats||[], sku:p.sku||'',
26
- video:p.vid||'', model:p.mod||'', slug:p.slug||'',
27
- brand:p.brand||'Điện Máy Xanh',
28
- _idx: (p.sku||'') + ' ' + (p.brand||'').toLowerCase() + ' ' + (p.n||'').toLowerCase()
29
  };
30
  }
31
-
32
- var checkInterval = setInterval(function(){
33
- if(typeof D !== 'undefined' && D.length > 0 && typeof init === 'function'){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  clearInterval(checkInterval);
35
- console.log('[DMX] Main data loaded:', D.length, 'products. Loading DMX...');
36
-
37
- fetch('products_dmx_slugs.json')
38
- .then(function(r){ return r.json(); })
39
- .then(function(dmxData){
40
- var existingSlugs = {};
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
- }, 500);
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
  })();