How to choose elements using jQuery according to their CSS properties?

Seeking a solution to a unique challenge I've encountered. The website I frequent has found a way to bypass my uBlock extension by using a script that generates random element classes, such as:

<div class="lcelqilne1471483619510ttupv"></div>
<div class="xgdxts1471483770461tkbfjhcr"></div>

Because the string changes each time, consistently selecting it proves to be difficult. This class overlays the site and prompts users to click on it, leading to pop-up ads utilizing some sort of Base64 exploit to display new tabs or pop-ups. While the element is invisible, it does have certain CSS variables that could potentially allow for selection by class/ID. However, my knowledge of how to achieve this through javascript/jQuery (with greasemonkey) is limited. Additionally, I am seeking help in hiding or blocking this popup.

display: block !important;
visibility: visible !important;
top: 0px !important;
left: 0px !important;
position: absolute !important;
z-index: 999999 !important;

In essence, I am looking for a way to target and hide/block this element based on its CSS attributes rather than the element name. http://prntscr.com/c7474u

Answer №1

Utilize the .filter() method to verify the computed style for specific settings you are seeking.

$("div[class]").filter(function() {
    if (this.className.length != 27) { // Avoid costly style check if we do not have a random-looking class
        return false;
    }
    var style = getComputedStyle(this);
    return (style.visibility == 'visible' && style.top == '0px' && style.left == '0px' && style.position == 'absolute' && style.zIndex == 999999);
}).hide();

This operation may be resource-intensive. Improving the selector could enhance performance.

Answer №2

To identify elements with a className of exactly 27 characters, consisting of lowercase letters followed by digits and then more lowercase letters, you can utilize

document.querySelectorAll("[class]")
, Array.prototype.filter(), and RegExp.prototype.test().

var filter = [].filter.call(document.querySelectorAll("[class]"), function(el) {
    return el.className.length === 27 && /[a-z]+[0-9]+[a-z]+/.test(el.className);
});    
console.log(filter);
// perform actions
filter[0].style.color = "blue";
<div class="xgdxts1471483770461tkbfjhcr">xgdxts1471483770461tkbfjhcr</div>
<div class="abc456wyx">abc</div>
<div class="123def789">123</div>

Answer №3

If you're looking for a CSS solution based on the provided markup in the screenshot, one option to consider is:

body > div:last-child {
    display: none !important;
}

This approach is effective because body > div:last-child has higher specificity compared to .xgdxts1471483770461tkbfjhcr (assuming how the selector is applied, as details weren't included in your initial post)

For example:

.xgdxts1471483770461tkbfjhcr {
display: block !important;
visibility: visible !important;
top: 0px !important;
left: 0px !important;
position: absolute !important;
z-index: 999999 !important;
}
body > div:last-child {
display: none !important;
}
<body>
    <div id="fb-root"></div>
    <div style="display:none"></div>
    <div class="xgdxts1471483770461tkbfjhcr">Hello</div>
</body>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve a parameter from jQuery and pass it to a JavaScript function

Currently, I am working with Puppeteer and encountering an issue where the "selector" is undefined when calling this function. async function checkIfTextIsPresent(page, selector){ let myElement = await page.evaluate(() => document.querySelector(sel ...

Differences Between Data Captured from Form Submissions and Data Obtained Through Ajax

When attempting to incorporate reCAPTCHA into my MVC site, I encountered an issue where it would not validate unless submitted from a form. Here is the current implementation: @using(Html.BeginForm("VerifyCaptcha", "Signup") ) { @ReCaptch ...

The error message "reload is not defined" indicates that the function reload

Initially, I encountered the error TypeError: require(...) is not a function, prompting me to add a semicolon at the end of require("./handlers/slashcommands"). However, this led to a new issue: ReferenceError: reload is not defined. This occurre ...

I am encountering an issue where body-parser is not functioning properly with typescript. Whenever I make a request, the request.body is returning as undefined

Below is the code snippet for my Express application using TypeScript version 3.7.4: import bodyParser from "body-parser"; import config from "config"; import cookieParser from "cookie-parser"; import express from "express"; import mongoose from "mongoose ...

Handling Errors in Asynchronous Functions with JavaScriptLet's explore the best practices for

I am a beginner in javascript and recently delved into async/await. After going through various resources, I gained a basic understanding. However, while experimenting with some code examples, I encountered unexpected results which left me puzzled about wh ...

Avoid making GET requests when clicking on a link

[UPDATE] I need help troubleshooting an issue with my ajax request. Here is the code snippet that I am working on: <a href="" class="undo_feedback">Undo</a> When I click on the link, it triggers an ajax POST request, but I encounter an error ...

Changing the size of a responsive navigation bar with React and adjusting it based on the window.scrollY position switches between collapsed and un

I'm struggling to create a responsive navbar that automatically adjusts its size once it surpasses the height of the navbar (currently set at 80px). However, when I scroll to around the 80px mark, it starts flickering between the collapsed and expande ...

Once the promise program enters the if-condition, even though the condition itself is actually false

There seems to be an issue with retrieving the location code using the AccuWeather API before getting the weather data for a city. Despite the location array being empty, the program still proceeds to a branch that expects a non-empty array. router.get(& ...

Implement a new list field to an object using javascript

I am facing a challenge in converting a JSON object to a list of JSON objects and then adding it back to JSON. Here is an example: config = { existing_value: 'sample' } addToListing = (field, value, index=0) => { config = { ...confi ...

Utilizing a Material UI custom theme in React with Typescript: A step-by-step guide

Upon using the tool , I received a js file and a json file following the paths mentioned on the theme generator page: // src/ui/theme/index.js /* src/ui/theme/theme.json */ The files operate smoothly when left with the .js extension. However, when I attem ...

The Viadeo Social Toolbox seems to be encountering technical difficulties at the moment

I attempted to utilize a Viadeo Social Toolbox, specifically the Viadeo Share Button, but it seems to be malfunctioning in certain browsers? I came across some outdated viadeo share URLs like this: http://www.viadeo.com/shareit/share/?url=${url}&title ...

The width of the child box does not properly receive the 100% application

Artistic Inspiration I have created a unique and elegant card layout design. * { box-sizing: border-box; } .card { display: flex; width: 600px; height: 400px; } .card > .img-window { width: 100%; background-image: url('https://s3- ...

Steer your keyboard attention towards the parent element that embodies a list

My implementation focuses on making drop down menus accessible via keyboard input using HTML/CSS and JS/jQuery events. The goal of keyboard accessibility includes: Tab key to navigate the menu elements. Pressing the down arrow key opens a focused menu. ...

Tips on transferring key values when inputText changes in ReactJs using TypeScript

I have implemented a switch case for comparing object keys with strings in the following code snippet: import { TextField, Button } from "@material-ui/core"; import React, { Component, ReactNode } from "react"; import classes from "./Contact.module.scss" ...

``Can you share your insights on transferring data from one controller to a service, and then accessing that data on another controller during the same

I'm encountering an issue with passing data from one controller to another using a service. To achieve this, I have implemented prototype inheritance using the $rootScope in my controller and broadcasting the object so that other controllers can acce ...

What about creating a PHP-MySQL voting platform?

I have implemented a PHP-MySQL voting system on my website, similar to YouTube's. I am using AJAX to execute the PHP in newtest.php. The PHP script works fine when tested individually, but when trying to implement the voting functionality through AJAX ...

Is there a way to determine the quantity of child objects and transmit the calculated index to each individual child object?

My data is structured as shown below: team1 : { author92 : "John" , author43 : "Smith" }, team2 : { author33 : "Dolly", author23 : "Mark" }, I want to display Authors grouped together with an ad ...

Steps for achieving a seamless transition in jqueryui

I currently have a jQuery toggle function that removes several divs from a webpage: $("a#list-mode").click(function() { $("a#list-mode").toggleClass("rm-toggle"); $("a#map-mode").toggleClass("rm-toggle"); $("div.rm-ta").t ...

JQuery UI autocomplete vanishes instantly without any warning

I am encountering an issue with JQuery UI's autocomplete feature where the dropdown results do not stay visible. While debugging, I noticed that the list briefly appears before disappearing. Below is my code snippet: HTML: <input type="text" plac ...

The rendering of the Angular 2 D3 tree is not functioning properly

Attempting to transition a tree created with d3 (v3) in vanilla JavaScript into an Angular2 component has been challenging for me. The issue lies in displaying it correctly within the component. Below is the code snippet from tree.component.ts: import { ...