Error loading external JSON resource in Chrome extension

I have been exploring various resources like Stack Overflow and Google in search of an answer, but unfortunately, I haven't found anything satisfactory. Since I am still new to JavaScript, I'm feeling a bit overwhelmed.

manifest.json

{
  "manifest_version": 2,

  "name": "Twitch Streams List",
  "description": "Discover all 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>Acquiring top Twitch streams</title>
    <style>
      body {
        min-width: 350px;
      }
    </style>
    <script src="twitch.js" type="text/javascript"></script>
  </head>
  <body>
  </body>
</html>

twitch.js

var streamGenerator = {
  /* URL from which the streams are fetched */
  url: chrome.extension.getURL('http://api.twitch.tv/kraken/streams?limit=20'),

  /* Retrieve the streams from the specified JSON URL */
  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();
  },

  /* Display the imported streams on 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);
  }
};

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

No content appears in the popup window, and the console displays:

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

What steps should I take to resolve this issue?

Thank you

Answer №1

'The file 'api.twitch.tv/kraken/streams?limit=20' is not located within your installation directory, therefore do not attempt to pass it as an argument to the function chrome.extension.getURL.

Furthermore, please note that XHR requests cannot retrieve content from different domains. It may be worth exploring if there is an alternative method available such as JSONP.

Answer №2

Of importance to mention:

gives a 404 error

provides JSON data

(Twitch API requires SSL for secure connection)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best approach for managing client-side routes when a page refresh displays the backend Rails route instead?

After deploying my SPA to Render, I encountered an issue. My application comprises a React frontend with Redux and a Rails backend. When the app initializes, it correctly displays the frontend route in the browser. Subsequent navigation without refreshing ...

How to send error message from ASP .NET MVC to AJAX request

Below is an example of my ajax call: $.ajax({ url: './Orders/SaveOrder', type: "POST", data: JSON.stringify(data), dataType: "JSON", contentType: "application/json", success: function (d) { if (d.status == tr ...

Attempting to execute this function to verify the presence of matching email addresses within the database

function checkEmail(){ var email = $("#email").val(); var xmlhttp = new XMLHttpRequest(); var url = serverURL() + "/checkIfEmailExists.php"; url += "?email=" + email; xmlhttp.onreadystatechange=func ...

Retrieve the username a person used to sign in to the computer

Can you retrieve the username of the logged-in user in PHP or JavaScript? For instance, if I log in to my laptop and then visit a website, I want it to display the username I used to log in. Is there a method to achieve this, or is it not possible? ...

Creating a versatile function for rendering content

I am working on a unique calendar feature that involves using checkboxes for filtering purposes. So far, I have managed to get all the filters functioning correctly by triggering my render event in this manner: //Initiate render event when checkbox is cli ...

Sending an object and array using an Angularjs service: An easy step-by-step guide

I encountered an issue while attempting to send both an object and an array to an API simultaneously. Here is the object data from input boxes: var vacation = { Vac_Main_Key: $scope.vackey, Vac_Main_Code: $scope.code, Gender_Type: $scope.gen ...

ng-repeat not displaying any content

I am trying to create a form where users can input extra information by adding new rows, but I am struggling with generating the first row <div ng-repeat="row in rows"> <input type="text" placeholder="name"><input type="tel" placeholder="te ...

What is the best way to locate posts from authors who are already part of the friends array?

I'm currently working on a task where I need to retrieve all posts from the friends list of a user and display them in descending order based on creation date. Below is the controller function responsible for fetching all posts from friends: async fu ...

Ways to activate an analysis when a component is re-rendered?

Is there a way to trigger an analytic when the search bar is used to search in the LibraryManager component and it gets re-rendered with the search value? The render function seems like the best option for adding the analytics trigger, but are there any ...

The error message states: "Cannot create element as the React object is not

Recently delving into the world of React and Material UI, I've been working on creating an AppBar with nested Tabs. Here's a snippet of my current approach: import {React, PropTypes, Component} from 'react'; import TodoTextInput from & ...

Error encountered in React when utilizing UseState for sorting: Unable to access properties of an undefined value

As I dive into my journey of learning React, I encounter a puzzling issue while attempting to sort an array of objects within a React component. The frustrating error message that keeps popping up is "Cannot read properties of undefined (reading 'pric ...

Excess space noted at the bottom of tablet and mobile versions of the page

I'm struggling to troubleshoot an issue with the mobile and tablet versions of a web-shop I'm designing for university. Some pages have excessive space after the HTML tag, but only on certain pages. I've tried commenting out CSS lines relate ...

Attempting to showcase a collection of values, specifically focusing on highlighting the even numbers within the array

const sampleArray = [ 469, " " + 755, " " + 244, " " + 245, " " + 758, " " + 450, " " + 302, " " + 20, " " + 712, " " + 71, " " + 456, ...

Fading a hidden field in and out using JQuery when a selection is changed

Looking for a solution to toggle the visibility of a field based on selected content using jQuery? Check out this code snippet: $('#State').on('change', function() { var s = $('#State option:selected').text; if(s !== "") { ...

Is it possible to have a div set to close upon loading the page?

Is there a way to have the toggle function set up so that when the page loads, the div being toggled starts out closed? I don't want them to be visible until clicked on. Best regards, Joey <script type="text/javascript> $(document ...

What are the advantages of using Context API compared to Redux in terms of performance?

After doing some research on the context API, it seems to offer a sophisticated solution for handling uni-directional data communication challenges in react. On the other hand, there is Redux which excels in sharing a global state efficiently. Will I exp ...

Issue when trying to use both the name and value attributes in an input field simultaneously

When the attribute "name" is omitted, the value specified in "value" displays correctly. However, when I include the required "name" attribute to work with [(ngModel)], the "value" attribute stops functioning. Without using the "name" attribute, an error ...

Get an array from the value in an object within an array of objects using Mongoose without using JavaScript

Is there a way to transform the array of objects shown below: [{ category:"AAA" },{ category:"BBB" },{ category: "CCC" }] Into a new format like this: ["AAA","BBB","CCC"] , without relying on filtering or traditional array functions in the backend, bu ...

There appears to be some lingering content in the JQuery Mobile slide-out dialog box

My jQuery mobile slide out dialog box is experiencing an issue. The first time the slide out occurs, a comment box appears where users can enter comments and then submit them, followed by a "THANK YOU" message. However, when the user closes the dialog box ...

What is the most effective way to reduce the number of http requests caused by onChange events in Material UI Slider?

Is there a way to optimize my request handling? In my React project, I am currently utilizing Material UI's slider. With the onChange property, I am making HTTP POST requests whenever the slider is moved. Here is a snippet of the code: <Slider ...