从Greasemonkey中的json / ajax的URL获取SoundCloud API轨道ID

时间:2014-10-09 17:54:04

标签: ajax json greasemonkey soundcloud

resolverURLI尝试在Greasemonkey脚本中使用SoundCloud API,但问题是当您浏览时,DOM中没有当前的轨道ID。

唯一可靠的数据是当前网址。他们提供api.soundcloud.com/resolve.json?url=来解析网址中的曲目ID,但Greasemonkey似乎没有遵循它。

使用以下脚本并进入随机页面。例如https://soundcloud.com/soulection/soulection-radio-show-187-w-sango

激活url: resolverURL,不会返回任何内容。

// ==UserScript==
// @name         SoundCloud track test
// @version      0
// @include      http*soundcloud.com*
// @run-at       document-end
// @grant        none
// ==/UserScript==

// waitUntilExists
(function(e,t){var n={};var r=function(e){if(n[e]){t.clearInterval(n[e]);n[e]=null}};var i="waitUntilExists.found";e.fn.waitUntilExists=function(s,o,u){var a=this.selector;var f=e(a);var l=f.not(function(){return e(this).data(i)});if(s==="remove"){r(a)}else{l.each(s).data(i,true);if(o&&f.length){r(a)}else if(!u){n[a]=t.setInterval(function(){f.waitUntilExists(s,o,true)},500)}}return f}})(jQuery,window)

$(".l-listen-sound").waitUntilExists(function() {

    // some old track id
    var trackIDurl = "https://api.soundcloud.com/tracks/13158665.json?client_id=386b66fb8e6e68704b52ab59edcdccc6";
    console.log( "trackIDurl: " + trackIDurl );

    // build the api url from the current browsing url
    var urlE = encodeURIComponent( $(location).attr('href').replace(/\?.+/g, "") ),
        resolverURL = 'http://api.soundcloud.com/resolve.json?url=' + urlE + '&client_id=386b66fb8e6e68704b52ab59edcdccc6';
    console.log( "resolverURL: " + resolverURL );

    $.ajax({
        url: trackIDurl,
        //url: resolverURL,
        dataType: 'json',
        success: function(result) {
           alert(result.title);
        }
    });

});

我尝试了几种解决方案,包括GM_xmlhttpRequest和unsafeWindow。根本没有运气:(

那么我怎样才能手动构建trackIDurl?

0 个答案:

没有答案