feat: wrap scrape & scrapeSchema in IIFE

This commit is contained in:
karishmas6
2024-08-02 19:07:18 +05:30
parent 2aa7cdc4bc
commit 8540905192

View File

@@ -130,7 +130,14 @@ function scrapableHeuristics(maxCountPerPage = 50, minArea = 20000, scrolls = 3,
* Returns a "scrape" result from the current page. * Returns a "scrape" result from the current page.
* @returns {Array<Object>} *Curated* array of scraped information (with sparse rows removed) * @returns {Array<Object>} *Curated* array of scraped information (with sparse rows removed)
*/ */
function scrape(selector = null) { // Wrap the entire function in an IIFE (Immediately Invoked Function Expression)
// and attach it to the window object
(function(window) {
/**
* Returns a "scrape" result from the current page.
* @returns {Array<Object>} *Curated* array of scraped information (with sparse rows removed)
*/
window.scrape = function(selector = null) {
/** /**
* **crudeRecords** contains uncurated rundowns of "scrapable" elements * **crudeRecords** contains uncurated rundowns of "scrapable" elements
* @type {Array<Object>} * @type {Array<Object>}
@@ -173,7 +180,7 @@ function scrape(selector = null) {
})); }));
return crudeRecords; return crudeRecords;
} };
/** /**
* Given an object with named lists of elements, * Given an object with named lists of elements,
@@ -181,7 +188,7 @@ function scrape(selector = null) {
* @param {Object.<string, object[]>} lists The named lists of HTML elements. * @param {Object.<string, object[]>} lists The named lists of HTML elements.
* @returns {Array.<Object.<string, string>>} * @returns {Array.<Object.<string, string>>}
*/ */
function scrapeSchema(lists) { window.scrapeSchema = function (lists) {
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)
@@ -224,3 +231,6 @@ function scrapeSchema(lists) {
(listOfElements) => listOfElements.find((elem) => mbe.contains(elem))?.innerText, (listOfElements) => listOfElements.find((elem) => mbe.contains(elem))?.innerText,
)); ));
} }
})(window);