Home > Blockchain >  How to check if a button exists?
How to check if a button exists?

Time:08-07

I'm learning to create a chrome extension. I want to create an extension to find a button and show alerts that found it

The button is

<button 
style="background-color: #fd7e14; margin:3px" disabled="">ABC - 01</button>

So how i can write a script to find that?

I have a script for showing the alert

chrome.browserAction.onClicked.addListener(function() { 
        alert("found it");
})

Thank you.

CodePudding user response:

chrome.browserAction.onClicked.addListener(function() { 
//Checking if there are any results of the button with that class
        if(document.querySelectorAll('.bt bt-sm text-white btn-flashing').length >= 1){
//Found results of the button

}else{
//Found no results of the button

});

All this code says is if there are more than 0 occurances of ".bt bt-sm text white btn-flashing" then it found results.

CodePudding user response:

You can use a content script to check for the presence of the button.

For example:

chrome.browserAction.onClicked.addListener(function(){
    chrome.tabs.executeScript({
        code: 'document.querySelector("button.bt.bt-sm.text-white.btn-flashing")'
    }, function(result) {
        if (result[0]) {
            alert('found it');
        } else {
            alert('not found');
        }
    });
});
  • Related