(function() { 'use strict'; // CẤU HÌNH - Thay đổi theo nhu cầu const CONFIG = { // Tăng giá theo phần trăm (0 = không tăng theo %) increasePercent: 15, // Tăng giá theo số tiền cố định (0 = không tăng cố định) increaseAmount: 0, // Các selector để tìm element chứa giá priceSelectors: [ '.price', '.product-price', '.current-price', '.p-current-price', '[data-price]', '.amount', 'span:contains("₫")', 'span:contains("đ")' ], // Hiển thị mũi tên tăng giá showIncreaseIndicator: true, // Màu sắc cho giá đã tăng updatedPriceColor: '#e74c3c' }; // Class chính xử lý cập nhật giá class DynamicPriceUpdater { constructor(config) { this.config = config; this.originalPrices = new Map(); this.processed = new Set(); } init() { if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', () => this.start()); } else { this.start(); } } start() { this.addStyles(); this.updatePrices(); this.observeChanges(); } addStyles() { const style = document.createElement('style'); style.textContent = ` .price-updated { color: ${this.config.updatedPriceColor} !important; font-weight: bold; position: relative; } ${this.config.showIncreaseIndicator ? ` .price-updated::after { content: "↑"; color: #27ae60; font-size: 0.8em; margin-left: 3px; opacity: 0.8; }` : ''} `; document.head.appendChild(style); } updatePrices() { const elements = this.findPriceElements(); let updated = 0; elements.forEach(element => { if (this.updateSinglePrice(element)) { updated++; } }); if (updated > 0) { console.log(`Price Updater: Đã cập nhật ${updated} giá sản phẩm`); } } findPriceElements() { const elements = new Set(); // Tìm theo selector this.config.priceSelectors.forEach(selector => { try { document.querySelectorAll(selector).forEach(el => { if (this.isPriceElement(el) && !this.processed.has(el)) { elements.add(el); } }); } catch (e) { // Bỏ qua selector không hợp lệ } }); // Tìm element chứa ký hiệu tiền document.querySelectorAll('*').forEach(el => { if (el.children.length === 0 && this.isPriceElement(el) && !this.processed.has(el)) { elements.add(el); } }); return Array.from(elements); } isPriceElement(element) { const text = element.textContent.trim(); // Kiểm tra patterns giá const patterns = [ /[\d,\.]+\s*(?:đ|₫|VND|vnđ)/i, /^[\d,\.]+$/, /[\d,\.]+\s*(?:000|k|K)$/, /^\d{4,}$/ ]; return patterns.some(pattern => pattern.test(text)) && text.length > 0 && text.length < 50; } updateSinglePrice(element) { const elementId = this.getElementId(element); if (this.processed.has(element)) { return false; } const originalText = element.textContent.trim(); this.originalPrices.set(elementId, originalText); const numericPrice = this.extractNumericPrice(originalText); if (numericPrice === null || numericPrice < 1000) { return false; } let newPrice = numericPrice; if (this.config.increasePercent > 0) { newPrice = numericPrice * (1 + this.config.increasePercent / 100); } else if (this.config.increaseAmount > 0) { newPrice = numericPrice + this.config.increaseAmount; } else { return false; } const formattedPrice = this.formatPrice(newPrice, originalText); element.textContent = formattedPrice; element.classList.add('price-updated'); element.title = `Giá gốc: ${originalText}`; this.processed.add(element); return true; } extractNumericPrice(priceText) { let cleaned = priceText.replace(/[đ₫VND\s]/gi, ''); if (/k$/i.test(cleaned)) { cleaned = cleaned.replace(/k$/i, '000'); } cleaned = cleaned.replace(/[.,]/g, ''); const numeric = parseFloat(cleaned); return isNaN(numeric) ? null : numeric; } formatPrice(price, originalFormat) { const rounded = Math.round(price); if (originalFormat.includes('đ') || originalFormat.includes('₫')) { return this.addThousandSeparator(rounded) + 'đ'; } else if (originalFormat.includes('VND')) { return this.addThousandSeparator(rounded) + ' VND'; } else if (/k$/i.test(originalFormat) && rounded >= 1000) { return Math.round(rounded / 1000) + 'k'; } else { return this.addThousandSeparator(rounded); } } addThousandSeparator(num) { return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.'); } getElementId(element) { if (element.id) return element.id; let path = ''; let current = element; let index = 0; while (current && current !== document.body && index < 5) { let selector = current.tagName.toLowerCase(); if (current.className) { selector += '.' + current.className.split(' ')[0]; } path = selector + '>' + path; current = current.parentElement; index++; } return btoa(path + element.textContent).substring(0, 12); } observeChanges() { const observer = new MutationObserver((mutations) => { let shouldUpdate = false; mutations.forEach((mutation) => { if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { mutation.addedNodes.forEach((node) => { if (node.nodeType === Node.ELEMENT_NODE) { shouldUpdate = true; } }); } }); if (shouldUpdate) { setTimeout(() => this.updatePrices(), 100); } }); observer.observe(document.body, { childList: true, subtree: true }); } } // Khởi tạo tự động const priceUpdater = new DynamicPriceUpdater(CONFIG); priceUpdater.init(); // Expose global functions window.updatePriceConfig = function(newConfig) { Object.assign(CONFIG, newConfig); const newUpdater = new DynamicPriceUpdater(CONFIG); newUpdater.start(); }; window.increasePricesByPercent = function(percent) { window.updatePriceConfig({ increasePercent: percent, increaseAmount: 0 }); }; window.increasePricesByAmount = function(amount) { window.updatePriceConfig({ increasePercent: 0, increaseAmount: amount }); }; // Debug info console.log('Price Updater loaded:', { increasePercent: CONFIG.increasePercent, increaseAmount: CONFIG.increaseAmount, version: '1.0' }); })();
Địa chỉ: Tầng 9 - 201 Trường Chinh, Đống Đa, Hà Nội
Gần ngã ba Trường Chinh - Lê Trọng Tấn
Đỗ xe ô tô trước cửa
Thời gian mở cửa: 9:00 - 19:00
Điện thoại: 024 3933 0111
Hotline: 090 3443 0111
email: [email protected]
Dự án có sử dụng sản phẩm của IMASOFA
Trong giờ làm việc (9h - 19h), chúng tôi sẽ phản hồi ngay với bạn.
Và IMA xin phép phản hồi sớm nhất nếu ngoài giờ làm việc