0
votes

I need to get some data from an RSS feed for at Chrome Extension. I'm attempting to use jQuery to do so.

Inspecting popup shows:

  • Uncaught ReferenceError: $ is not defined - popup.js:5
  • Refused to load the script 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.0.min.js' because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:". - popup.html:1

This is the code:

popup.js

(function () {

alert("Function running"); //Fired

$(document).ready(function (e) {
    $.get("https://fupifarvandet.dk/episodes.rss", function (data) {
        $(data).find("item").each(function () { 
            var el = $(this);

            console.log("------------------------");
            console.log("title      : " + el.find("title").text());

            alert(el.find("title").text());

            alert("Inside the function"); //Never fired
        });
    });
});  

  alert("Function has run"); //Never fired
})();

manifest.json

{
  "manifest_version": 2,

  "name": "RSS Read test",
  "description": "Extension description",
  "version": "1.0",

  "browser_action": {
    "default_icon": "img/icon.png",
    "default_popup": "popup.html"
  }
}

popup.html

<!doctype html>
<html>
  <head>
    <title>Getting Started Extension's Popup</title>
    <style>
    </style>
    <script src="popup.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.0.min.js"></script>
  </head>
  <body>
  </body>
</html>
2

2 Answers

1
votes

There are 2 ways to do this. The easiest is to just include the jQuery file with the extension files. The other would be to edit the CSP to allow scripts to be loaded from that url. I would recommend the first way as it is one less network related thing that you have to worry about.

0
votes

As BeardFist pointed out, including the jQuery file like this:

<script src="jquery.js"></script>

Making sure jquery.js is located inside the same folder as manifest.json

instead of:

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.0.min.js"></script>

And making sure to run this line of code before any other <script> commands. This fixed the problem and i'm now able to retrieve the information i need.