Skip to content

Instantly share code, notes, and snippets.

@dbarjs
Last active February 16, 2021 19:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbarjs/4ac3a654224b372c13a9b993f75b1829 to your computer and use it in GitHub Desktop.
Save dbarjs/4ac3a654224b372c13a9b993f75b1829 to your computer and use it in GitHub Desktop.
kill_btb21
// 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;
}
// test
findRealPlayerElement('Nego Di');
@dbarjs
Copy link
Author

dbarjs commented Feb 10, 2021

Fica aberto para quem quiser utilizar em algum projeto de crawler.

Melhorias são bem vindas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment