Need to fix description and spec
This commit is contained in:
parent
be92c28c55
commit
a3306bf955
80
popup.js
80
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!");
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user