· 4 years ago · May 18, 2021, 06:46 PM
1 function generateRequest(data) {
2 // Data would be a list of all the gallery URLs, or the parent elements, present on a page with no duplicates
3 var reqList = []; // We use an array for our gidlist, since the API can handle up to 25 galleries per request
4 for (var i = 0; i < data.length; i++) {
5 if (data[i] == undefined) continue;
6 // If these were elements instead of links, you'd have to do this differently
7 var str = data[i].split('/'); // Split the key to match request specifications of galleryID, galleryToken
8 reqList[i] = [str[4], str[5]];
9 }
10 var request = {"method": "gdata", "gidlist": reqList, "namespace": 1};
11
12 var req = new XMLHttpRequest();
13 req.onreadystatechange = e => {
14 if (req.readyState == 4) {
15 if (req.status == 200) {
16 var apirsp = JSON.parse(req.responseText);
17 //console.log(apirsp);
18 for (var i = 0; i < apirsp.gmetadata.length; i++) {
19 // This is where you would put functions to do with the tags, once you sorted them out
20 if (apirsp.gmetadata[i].tags.some(tag => /^parody:/.test(tag))) {
21 // Do something to hide the element, easier when you have a list of elements rather than a list of links
22 // Though the variable i will correspond to the index of the link or element in the data array
23 }
24 }
25 } else {
26 console.error();
27 }
28 }
29 }
30 req.open("POST", document.location.origin + "/api.php", true); // Due to CORS, we need to use the API on the same domain as the script
31 req.send(JSON.stringify(request));
32 }