Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sometimes blocked element reappeared again after refreshing the page #27269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 0 additions & 93 deletions components/cosmetic_filters/resources/data/element_picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,6 @@ class TargetsCollection {
});
}

getXpathsForMarked(): string[] {
return this.targets.map((el) => getElementXpath(el.element))
.filter((item): item is string => item !== null)
}

forceRecalcCoords() {
this.targets.forEach(t => t.forceRecalcCoords())
// for case when element no longer in the DOM
Expand Down Expand Up @@ -433,81 +428,10 @@ const hideByCssSelector = (selector: string) => {
style.innerText += `${selector} {display: none !important;}`
}

const hideByXPath = (xpath: string): void => {
const hideStyle: Partial<CSSStyleDeclaration> = {
display: 'none'
}
const result = document.evaluate(
xpath,
document,
null,
XPathResult.ORDERED_NODE_ITERATOR_TYPE,
null
);

let node: Node | null = result.iterateNext();

while (node) {
if (node instanceof HTMLElement) {
Object.assign(node.style, hideStyle);
}
node = result.iterateNext();
}
}

const getElementByXpath = (xpath: string) => {
const result = document.evaluate(
xpath,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
);

// Return the single node found, or null if none
return result.singleNodeValue as Element | null;
}

const getElementXpath = (element: Element | null): string => {
if (!element || !(element instanceof Element)) {
return ''
}

const getXPathSegment = (el: Element): string => {
const tagName = el.tagName.toLowerCase();

if (!el.parentElement) {
return `/${tagName}`
}

const siblings = Array.from(el.parentElement.children).filter(
(sibling) => sibling.tagName === el.tagName
);
if (siblings.length === 1) {
return `/${tagName}`
}

const index = siblings.indexOf(el) + 1
return `/${tagName}[${index}]`
};

const segments: string[] = []

while (element) {
segments.unshift(getXPathSegment(element))
element = element.parentElement!
}

return segments.join('')
}

const onTargetSelected = (selected: Element | null, index: number): string => {
if (lastHoveredElem === null) { return '' }

if (isAndroid) {
return getElementXpath(selected)
}

let elem: Element | null = selected
const selectorBuilders = []
const specificityMasks = [
Expand Down Expand Up @@ -559,13 +483,8 @@ const elementPickerHoverCoordsChanged = (x: number, y: number) => {

const getElementBySelector = (selector: string) => {
let elements: Element[] | null;
if (isAndroid) {
const singleElement = getElementByXpath(selector);
elements = singleElement ? [singleElement] : null;
} else {
const nodeList = document.querySelectorAll(selector);
elements = nodeList.length > 0 ? Array.from(nodeList) : null;
}
return elements
}

Expand Down Expand Up @@ -753,18 +672,6 @@ const launchElementPicker = (root: ShadowRoot) => {
if (createButton.classList.contains('block-button-disabled')) {
return
}
if (isAndroid) {
const selectedXpaths = targetedElems.getXpathsForMarked()
if(selectedXpaths && selectedXpaths.length > 0) {
for(const expr of selectedXpaths) {
const rule = `:xpath(${expr})`
api.cosmeticFilterCreate(rule)
hideByXPath(expr)
}
quitElementPicker()
}
return
}
const selector = rulesTextArea.value.trim()
if (selector.length > 0) {
api.cosmeticFilterCreate(selector)
Expand Down
Loading