-1

It says "$ is not defined", however, the code works perfectly.

I have been trying to develop an extension, I ran into some issues when linking Jquery's CDN in my file, so I just copied the compressed code of Jquery and created a new file and linked the jquery with my extension that way. In manifest, the popup.js (which gives me an error) is defined as:

"background": {
        "scripts": ["popup.js"]
    },

Here is html:

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta hhtp-equiv="X-UA-Compatible" content="ie=edge">
    <title>Ad ბლოკერი</title>
    <link rel="stylesheet" type="text/css" href="popup.css">
    <script src="jqueryCode.js" type="text/javascript"></script>
    <script src="Blacklist.js"></script>
    <script src="popup.js"></script>
</head>

Here is Jquery:

    /*$("#checkbox").prop('checked', true);
    $("#onOroff").text("ON");
    document.getElementById("MyElement").classList.remove('.pauseDiv');*/

    $("#checkbox").prop('checked', true);
    $("#onOroff").text("ON");

    $(".slider").click(function() {
        if( $("#checkbox").is(":checked") ){
            $("#onOroff").text("OFF");
            $(".slider").css({"background-color": "#C75052"});

        } else {
            $("#onOroff").text("ON");
            $(".slider").css({"background-color": "#50c878"});
        }
    }); // Switches it from On and Off
});

For some odd reason, I get an error when I upload the unpacked files to chrome as an extension, yet, it works fine. full error:

Uncaught ReferenceError: $ is not defined Context _generated_background_page.html Stack Trace popup.js:1 (anonymous function)

CODER11713
  • 79
  • 6

1 Answers1

0

Okay, I guess it was an easy fix :D

For others: I had the script as a background script, which starts when Chrome is loaded. Content script is what is loaded when the page loads. So it seems that the script was running before I opened new tabs when uploaded to chrome://extensions and so that's why it gave me the error.

Switched from "background": "scripts" to "content_scripts":

CODER11713
  • 79
  • 6