Need to fix description and spec
This commit is contained in:
parent
be92c28c55
commit
a3306bf955
66
popup.js
66
popup.js
@ -1,22 +1,63 @@
|
|||||||
document.addEventListener("DOMContentLoaded", async () => {
|
document.addEventListener("DOMContentLoaded", async () => {
|
||||||
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
||||||
|
|
||||||
// Inject inject.js into page
|
|
||||||
chrome.scripting.executeScript({
|
chrome.scripting.executeScript({
|
||||||
target: { tabId: tab.id },
|
target: { tabId: tab.id },
|
||||||
func: () => {
|
func: () => {
|
||||||
const script = document.createElement("script");
|
return new Promise((resolve) => {
|
||||||
script.src = chrome.runtime.getURL("inject.js");
|
try {
|
||||||
document.documentElement.appendChild(script);
|
const title = document.querySelector('[data-pl="product-title"]')?.innerText || '';
|
||||||
script.remove();
|
|
||||||
|
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
|
}, (results) => {
|
||||||
chrome.runtime.onMessage.addListener(function listener(message) {
|
const data = results?.[0]?.result || {};
|
||||||
if (message?.type === "PRODUCT_DATA") {
|
|
||||||
const data = message.data;
|
|
||||||
|
|
||||||
const output = document.getElementById("output");
|
const output = document.getElementById("output");
|
||||||
output.textContent = JSON.stringify(data, null, 2);
|
output.textContent = JSON.stringify(data, null, 2);
|
||||||
|
|
||||||
@ -24,8 +65,5 @@ document.addEventListener("DOMContentLoaded", async () => {
|
|||||||
navigator.clipboard.writeText(output.textContent);
|
navigator.clipboard.writeText(output.textContent);
|
||||||
alert("Copied!");
|
alert("Copied!");
|
||||||
};
|
};
|
||||||
|
|
||||||
chrome.runtime.onMessage.removeListener(listener);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user