From a3306bf9551bcd40a6e97b88a165568edffb3a24 Mon Sep 17 00:00:00 2001 From: dvirlabs Date: Tue, 17 Jun 2025 02:06:04 +0300 Subject: [PATCH] Need to fix description and spec --- popup.js | 80 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 21 deletions(-) diff --git a/popup.js b/popup.js index 1a55dd1..07d8c06 100644 --- a/popup.js +++ b/popup.js @@ -1,31 +1,69 @@ 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: '' }); + } + }); } - }); + }, (results) => { + const data = results?.[0]?.result || {}; + const output = document.getElementById("output"); + output.textContent = JSON.stringify(data, null, 2); - // 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; - - const output = document.getElementById("output"); - output.textContent = JSON.stringify(data, null, 2); - - document.getElementById("copyBtn").onclick = () => { - navigator.clipboard.writeText(output.textContent); - alert("Copied!"); - }; - - chrome.runtime.onMessage.removeListener(listener); - } + document.getElementById("copyBtn").onclick = () => { + navigator.clipboard.writeText(output.textContent); + alert("Copied!"); + }; }); });