Search code examples
google-chromegoogle-chrome-extensionfirefox-addonfirefox-addon-webextensionsbrowser-extension

How can I modify an extensions badge in jQuery?


Can I use jQuery to add Removed += 1 like it has been used in the top function? Can I make it only add 1 if the attribute was removed?

window.onload = function() {

    let Removed = 0
    const anchorElements = document.getElementsByTagName('A')

    for (element of anchorElements) {
        if (!element.getAttribute('ping')) continue
            console.log("Removed ping: " + element.getAttribute('ping'))
                element.removeAttribute('ping')
                Removed += 1
                chrome.extension.sendMessage(Removed)
    }
}

link();

function link(){

    jQuery("a[onclick*='ga']").removeAttr('onclick');
    jQuery("a[onclick*='_gaq.push']").removeAttr('onclick');
    jQuery("link[rel*='pingback']").removeAttr('rel');

}

Solution

  • Please note that extensions use dedicated API browserAction.setBadgeText() to interact with the extensions badge from privileged scripts (not content scripts).

    JQuery is used in your content and does not have access to above API.

    You can use runtime.sendMessage() to pass the request to the background script.