Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I want to edit a chrome extension browser action popup using jquery, where do I need to include jquery in the program to use it?

I am trying to make an extension that gets an XMLrequest and takes the information and writes it to the browser action popup's html page. Is this possible?

Here's my manifest file

//manifest.json
"name":"myExtension",
"version":"1.0",
"manifest_version": 2,
"browser_action": {
    "default_icon":"favicon.ico",
    "default_popup": "popup.html"
}

My browser action popup html file:

<!--popup.html-->
<!DOCTYPE html>
<html>
<head>
    <title>My Title</title>
<script src="popup.js"></script>
<script src="jquery.min.js"></script>
</head>
<body>
    <p>Hello</p>
    <p class="add"></p>

</body>
</html>

And my script:

//popup.js

//Code up to this point gets xml info, I didn't include

var info = //string from xml request
$.(".add").html(info);

I see the Hello paragraph in the popup window, but never the info which should be in the add paragraph, and when I check the console, it tells me that the $ is undefined. How can I get jquery to work for me? I'm a relative newcomer to this so I'm sorry if this is really simple and I don't understand it.

like image 352
Ryan Gibbons Avatar asked Nov 25 '22 14:11

Ryan Gibbons


1 Answers

There are a few points that are wrong

  • The order of adding your scripts; it should be:

    <script src="jquery.js"></script>
    <script src="popup.js"></script>
    
  • Your selector is wrong; it shouldn't be $.(".add") but $(".add"):

    $(".add").html(info);
    
  • You need to wrap your jQuery commands into DOM ready/on load:

    $(function() {
      var info = //string from xml request
      $(".add").html(info);
    });
    

Also, you don't need to add the script to content_scripts if you don't going to use it there.
You have full access to your extensions resources from the background page and the popup.

like image 76
dan-lee Avatar answered Jan 02 '23 18:01

dan-lee