parse-sahibinden/content.js
2024-03-08 16:41:10 +03:00

73 lines
2.2 KiB
JavaScript

if (window.location.href.includes("sahibinden.com")) {
const title = document.querySelector('div.classifiedDetailTitle>h1');
const button = document.createElement("button");
button.innerText = "Parse it"
title.appendChild(button)
const priceTag = document.querySelector('div.classifiedInfo>h3');
const locationTag = document.querySelector('div.classifiedInfo > h2 ');
const infoTag = document.querySelector('div.classifiedInfo>ul.classifiedInfoList ');
const desc = document.querySelector("div#classifiedDescription > p")
if (title) {
const dataObject = {
content: title.textContent, // Adjust this based on your needs
};
console.log(dataObject )
const priceObj = priceTag.textContent.split("\n")
const price = priceObj.find(item=>item.trim()).trim()
console.log(price)
const location = locationTag.textContent.split("\n").map(item=>item.trim()).filter(item=>item).join(' ')
console.log(location)
const description = document.querySelector('.classifiedDescription');
if (infoTag) {
const infoItems = infoTag.querySelectorAll('li');
const resultObject = {};
infoItems.forEach((li) => {
const fieldName = li.querySelector('strong').textContent.trim();
const fieldValue = li.querySelector('span').textContent.trim();
resultObject[fieldName] = fieldValue;
});
console.log(resultObject)
}
console.log(desc.innerText)
const h3Elements = description.querySelectorAll('h3');
const descriptionObject = {};
h3Elements.forEach((h3) => {
const category = h3.textContent.trim();
const ul = h3.nextElementSibling;
if (ul && ul.tagName === 'UL') {
const liElements = ul.querySelectorAll('li');
const itemsInfo = [];
liElements.forEach((li) => {
const itemText = li.textContent.trim();
const isSelected = li.classList.contains('selected');
// Add information about the item to the array
itemsInfo.push({
item: itemText,
selected: isSelected,
});
});
descriptionObject[category] = itemsInfo;
}
});
console.log(descriptionObject);
// Send the data to the background script
// chrome.runtime.sendMessage({ data: dataObject });
}
}