document.addEventListener("DOMContentLoaded", function() {
// جميع محددات العناصر الممكنة لوصف المنتجات والمدونات في ثيمات سلة المختلفة
const selectors = [
".product-details__description",
".product__description",
"#description",
".description-content",
".product__description article",
"article",
".pro-details",
"div[data-salla-product-description]",
"salla-product-details",
".product-description",
".s-product-card-description",
".description"
];
function processContent(targetElement) {
if (!targetElement || targetElement.dataset.processed) return;
let htmlContent = targetElement.innerHTML;
let isUpdated = false;
// 1. معالجة الجداول (مع تنظيف الأوسمة الخفية للـ HTML)
const tableRegex = /\|جدول\|([\s\S]*?)\|الجدول\|/gi;
if (tableRegex.test(htmlContent)) {
htmlContent = htmlContent.replace(tableRegex, function (match, tableContent) {
// تنظيف النص من أوسمة p و span و br لضمان قراءة الأسطر بدقة
const cleanText = tableContent
.replace(/
/gi, "\n")
.replace(/<\/p>/gi, "\n")
.replace(/
]*>/gi, "")
.replace(/<[^>]*>/g, "")
.replace(/ /g, " ");
const rows = cleanText.split("\n").filter(row => row.trim() !== "");
let tableHTML = '
';
let isFirstRow = true;
rows.forEach(function (row) {
let cleanRow = row.trim();
if (!cleanRow) return;
// تنظيف أي شريطة | في بداية أو نهاية السطر لو كانت زائدة
if (cleanRow.startsWith('|')) cleanRow = cleanRow.substring(1);
if (cleanRow.endsWith('|')) cleanRow = cleanRow.slice(0, -1);
const cols = cleanRow.split("|").map(col => col.trim());
if (cols.length === 0 || (cols.length === 1 && cols[0] === "")) return;
if (isFirstRow) {
tableHTML += "";
cols.forEach(col => tableHTML += "| " + col + " | ");
tableHTML += "
";
isFirstRow = false;
} else {
tableHTML += "";
cols.forEach(col => tableHTML += "| " + col + " | ");
tableHTML += "
";
}
});
tableHTML += "
";
isUpdated = true;
return tableHTML;
});
}
// 2. معالجة الأسئلة الشائعة
const faqRegex = /\|اسئلة\|([\s\S]*?)\|نهاية\|/gi;
if (faqRegex.test(htmlContent)) {
const faqData = [];
htmlContent = htmlContent.replace(faqRegex, function(match, faqContent) {
const cleanText = faqContent
.replace(/
/gi, "\n")
.replace(/<\/p>/gi, "\n")
.replace(/]*>/gi, "")
.replace(/<[^>]*>/g, "")
.replace(/ /g, " ");
const lines = cleanText.split('\n').filter(line => line.trim() !== '');
let faqHTML = '
';
for (let i = 0; i < lines.length; i++) {
let line = lines[i].trim();
if (!line) continue;
line = line.replace(/^س\s*:\s*/, '');
let parts = line.split(/ج\s*:\s*/);
let question = parts[0].trim();
let answer = parts[1] ? parts[1].trim() : '';
if (question) {
if (!answer && i + 1 < lines.length && (lines[i+1].trim().startsWith('ج:') || lines[i+1].trim().startsWith('ج :'))) {
answer = lines[i+1].trim().replace(/^ج\s*:\s*/, '').trim();
i++;
}
faqData.push({ question: question, answer: answer });
faqHTML += '
';
faqHTML += '
' + question + '
';
faqHTML += '
' + (answer || 'لا توجد إجابة متاحة حالياً.') + '
';
faqHTML += '
';
}
}
faqHTML += '
';
if (faqData.length > 0) {
addFAQSchema(faqData);
}
isUpdated = true;
return faqHTML;
});
}
if (isUpdated) {
targetElement.innerHTML = htmlContent;
attachAccordionEvents(targetElement);
}
targetElement.dataset.processed = "true";
}
function attachAccordionEvents(container) {
container.querySelectorAll('.salla-faq-trigger').forEach(trigger => {
trigger.addEventListener('click', function() {
const item = this.parentElement;
container.querySelectorAll('.salla-faq-item').forEach(el => {
if (el !== item) el.classList.remove('active');
});
item.classList.toggle('active');
});
});
}
function addFAQSchema(faqData) {
const oldSchema = document.getElementById('faq-schema');
if (oldSchema) oldSchema.remove();
const schemaData = {
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": []
};
for (let i = 0; i < faqData.length; i++) {
if (!faqData[i].answer) continue;
schemaData.mainEntity.push({
"@type": "Question",
"name": faqData[i].question,
"acceptedAnswer": {
"@type": "Answer",
"text": faqData[i].answer
}
});
}
const script = document.createElement('script');
script.id = 'faq-schema';
script.type = 'application/ld+json';
script.text = JSON.stringify(schemaData);
document.head.appendChild(script);
}
function initializeProcessing() {
selectors.forEach(selector => {
document.querySelectorAll(selector).forEach(processContent);
});
}
initializeProcessing();
// مراقبة المحتوى الديناميكي
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
mutation.addedNodes.forEach(node => {
if (node.nodeType === 1) {
selectors.forEach(selector => {
if (node.matches(selector)) processContent(node);
node.querySelectorAll(selector).forEach(processContent);
});
}
});
}
});
});
observer.observe(document.body, { childList: true, subtree: true });
// تشغيل احتياطي متكرر لضمان السحب بعد التحميل البطين في سلة
setTimeout(initializeProcessing, 500);
setTimeout(initializeProcessing, 1500);
setTimeout(initializeProcessing, 3000);
});
(function () {
function fixSlider() {
var el = document.querySelector('#main-links-1');
if (!el) return;
// Swiper عادة يخزن نسخته على العنصر نفسه أو داخل shadow DOM لدى مكونات سلا
var swiperInstance = el.swiper || (el.shadowRoot && el.shadowRoot.querySelector('.swiper')?.swiper);
if (swiperInstance) {
swiperInstance.params.watchOverflow = false;
swiperInstance.allowSlideNext = true;
swiperInstance.allowSlidePrev = true;
swiperInstance.navigation.update();
swiperInstance.update();
}
}
// ينتظر تحميل المكون (Stencil web component) قبل ما يحاول
window.addEventListener('load', function () {
setTimeout(fixSlider, 800);
});
// في حال المكون يعيد الرندر (مثلا بعد تغيير حجم الشاشة)
window.addEventListener('resize', function () {
setTimeout(fixSlider, 300);
});
})();