Need to fix description and spec

This commit is contained in:
dvirlabs 2025-06-17 02:06:04 +03:00
parent be92c28c55
commit a3306bf955

View File

@ -1,22 +1,63 @@
document.addEventListener("DOMContentLoaded", async () => {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
// Inject inject.js into page
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => {
const script = document.createElement("script");
script.src = chrome.runtime.getURL("inject.js");
document.documentElement.appendChild(script);
script.remove();
return new Promise((resolve) => {
try {
const title = document.querySelector('[data-pl="product-title"]')?.innerText || '';
let images = [];
let specs = {};
// חפש סקריפט שמכיל runParams
const scripts = Array.from(document.scripts);
const runParamsScript = scripts.find(s => s.textContent.includes('runParams'));
if (runParamsScript) {
// --- תמונות ---
const match = runParamsScript.textContent.match(/"imagePathList":\[(.*?)\]/s);
if (match && match[1]) {
images = match[1]
.split(',')
.map(url => url.replace(/"/g, '').trim())
.filter(url => url.startsWith('https'));
}
// --- מאפיינים (specs) ---
const specsMatch = runParamsScript.textContent.match(/"specifications":(\[.*?\])\s*,\s*"props"/s);
if (specsMatch && specsMatch[1]) {
try {
const raw = specsMatch[1]
.replace(/([{,])(\s*)(\w+)(\s*):/g, '$1"$3":') // מוסיף גרשיים למפתחות
.replace(/'/g, '"'); // מחליף גרשיים בודדים
const specsJson = JSON.parse(raw);
specsJson.forEach(group => {
(group.attributes || []).forEach(attr => {
specs[attr.attrName] = attr.attrValue;
});
});
} catch (e) {
console.log("Failed to parse specs JSON:", e);
}
}
}
resolve({
title,
images,
description: '', // ניתן להוסיף בשלב הבא
specs
});
} catch (err) {
console.error("Error extracting AliExpress data:", err);
resolve({ title: '', images: [], specs: {}, description: '' });
}
});
// Wait for content.js to relay message from injected script
chrome.runtime.onMessage.addListener(function listener(message) {
if (message?.type === "PRODUCT_DATA") {
const data = message.data;
}
}, (results) => {
const data = results?.[0]?.result || {};
const output = document.getElementById("output");
output.textContent = JSON.stringify(data, null, 2);
@ -24,8 +65,5 @@ document.addEventListener("DOMContentLoaded", async () => {
navigator.clipboard.writeText(output.textContent);
alert("Copied!");
};
chrome.runtime.onMessage.removeListener(listener);
}
});
});