Get request failing to get track using Soundcloud JavaScript sdk (Chrome extention)

250 views Asked by At

I am developing a Chrome extension using soundcloud JavaScript sdk. All it does is when you click a button it plays a random track from soundcloud.

My get request works now but when I try to stream the. This is my code.

SC.initialize({
client_id: "2d2d0fb839111694b3877c008ea03a85",
});

$(document).ready(function(){
    $("#loadTracks").on("click", function(){

        SC.get('https://api.soundcloud.com/users/user486627426/tracks', {limit: 1}, function(tracks){
            var track = tracks[0];
            console.log(track.uri);
        });

        SC.stream('https://api.soundcloud.com/users/user486627426/tracks', function(sound){
            sound.play();
        });
    });
});

I get a:

GET chrome-extension://connect.soundcloud.com/audiomanager/audiomanager.js net::ERR_FAILED

And here is my manifest.json

 {
  "manifest_version": 2,

  "name": "Soundcloud",
  "description": "This extension plays a random track using soundcloud",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "activeTab",
    "https://connect.soundcloud.com/sdk-2.0.0.js",
    "*://api.soundcloud.com/*"
  ],

  "content_security_policy": "script-src 'self' https://connect.soundcloud.com/; object-src 'self' https://connect.soundcloud.com/"
}

How can I change the request to use https:// instead of Chrome-extension?

2

There are 2 answers

1
Brian On

This probably isn't a legal url: chrome-extension://api.soundcloud.com/tracks

My guess is you want https://api.soundcloud.com/tracks.

0
xsnvt On

Had a similar problem when making a request to the SoundCloud API, I wound up working around it by using SC.get() to resolve the url of a track and then using and XMLHttpRequest to play the track.

It looked something like this:

SC.get("https://api.soundloud.com/resolve/?url=" + urls[counter], {limit: 1}, function(result){

      var xhr = new XMLHttpRequest();

      client_id = '?client_id=d4ab52d80ed2e7790c3a243495b30093';
      xhr.open('GET', result.uri + client_id);
      xhr.onload = function(){
        var track = JSON.parse(xhr.responseText);
        stream.src = track.stream_url + client_id;
        stream.play();
      }; 
      xhr.send()
  });

Here's a working fiddle of what I'm referring to.

I've commented it in, but for the Chrome Extension the trick is to instead of using /resolve, insert the https://api.soundcloud.com into the SC.get() method. Hope I could help if this is still even an issue.