V.AISTUDIO / dmx-loader.js
bep40's picture
dmx-loader v5: purge ALL Malloca from D after DMX load (not just filter during load)
139c126 verified
// External Products Loader v5 — DMX + Garis + PURGE ALL Malloca from DMX source
(function(){
function cleanUrl(u){try{return u.split('?')[0].split('#')[0]}catch(e){return u}}
function fixImg(src){
if(!src)return '';src=src.trim();
if(src.startsWith('//'))src='https:'+src;
return src.split('?')[0];
}
function mapProduct(p,defaultBrand){
var link=cleanUrl(p.l||'');
var image=fixImg(p.i||'');
var images=(p.imgs||[]).map(fixImg).filter(Boolean);
if(image&&images.indexOf(image)===-1)images.unshift(image);
if(!image&&images.length)image=images[0];
return {
name:p.n,link:link,image:image,price:p.p,priceNum:p.pn,
cat:p.c,catSlug:p.cs,catIcon:p.ci,
subCat:p.sub_c||'',subCatSlug:p.sub_cs||'',
images:images,summary:p.sum||'',desc:p.desc||'',
specs:p.specs||{},feats:p.feats||[],sku:p.sku||'',
video:p.vid||'',model:p.mod||'',slug:p.slug||'',
brand:p.brand||defaultBrand,
_src:defaultBrand,
_idx:(p.sku||'')+' '+(p.brand||'').toLowerCase()+' '+(p.n||'').toLowerCase()
};
}
// === FILTER: Loại bỏ sản phẩm Malloca ===
function isMallocaProduct(p){
var check = ((p.n || p.name || '') + ' ' + (p.brand || '') + ' ' + (p.sku || '') + ' ' + (p.mod || '') + ' ' + (p.model || '')).toLowerCase();
return check.indexOf('malloca') !== -1;
}
// === PURGE: Xóa tất cả Malloca có nguồn DMX khỏi mảng D ===
function purgeMallocaFromDMX(){
var before = D.length;
var newD = [];
for(var i=0; i<D.length; i++){
var p = D[i];
// Sản phẩm từ DMX có brand = "Điện Máy Xanh" hoặc _src = "Điện Máy Xanh"
var isDMX = (p._src === 'Điện Máy Xanh') ||
(p.brand && p.brand.toLowerCase().indexOf('điện máy xanh') !== -1) ||
(p.brand && p.brand.toLowerCase().indexOf('dien may xanh') !== -1) ||
(p.link && p.link.indexOf('dienmayxanh') !== -1);
if(isDMX){
var name = ((p.name || p.n || '') + ' ' + (p.brand || '') + ' ' + (p.sku || p.model || '')).toLowerCase();
if(name.indexOf('malloca') !== -1){
continue; // Skip — loại bỏ
}
}
newD.push(p);
}
// Replace D in-place
D.length = 0;
for(var j=0; j<newD.length; j++) D.push(newD[j]);
var removed = before - D.length;
if(removed > 0) console.log('[Loader v5] PURGED '+removed+' Malloca products from DMX. Remaining:',D.length);
}
function updateStats(){
var st=document.getElementById('statTotal');
if(st)st.textContent=D.length;
var sc=document.getElementById('statCats');
if(sc){var cats={};D.forEach(function(p){cats[p.catSlug]=true});sc.textContent=Object.keys(cats).length}
}
function loadSource(jsonFile,brand,label,filterFn){
return fetch(jsonFile).then(function(r){return r.json()}).then(function(data){
var existingSlugs={};
D.forEach(function(p){if(p.slug)existingSlugs[p.slug]=true});
var added=0,skipped=0;
data.forEach(function(p){
if(filterFn && filterFn(p)){skipped++;return;}
var mapped=mapProduct(p,brand);
if(!existingSlugs[mapped.slug]){
if(typeof buildSearchIndex==='function')mapped._idx=buildSearchIndex(mapped);
D.push(mapped);existingSlugs[mapped.slug]=true;added++;
}
});
console.log('['+label+'] Added',added,'products (skipped '+skipped+' filtered). Total:',D.length);
return added;
});
}
var checkInterval=setInterval(function(){
if(typeof D!=='undefined'&&D.length>0&&typeof init==='function'){
clearInterval(checkInterval);
console.log('[Loader v5] Main data:',D.length,'products. Loading external sources...');
// Load DMX (LỌC BỎ MALLOCA khi load) then Garis
loadSource('products_dmx_slugs.json','Điện Máy Xanh','DMX', isMallocaProduct)
.then(function(){return loadSource('products_garis_slugs.json','Garis','Garis')})
.then(function(){
// SAU KHI load xong: purge ALL Malloca có nguồn DMX khỏi D
purgeMallocaFromDMX();
updateStats();
if(typeof init==='function')init();
})
.catch(function(e){console.warn('[Loader] Error:',e);purgeMallocaFromDMX();updateStats();if(typeof init==='function')init()});
}
},500);
setTimeout(function(){clearInterval(checkInterval)},30000);
})();