Last active
February 16, 2021 19:22
-
-
Save dbarjs/4ac3a654224b372c13a9b993f75b1829 to your computer and use it in GitHub Desktop.
kill_btb21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BIG TRETA BRASIL ELEMENT FINDER | |
let GHOST_PROPERTIES = [ | |
{ property: 'overflow', value: 'hidden' }, | |
{ property: 'left', value: '-9999px' }, | |
{ property: 'display', value: 'none' }, | |
]; | |
let ANCESTOR_MAX_CHILD = 4; | |
function hasGhostAncestor(childElement, depth) { | |
if (hasGhostStyle(childElement.parentNode)) { | |
return true; | |
} | |
if (childElement?.parentNode?.childElementCount < ANCESTOR_MAX_CHILD) { | |
return hasGhostAncestor(childElement.parentNode); | |
} | |
return false; | |
} | |
function hasGhostStyle(element) { | |
const computedStyle = | |
element instanceof HTMLElement ? getComputedStyle(element) : null; | |
if (computedStyle) { | |
return GHOST_PROPERTIES.some( | |
({ property, value }) => computedStyle[property] === value | |
); | |
} | |
return true; | |
} | |
function findClickableAncestor(element) { | |
const computedStyle = | |
element instanceof HTMLElement ? getComputedStyle(element) : null; | |
if (computedStyle?.cursor === 'pointer' && computedStyle?.display === 'flex') { | |
return element; | |
} | |
if (element?.parentNode?.childElementCount < ANCESTOR_MAX_CHILD) { | |
return findClickableAncestor(element.parentNode); | |
} | |
return null; | |
} | |
function findRealPlayerElement(playerName) { | |
const list = Array.from(document.getElementsByTagName('div')) | |
.filter(({ innerHTML }) => innerHTML === playerName) | |
.filter((element) => !hasGhostAncestor(element)); | |
if (list?.length === 1) { | |
return findClickableAncestor(list[0]); | |
} | |
return null; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test | |
findRealPlayerElement('Nego Di'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fica aberto para quem quiser utilizar em algum projeto de crawler.
Melhorias são bem vindas.