I am trying to pass the following javascript program as a string to my WebView in my react native app but it keeps failing and I can not figure out why. Can someone see what I am missing?
export function extractor(paths) {
const pathsString = JSON.stringify(paths)
const functionString = `
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
function GetListingInfo(elements) {
const titleKeys = Object.keys(elements).filter((element) => {
return element.includes('title')
})
const priceKeys = Object.keys(elements).filter((element) => {
return element.includes('price')
})
let title = getContent(titleKeys, elements);
let price = getContent(priceKeys, elements);
return { title, price };
}
function getContent(keys, elements) {
let textContent
for( var i in keys ) {
let key = keys[i]
let xpath = elements[key]
textContent = getElementByXpath(xpath)? getElementByXpath(xpath).textContent : null;
if(textContent != null) {
return textContent
}
}
return null;
}
let content = GetListingInfo(JSON.parse(${pathsString}));
title = content['title']? content['title']: '';
price = content['price']? content['price'] : null;
let sale_price;
if(request.sale_priceXpath) {
sale_price = getElementByXpath(request.sale_priceXpath)? getElementByXpath(request.sale_priceXpath).textContent: null;
}
price = sale_price? sale_price: price;
window.alert({title, price});
window.alert(content)
true;
`
return functionString;
}
the path object I am trying to pass is the following:
{
"priceXpath": "//span[contains(concat(" ",normalize-space(@class)," ")," style__PriceFontSize-sc-1o3i6gc-0 ")]",
"price_1Xpath": "//span[@data-test="product-price"]",
"status": "Enrolled",
"titleXpath": "//h1[contains(concat(" ",normalize-space(@class)," ")," Heading__StyledHeading-sc-1mp23s9-0 ")]//span",
}
is it sometype of element or quotation that is making this invalid and unable to execute?
Thank you for taking the time to look at this post :)