feat: add functionality to scrape shadowDOM elements

This commit is contained in:
RohitR311
2024-12-30 01:24:32 +05:30
parent 542f4d31fa
commit b60f4b73b8

View File

@@ -189,6 +189,7 @@ function scrapableHeuristics(maxCountPerPage = 50, minArea = 20000, scrolls = 3,
* @returns {Array.<Object.<string, string>>} * @returns {Array.<Object.<string, string>>}
*/ */
window.scrapeSchema = function (lists) { window.scrapeSchema = function (lists) {
// These utility functions remain unchanged as they work perfectly
function omap(object, f, kf = (x) => x) { function omap(object, f, kf = (x) => x) {
return Object.fromEntries( return Object.fromEntries(
Object.entries(object) Object.entries(object)
@@ -203,11 +204,39 @@ function scrapableHeuristics(maxCountPerPage = 50, minArea = 20000, scrolls = 3,
); );
} }
function getSeedKey(listObj) { function findElement(config) {
const maxLength = Math.max(...Object.values(omap(listObj, (x) => document.querySelectorAll(x.selector).length))); // If this is a shadow DOM query
return Object.keys(ofilter(listObj, (_, v) => document.querySelectorAll(v.selector).length === maxLength))[0]; if (config.shadow && config.selector.includes('>>')) {
const [hostSelector, shadowSelector] = config.selector.split('>>').map(s => s.trim());
const host = document.querySelector(hostSelector);
return host?.shadowRoot?.querySelector(shadowSelector) || null;
}
// Otherwise, use regular querySelector
return document.querySelector(config.selector);
} }
function findAllElements(config) {
// If this is a shadow DOM query
if (config.shadow && config.selector.includes('>>')) {
const element = findElement(config);
return element ? [element] : [];
}
// Otherwise, use regular querySelectorAll
return Array.from(document.querySelectorAll(config.selector));
}
// Modified to use our new element finding functions
function getSeedKey(listObj) {
const maxLength = Math.max(...Object.values(
omap(listObj, (x) => findAllElements(x).length)
));
return Object.keys(
ofilter(listObj, (_, v) => findAllElements(v).length === maxLength)
)[0];
}
// This function remains unchanged as it works with DOM elements
// regardless of how they were found
function getMBEs(elements) { function getMBEs(elements) {
return elements.map((element) => { return elements.map((element) => {
let candidate = element; let candidate = element;
@@ -224,33 +253,38 @@ function scrapableHeuristics(maxCountPerPage = 50, minArea = 20000, scrolls = 3,
} }
const seedName = getSeedKey(lists); const seedName = getSeedKey(lists);
const seedElements = Array.from(document.querySelectorAll(lists[seedName].selector)); const seedElements = findAllElements(lists[seedName]);
const MBEs = getMBEs(seedElements); const MBEs = getMBEs(seedElements);
return MBEs.map((mbe) => omap( return MBEs.map((mbe) => omap(
lists, lists,
({ selector, attribute }, key) => { (config, key) => {
const elem = Array.from(document.querySelectorAll(selector)).find((elem) => mbe.contains(elem)); // Use our new findAllElements function
const elem = findAllElements(config)
.find((elem) => mbe.contains(elem));
if (!elem) return undefined; if (!elem) return undefined;
switch (attribute) { switch (config.attribute) {
case 'href': case 'href': {
const relativeHref = elem.getAttribute('href'); const relativeHref = elem.getAttribute('href');
return relativeHref ? new URL(relativeHref, window.location.origin).href : null; return relativeHref ? new URL(relativeHref, window.location.origin).href : null;
case 'src': }
case 'src': {
const relativeSrc = elem.getAttribute('src'); const relativeSrc = elem.getAttribute('src');
return relativeSrc ? new URL(relativeSrc, window.location.origin).href : null; return relativeSrc ? new URL(relativeSrc, window.location.origin).href : null;
}
case 'innerText': case 'innerText':
return elem.innerText; return elem.innerText;
case 'textContent': case 'textContent':
return elem.textContent; return elem.textContent;
default: default:
return elem.innerText; return elem.getAttribute(config.attribute) || elem.innerText;
} }
}, },
(key) => key // Use the original key in the output (key) => key
)) || []; )) || [];
} };
/** /**
* Scrapes multiple lists of similar items based on a template item. * Scrapes multiple lists of similar items based on a template item.