Created
May 11, 2017 14:49
-
-
Save acoyfellow/890bf649aebcb3ca55014101e73937c2 to your computer and use it in GitHub Desktop.
XHR INSTA example
This file contains hidden or 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
function getMedia(instaData, mediaCount, callback) { | |
var profileData = instaData.entry_data.FeedPage[0].graphql.user; | |
var userID = profileData.user.id; | |
var totalMedia = profileData.user.media.count; | |
var mediaCount = mediaCount > totalMedia ? totalMedia : mediaCount; | |
var csrf_token = instaData.config.csrf_token; | |
var xhrBody = "ig_user(" + userID + ") { media.after(0, " + mediaCount + ") {nodes {display_src }}}"; | |
var xhr = new XMLHttpRequest(); | |
xhr.open("POST", '/query/', true) | |
xhr.setRequestHeader("X-CSRFToken", csrf_token); | |
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); | |
xhr.onreadystatechange = function() { | |
if (this.readyState != 4) return; | |
callback(this.responseText); | |
} | |
xhr.send("q=" + encodeURIComponent(xhrBody)); | |
} | |
// I want 20 instagram posts from this user | |
getMedia(window._sharedData, 20, function(data){ | |
console.log(JSON.parse(data)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment