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

What is the recommended life cycle method to use with Redux?

What is the recommended approach for accessing data from the redux store in props? ...

"Upon the second click, the Ajax success function will display the returned view

When using a post request in angular's factory with ajax, everything seems to be working fine except for the success function. Upon clicking the login button, it seems like I have to click it twice to navigate to another page (logout.html). I'm n ...

What is the appropriate utilization of the await keyword within concise if-else statements in JavaScript?

Along with transitioning away from jQuery, my main focus is to make my code more efficient. In large scale enterprise applications, the excessive use of jQuery and JavaScript can become problematic. I have decided to switch back to vanilla JavaScript for ...

Encountering deployment problems with React and TypeScript involving router on Github Pages

After successfully running locally, I encountered a 404 error when deploying the website using "npm run deploy." My application is built with React and TypeScript, utilizing react-router-dom BrowserRouter for navigation between pages. I've spent 7 h ...

Tips for confirming schedule accuracy

Trying to determine if a specific time falls between two others is the task at hand. Allow me to illustrate: Presently, it's Thursday, and the time reads 11:39 PM. Establishment X operates from 12:00 AM to 11:59 PM on Thursdays (a regular occurrence ...

I am looking to implement a permanent change using PHP Ajax

I am facing an issue with the "Add as Buddy" button on my webpage. I want it to change permanently to "Pending Request" once clicked, but it keeps reverting back to "Add as Buddy" whenever I refresh the page. Can anyone suggest a solution for this problem? ...

how to execute a javascript function in an ionic controller from the template

My journey in creating my first Ionic app has been smooth so far. I am not well-versed in UI design, so I may not be implementing the best practices, but I am making progress nonetheless... Within a template, I have encountered the following code snippet ...

Establish communication between two Sails applications

I am currently working on a Sails.js project that features a member portal with all possible routes and actions. However, I am considering developing a CMS using Sails as well. Originally, my plan was to integrate the CMS into the existing project, but n ...

Can one button be clicked to trigger the activation of another button through a click?

Just as the title suggests, I am wondering how to make this happen? Any guidance would be greatly appreciated! $('.notVisible').click (function(){ alert("I'm the invisible button") }); $('.visible').click (function(){ al ...

Customized Quick Access MUI

Hey there! I'm currently working with the Speed ​​dial component for MUI and I'm having trouble figuring out how to style the tooltipTytle. I can only seem to style them when they are all displayed at once, but that's not what I want. I ...

Border becomes dashed when dragging and dropping

I am working on enabling drag and drop functionality for users. When an item is lifted from its original position, a gray dashed box should appear in its place. As the item is moved closer to another spot, the containers adjust to create a target area (gra ...

The functionality for validating and submitting forms using Ajax is not functioning as expected

My input field validation is functioning properly, but I am having trouble with the ajax submission. Can anyone help me find the bug in my jQuery code snippet? $(document).ready(function() { $("#eValidate").live("click", function() { if (!Valida ...

Encountering difficulties setting cookies within the app router in Next.js

I've been diving into next.js and I'm trying to figure out how to set a cookie using the code below. However, I'm encountering an error: "Unhandled Runtime Error. Error: Cookies can only be modified in a Server Action or Route Handler." I ...

Listening for select elements that have remained unchanged

My current situation involves a select box with predetermined options. One of these options is preselected when the page loads: <select name="select" class="js-choice-select"> <option value="option-1">Option 1</option> <option ...

Is it possible to redefine a function that is attached to $ctrl outside of the AngularJS framework?

Within my DOM, there exists an element containing ng-click="$ctrl.goToHome(), which is connected to the logo on my site. This particular element is generated by a third-party AngularJS application. The complete element can be seen below: <img ng-if=":: ...

Implementing class styles using jQuery; however, certain styles fail to be effectively applied

As a beginner, I am experimenting with applying CSS class styles using jQuery. Specifically, I am trying to set two class attributes: color and font size. Surprisingly, only the color attribute is being applied despite both attributes being defined under t ...

Calculations for jQuery UI sliders

On the page, there are 2 sliders that control the number of days and the loan amount. There is a specific calculation function for this: function calculate(){ var days = $("#days-value").val(); var amount = $(".loan-amount-value").val(); ...

POST request in Ajax with NodeJs/MongoDB/Mongoose causing an Uncaught TypeError: Illegal invocation

Whenever I attempt to make a POST request using Ajax/Jquery, I keep encountering an error in the console. The specific errors are on lines 67 and 31 in the createTeam.js file: $.ajax({ //line 67 sendInvitation(teamID,_companyName,teamName) //lin ...

Interactive element for initiating a HTTP request to NodeJS/Express server to trigger a specific function

I currently have a frontend button implemented using nodejs and express for my server-side backend. The button is supposed to trigger a function that controls the Philips Hue API on the backend via an HTTP request. I have experimented with various approac ...

Is there a new update to Google Maps API?

After successfully building a map component for a web application using google maps, open layers, and the dojo toolkit, it suddenly stopped loading earlier this morning. Even though there are no JavaScript errors and open layers and Google still initialize ...