Before we dive in, a quick disclaimer: My knowledge of JS is limited, and I typically learn on the go when creating scripts. However, my usual method didn't work this time.
Currently, I'm working on a small JS script that will function as a bookmarklet. The goal is to utilize the Tinder API to display profile pictures of users who have liked them - a feature usually available with Gold membership.
Here's what the current script looks like:
var stringz;
var xhr = new XMLHttpRequest();
var tokez = localStorage.getItem("TinderWeb/APIToken");
var url = "https://api.gotinder.com/v2/fast-match/teasers?locale=en";
xhr.withCredentials = true;
xhr.open("GET", url);
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("content-type", "application/json; charset=utf-8");
xhr.setRequestHeader("x-auth-token", tokez);
xhr.setRequestHeader("tinder-version", "2.35.0");
xhr.setRequestHeader("platform", "web");
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
stringz = xhr.responseText;
return stringz;
}
};
//Converting the xhr response into a JSON string
var jasonstring = JSON.parse(stringz);
//Extracting the URLs
var jasonstrung = jasonstring.data.results.map(x => x.user.photos.map(y => y.url));
//Formatting the URLs into a nicely structured JSON string
var jason = JSON.stringify(jasonstrung, null, 4);
//Checking the output
console.log(jason);
I perform both JSON.parse and JSON.stringify because the data returned by xhr appears as a text string formatted like JSON, but it isn't actually JSON. Therefore, I need to parse it to extract the desired elements and then format them for better readability.
When running this script in the Chrome Dev Console for the first time, it throws an error:
VM5418:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at <anonymous>:18:24
The reason for this error could be that 'stringz' is still undefined when JSON.parse attempts to process it.
However, after the script finishes executing, typing console.log(stringz)
reveals the expected string. If the script is run twice, the final dataset is printed out as desired:
[
[
"https://preview.gotinder.com/5ea6601a4a11120100e84f58/original_65b52a4a-e2b2-4fdb-a9e6-cb16cf4f91c6.jpeg"
],
// More URLs here...
]
Is there a way to make this script work seamlessly in one go (like a bookmarklet)? Unfortunately, using setTimeout doesn't seem to resolve the issue, possibly due to the delay in populating 'stringz' before applying JSON.parse.
Thank you!