Chrome extension: Failed to load resource for external json

I've searched stackoverflow and google for the answer, but I can't find anything ... I'm still new to javascript, so I'm a little confused;

manifest.json

{
  "manifest_version": 2,

  "name": "Twitch Streams List",
  "description": "Find all the streams directly through Chrome",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
   "permissions": [
    "http://api.twitch.tv/kraken/streams?limit=20"
  ],
  "web_accessible_resources": ["twitch.js"]
}

      

popup.html

<!doctype html>
<html>
  <head>
    <title>Getting top streams from Twitch</title>
    <style>
      body {
        min-width: 350px;
      }
    </style>
    <script src="twitch.js" type="text/javascript"></script>
  </head>
  <body>
  </body>
</html>

      

twitch.js

var streamGenerator = {
  /* url where to get the streams from */
  url: chrome.extension.getURL('http://api.twitch.tv/kraken/streams?limit=20'),

  /* go get the streams from the json at the url given */
  requestStreams: function() {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', this.url, true);
    xhr.onreadystatechange = function() {
      if (xhr.readyState == 4) {
        var resp = JSON.parse(xhr.responseText);
      }
    }
    xhr.send();
  },

  /* import the streams in the html page */
  showStreams: function (e) {
    var streams = resp.streams;
    var output = '<ul>';

    for (var i = 0; i < streams.length; i++) {
      output += '<li>' + streams[i].channel.display_name + ' - ' + streams[i].channel.viewers + ' - ' + streams[i].channel.game + '</li>';
    }

    output += '</ul>';
    console.log(output);
    document.body.appendChild(output);
  }
};

/* Run the script as soon as the popup is loaded */
document.addEventListener('DOMContentLoaded', function () {
  streamGenerator.requestStreams();
});

      

Nothing shows up in the popup and the console says

Failed to load resource chrome-extension://dihpppnflhlpkehcgnjggjcipffmjlgp/http://api.twitch.tv/kraken/streams?limit=20

      

What to do???

thank

+3


source to share


2 answers


'api.twitch.tv/kraken/streams?limit=20' is not in your install directory, so don't pass it as an argument chrome.extension.getURL

.



However, XHR cannot fetch the content domain. You might want to know if they offer an alternative like JSONP.

+2


source


It is worth noting:

http://api.twitch.tv/kraken/streams?limit=20 returns 404



https://api.twitch.tv/kraken/streams?limit=20 returns JSON

(SSL required for twitch api)

+1


source







All Articles