Skip to content

Instantly share code, notes, and snippets.

@Wattenberger
Last active February 6, 2018 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Wattenberger/a7819168414a727f013d11704c843c9b to your computer and use it in GitHub Desktop.
Save Wattenberger/a7819168414a727f013d11704c843c9b to your computer and use it in GitHub Desktop.
Xpath twin finder
var mainTextStyles = [
"color: #fff",
"background: #5ba745",
"font-weight: bold",
"display: block",
].join(";")
var errorStyles = [
"color: #fff",
"background: #D05C4D",
"font-weight: bold",
"display: block",
].join(";")
var xpathStyles = [
"color: #fff",
"background: #FBD481",
"font-weight: bold",
"display: block",
].join(";")
var elemStyles = [
"color: #666",
"font-weight: bold"
].join(";")
var dividerStyles = [
"color: #ccc",
].join(";")
if (typeof ParselyOverlay == "undefined") {
// console.log(`%c Parse.ly Overlay not detected`, errorStyles)
} else {
var isNotOnlyInXpath = (i, elem) => {
const xpathElem = ParselyOverlay.elemFromXPath(ParselyOverlay.getElementXPath(elem))[0]
return elem != xpathElem
}
var links = $("#parsely_site_body_wrapper a")
console.log(`%c ${links.length} links found`, mainTextStyles)
console.log("%c Searching...", elemStyles)
var invalidXpaths = links.filter(isNotOnlyInXpath).map((i, elem) => {
const xpath = ParselyOverlay.getElementXPath(elem)
const xpathTwin = ParselyOverlay.elemFromXPath(xpath)
return {
elem,
xpath,
xpathTwin,
}
})
if (invalidXpaths.length) {
console.log(`%c ${invalidXpaths.length} invalid xpaths`, errorStyles)
console.groupCollapsed("xpath twins")
invalidXpaths.each((i, elem) => {
console.log(`%c ------`, dividerStyles)
console.log(`xpath:`)
console.log(`%c ${elem.xpath}`, xpathStyles)
console.log(`%c elem:`, elemStyles, elem.elem)
console.log(`%c xpathTwin:`, elemStyles, elem.xpathTwin)
})
console.groupEnd()
console.log(invalidXpaths)
} else {
console.log("%c No duplicate xpaths - we all good!", mainTextStyles)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment