Created
August 18, 2018 17:18
-
-
Save demaisj/9f8a4bbbe6828fb3c62643b67d75dfea to your computer and use it in GitHub Desktop.
Soundcloud stream filtering done right
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Soundcloud filter | |
// @namespace https://gist.github.com/demaisj/9f8a4bbbe6828fb3c62643b67d75dfea | |
// @version 1.0.0 | |
// @description Soundcloud stream filtering done right | |
// @author demaisj | |
// @match https://soundcloud.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
window.XMLHttpRequest_patches = window.XMLHttpRequest_patches || {}; | |
window.XMLHttpRequest_patches['https://api-v2.soundcloud.com/stream'] = function (xhr) { | |
var data = xhr.responseJSON; | |
for (var i = data.collection.length - 1; i >= 0; i--) { | |
if (data.collection[i].type.endsWith('repost')) { | |
data.collection.splice(i, 1); | |
} | |
} | |
xhr.responseJSON = data; | |
}; | |
if (!window.XMLHttpRequest.patched) { | |
const old_XMLHttpRequest = window.XMLHttpRequest; | |
window.XMLHttpRequest = class extends old_XMLHttpRequest { | |
set onload (fcn) { | |
super.onload = function (event) { | |
var url = this.responseURL.substr(0, this.responseURL.indexOf('?')), | |
count = 0; | |
for (var key in window.XMLHttpRequest_patches) { | |
if (window.XMLHttpRequest_patches.hasOwnProperty(key) && url == key) { | |
window.XMLHttpRequest_patches[key](this); | |
count += 1; | |
} | |
} | |
if (count > 0) { | |
console.log(`Applied ${count} ${count > 1 ? "patches" : "patch"} to ${url}`); | |
} | |
fcn.apply(this, event); | |
}; | |
} | |
get responseText () { | |
if (this.patchedResponseText) { | |
return this.patchedResponseText; | |
} | |
return super.responseText; | |
} | |
set responseText (text) { | |
this.patchedResponseText = text; | |
} | |
get responseJSON () { | |
return JSON.parse(this.responseText); | |
} | |
set responseJSON (data) { | |
this.responseText = JSON.stringify(data); | |
} | |
}; | |
window.XMLHttpRequest.patched = true; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment