PDA

View Full Version : Merry Go Round Script doubts



DarkSkies
02-10-2024, 08:46 PM
Since I was tired that my sad pets won't get happy with 1 or 2 rides, I decided to make a script to attack the problem.

This is my first script in JS so please bare with me lol

Anyway, I've already gotten to make it refresh back and forth according to the Start/Stop buttons, at least in Chrome with Tampermonkey (haven't tested on others yet).

Buuut my ultimate goal is to auto stop when pet is finally happy. Here is where I'm stuck now.

Questions:

1. I tried a fetch to get the mood of the chosen pet which on console get it right, but outside of my function I only get undefined (apparently I'm having problems with returning + async + fetch taking time maybe + I'm a noob trying sorry haha). So any hints about how to get my mood variable outside of my function?

2. Since I couldn't use LocalStorage to save the input.Click() because of object type I had to copy paste twice the block for filling the combobox of pet names (block in sendPet function). Any advice on how to improve this? I hated copypasting.

3. After solving point 1 I'll move onto mood evaluation, so that's missing for now.


[Only registered and activated users can see links]


Error:
[Only registered and activated users can see links]




// ==UserScript==
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
// ==/UserScript==

(function() {
'use strict';
/* global $ */

const quickref="[Only registered and activated users can see links]";

let timeout1, timeout2;
let flag, counter, moodPet;
let auxFlag, auxCounter, auxPet, auxInps, auxInput;
let petNameInputs = []; // checkbox inputs
let petNamesList = []; // petnames
const roundForm = $("form[action='merrygoround2.phtml' i]");

// Upperbar
var uppertext=$("[class='content']")[0];

const merryRefresher = document.createElement("div");
merryRefresher.style.cssText = "text-align: center; padding: 10px;";
merryRefresher.setAttribute("id", "merryRefresher");
merryRefresher.innerHTML = "<br><br><b>-* Merry Go Round Refresher *-</b><br><br>";
uppertext.prepend(merryRefresher);

// Pet Status
const merryStatus = document.createElement("p");
merryStatus.style.cssText = "text-align: center; display: none";
merryStatus.setAttribute("id", "merryStatus");
merryRefresher.appendChild(merryStatus);

// Combobox
const petChoice = document.createElement("select");
petChoice.style.cssText = "text-align: center";
petChoice.setAttribute("id", "petChoice");
petChoice.innerHTML = "<option disabled selected hidden>Choose your pet</option>";
merryRefresher.appendChild(petChoice);

const lineBreak = document.createElement("br");
merryRefresher.appendChild(lineBreak);

const startButton = document.createElement("button");
startButton.style.cssText = "text-align: center; margin-top: 20px";
startButton.setAttribute("id", "startButton");
startButton.innerHTML = "Start";
merryRefresher.appendChild(startButton);

const stopButton = document.createElement("button");
stopButton.style.cssText = "text-align: center; margin-top: 20px; display: none";
stopButton.setAttribute("id", "stopButton");
stopButton.innerHTML = "Stop";
merryRefresher.appendChild(stopButton);

if(document.URL.indexOf("worlds/roo/merrygoround.phtml") != -1) {
auxFlag = parseInt(localStorage.getItem("isFlag"));

if (auxFlag == 1){
hidestart();

counter = parseInt(localStorage.getItem("Rounds"));
counter++;
localStorage.setItem("Rounds", counter);
auxPet = localStorage.getItem("thePet");
sendPet(auxPet);
}
else{
// Get list of inputs values (names) whose parent is a list item
const inputList = document.querySelectorAll("li > input");
for (const input of inputList) {
// petname
const name = input.value;
// build names list
if (!petNamesList.includes(name)) {
const option = document.createElement("option");
option.value = name;
option.text = name;
petChoice.add(option);
petNameInputs.push(input);
petNamesList.push(name);
}
}

// add startButton onClick
startButton.addEventListener("click", () => {
const selectedPet = petChoice.selectedIndex;
const chosenPetname = petChoice.options[selectedPet].value;
localStorage.setItem("thePet", chosenPetname);
if (selectedPet != 0) {
//while(flag == 1){
flag = 1;
localStorage.setItem("isFlag", flag);
counter = 1;
localStorage.setItem("Rounds", counter);
sendPet(chosenPetname);
//}
}
});

}

// add stopButton onClick
stopButton.addEventListener("click", () => { stopall() });
}

if(document.URL.indexOf("worlds/roo/merrygoround2.phtml") != -1) {
auxFlag = parseInt(localStorage.getItem("isFlag"));

if(auxFlag == 0){
hideall();
}
else{
hidestart();

// Get pet's mood
auxPet = localStorage.getItem("thePet");
moodPet = getMood(auxPet);
counter = parseInt(localStorage.getItem("Rounds"));
merryStatus.innerHTML = "Mood: " + moodPet + " | Rounds: " + counter.toString();
merryStatus.style.display = "inline-block";

// add stopButton onClick
stopButton.addEventListener("click", () => { stopall() });

timeout2 = setTimeout(function () { goback();},6000);
}
}

function sendPet(petname){
// Get list of inputs values (names) whose parent is a list item
const inputList = document.querySelectorAll("li > input");
for (const input of inputList) {
// petname
const name = input.value;
// build names list
if (!petNamesList.includes(name)) {
const option = document.createElement("option");
option.value = name;
option.text = name;
petChoice.add(option);
petNameInputs.push(input);
petNamesList.push(name);
}
}

// Select radio button
for (const input of petNameInputs) {
if (input.value == petname) {
input.click();
}
}

timeout1 = setTimeout(function () { roundForm[0].submit();},5000);
}

function getMood(petname){
fetch("[Only registered and activated users can see links]").then(function(response) {
// When the page is loaded convert it to text
response.text()
}).then(function(html) {
// Initialize the DOM parser
var parser = new DOMParser();
// Parse the text
var doc = parser.parseFromString(html, "text/html");
var doc2 = doc.getElementById(petname + "_details");
var doc3 = doc2.querySelector(".pet_info");
let moodStatus = doc3.getElementsByTagName("td")[6];
console.log(moodStatus);
return moodStatus;
}).catch(function(err) {
console.log('Failed to fetch page: ', err);
moodPet = "Error fetching pet info";
});
}

function goback() {
window.location.href = '[Only registered and activated users can see links]';
}

function stopall(){
clearTimeout(timeout1);
clearTimeout(timeout2);
flag = 0;
counter = 0;
localStorage.setItem("isFlag", flag);
localStorage.setItem("Rounds", counter);

if(document.URL.indexOf("worlds/roo/merrygoround.phtml") != -1) {
hidestop();
}
if(document.URL.indexOf("worlds/roo/merrygoround2.phtml") != -1) {
hideall();
}
}

function hidestart(){
startButton.style.display = "none";
petChoice.style.display = "none";
merryStatus.style.display = "none";
stopButton.style.display = "inline-block";
}

function hidestop(){
startButton.style.display = "inline-block";
petChoice.style.display = "inline-block";
stopButton.style.display = "none";
}

function hideall(){
merryRefresher.style.display = "none";
}

})();



Thanks for reading. I'm burn out now but I'll continue tomorrow with whatever you can suggest me :tiger:

Nyu
02-10-2024, 10:01 PM
1:

To get the mood you can use this inside the getMood function.




const response = await fetch(`[Only registered and activated users can see links]`);
const text = await response.text();
const div = document.createElement('div');
div.innerHTML = text;
return div.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`).innerText



Looks like you're returning the promise, not the value, you can try on your browser console to see what this one returns vs what the current one does


2:

One thing you can do is save as string and then convert to your desired type, i do this on my Battledome fighter script actually, the weapons and abilities are saved as a json that i save as a string and parse as json again.
Now, if all you can do is refactor, just put it in another function and call it:



if(document.URL.indexOf("worlds/roo/merrygoround.phtml") != -1) {
auxFlag = parseInt(localStorage.getItem("isFlag"));


if (auxFlag == 1){
hidestart();


counter = parseInt(localStorage.getItem("Rounds"));
counter++;
localStorage.setItem("Rounds", counter);
auxPet = localStorage.getItem("thePet");
sendPet(auxPet);
}
else{
// Get list of inputs values (names) whose parent is a list item
createOptions(petname)// We call the new function


// add startButton onClick
startButton.addEventListener("click", () => {
const selectedPet = petChoice.selectedIndex;
const chosenPetname = petChoice.options[selectedPet].value;
localStorage.setItem("thePet", chosenPetname);
if (selectedPet != 0) {
//while(flag == 1){
flag = 1;
localStorage.setItem("isFlag", flag);
counter = 1;
localStorage.setItem("Rounds", counter);
sendPet(chosenPetname);
//}
}
});


}


// add stopButton onClick
stopButton.addEventListener("click", () => { stopall() });
}


(...)



function sendPet(petname){
// Get list of inputs values (names) whose parent is a list item
createOptions(petname) // We call the new function


// Select radio button
for (const input of petNameInputs) {
if (input.value == petname) {
input.click();
}
}


timeout1 = setTimeout(function () { roundForm[0].submit();},5000);
}

//This is the new function
function createOptions(petname) {
const inputList = document.querySelectorAll("li > input");
for (const input of inputList) {
// petname
const name = input.value;
// build names list
if (!petNamesList.includes(name)) {
const option = document.createElement("option");
option.value = name;
option.text = name;
petChoice.add(option);
petNameInputs.push(input);
petNamesList.push(name);
}
}
}


I didnt test this btw, just a general idea :P

DarkSkies
02-10-2024, 10:10 PM
1:

To get the mood you can use this inside the getMood function.




const response = await fetch(`[Only registered and activated users can see links]`);
const text = await response.text();
const div = document.createElement('div');
div.innerHTML = text;
return div.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`).innerText



Looks like you're returning the promise, not the value, you can try on your browser console to see what this one returns vs what the current one does


2:

One thing you can do is save as string and then convert to your desired type, i do this on my Battledome fighter script actually, the weapons and abilities are saved as a json that i save as a string and parse as json again.
Now, if all you can do is refactor, just put it in another function and call it:



if(document.URL.indexOf("worlds/roo/merrygoround.phtml") != -1) {
auxFlag = parseInt(localStorage.getItem("isFlag"));


if (auxFlag == 1){
hidestart();


counter = parseInt(localStorage.getItem("Rounds"));
counter++;
localStorage.setItem("Rounds", counter);
auxPet = localStorage.getItem("thePet");
sendPet(auxPet);
}
else{
// Get list of inputs values (names) whose parent is a list item
createOptions(petname)// We call the new function


// add startButton onClick
startButton.addEventListener("click", () => {
const selectedPet = petChoice.selectedIndex;
const chosenPetname = petChoice.options[selectedPet].value;
localStorage.setItem("thePet", chosenPetname);
if (selectedPet != 0) {
//while(flag == 1){
flag = 1;
localStorage.setItem("isFlag", flag);
counter = 1;
localStorage.setItem("Rounds", counter);
sendPet(chosenPetname);
//}
}
});


}


// add stopButton onClick
stopButton.addEventListener("click", () => { stopall() });
}


(...)



function sendPet(petname){
// Get list of inputs values (names) whose parent is a list item
createOptions(petname) // We call the new function


// Select radio button
for (const input of petNameInputs) {
if (input.value == petname) {
input.click();
}
}


timeout1 = setTimeout(function () { roundForm[0].submit();},5000);
}

//This is the new function
function createOptions(petname) {
const inputList = document.querySelectorAll("li > input");
for (const input of inputList) {
// petname
const name = input.value;
// build names list
if (!petNamesList.includes(name)) {
const option = document.createElement("option");
option.value = name;
option.text = name;
petChoice.add(option);
petNameInputs.push(input);
petNamesList.push(name);
}
}
}


I didnt test this btw, just a general idea :P

Thanks a lot for the advices Nyu! I'll give it a test tomorrow and let you know my results :D

DarkSkies
02-16-2024, 12:15 AM
Testing a bit more, I'm getting the following problem on the await lines:


[Only registered and activated users can see links]

Nyu do you know if I need to update or do sth about babel-eslint? I've read a bit about it and found how to edit the custom file on TM though I'm not sure if I should install a package first? :S

Nyu
02-16-2024, 07:28 AM
Testing a bit more, I'm getting the following problem on the await lines:


[Only registered and activated users can see links]

[Only registered and activated users can see links]

Sorry! I forgot about that part.
Just add "async" to the start of the function and that should fix it.
Try this c:



async function getMood(petname){
const response = await fetch(`[Only registered and activated users can see links]`);
const text = await response.text();
const div = document.createElement('div');
div.innerHTML = text;
return div.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`).innerText
}


If you want to use await, the function must be async.

Praise_the_Sun
02-16-2024, 09:34 AM
Hey folks, wondering if you both landed on a final version of this script? My depressed pet is making me ya know... depressed haha

Nyu
02-16-2024, 01:23 PM
Hey folks, wondering if you both landed on a final version of this script? My depressed pet is making me ya know... depressed haha
DarkSkies is still working on it, I guess she'll post it on the scripts section when it's done c:

DarkSkies
02-16-2024, 03:17 PM
Yes I'm on it, I'm just still having problem with the fetch thing not returning any value out of local part :smilet-digitalpoint

I tried this:



// Nyu's part but still getting Promise outside of function

moodPet = getMood(auxPet);


async function getMood(petname){
const response = await fetch("[Only registered and activated users can see links]");
// Get HTML page as text
const text = await response.text();
const div = document.createElement('div');
div.innerHTML = text;

// Get mood info
console.log(div.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`).innerText);
return div.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`).innerText
}


And then this other one:


const parser=new DOMParser;
let t;
.........

moodPet = asyncCall(auxPet);

// Me trying again but getting undefined outside of function
function getMood(petname,t){
fetch("[Only registered and activated users can see links]").then(response=>response.text()).
then(response=>parser.parseFromString(response,"text/html")).
then(response=>{const t=response.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`);
return t?t.textContent:0});
}

async function asyncCall(petname) {
console.log('calling');
const result = await getMood(petname,t);
console.log(result);
console.log(t);
return result;

Nyu
02-16-2024, 04:31 PM
Try again with this



moodPet = await getMood(auxPet);


async function getMood(petname){
const response = await fetch("[Only registered and activated users can see links]");
// Get HTML page as text
const text = await response.text();
const div = document.createElement('div');
div.innerHTML = text;


// Get mood info
console.log(div.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`).innerText);
return div.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`).innerText
}


moodPet should save the mood

DarkSkies
02-16-2024, 09:17 PM
Try again with this



moodPet = await getMood(auxPet);


async function getMood(petname){
const response = await fetch("[Only registered and activated users can see links]");
// Get HTML page as text
const text = await response.text();
const div = document.createElement('div');
div.innerHTML = text;


// Get mood info
console.log(div.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`).innerText);
return div.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`).innerText
}


moodPet should save the mood

I'm sorry to keep bothering, the eslint error came back again. Could it be possible that my TM is outdated or sth? :S

[Only registered and activated users can see links]




// ==UserScript==
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
// ==/UserScript==

(function() {
'use strict';
/* global $ */
const parser=new DOMParser;
const quickref="[Only registered and activated users can see links]";
let t;

let timeout1, timeout2;
let flag, counter, moodPet;
let auxFlag, auxCounter, auxPet, auxInps, auxInput;
let petInputs = []; // global checkbox inputs
const roundForm = $("form[action='merrygoround2.phtml' i]");

// Upperbar
var uppertext=$("[class='content']")[0];

const merryRefresher = document.createElement("div");
merryRefresher.style.cssText = "text-align: center; padding: 10px;";
merryRefresher.setAttribute("id", "merryRefresher");
merryRefresher.innerHTML = "<br><br><b>-* Merry Go Round Refresher *-</b><br><br>";
uppertext.prepend(merryRefresher);

// Pet Status
const merryStatus = document.createElement("p");
merryStatus.style.cssText = "text-align: center; display: none";
merryStatus.setAttribute("id", "merryStatus");
merryRefresher.appendChild(merryStatus);

// Combobox
const petChoice = document.createElement("select");
petChoice.style.cssText = "text-align: center";
petChoice.setAttribute("id", "petChoice");
petChoice.innerHTML = "<option disabled selected hidden>Choose your pet</option>";
merryRefresher.appendChild(petChoice);

const lineBreak = document.createElement("br");
merryRefresher.appendChild(lineBreak);

const startButton = document.createElement("button");
startButton.style.cssText = "text-align: center; margin-top: 20px";
startButton.setAttribute("id", "startButton");
startButton.innerHTML = "Start";
merryRefresher.appendChild(startButton);

const stopButton = document.createElement("button");
stopButton.style.cssText = "text-align: center; margin-top: 20px; display: none";
stopButton.setAttribute("id", "stopButton");
stopButton.innerHTML = "Stop";
merryRefresher.appendChild(stopButton);

if(document.URL.indexOf("worlds/roo/merrygoround.phtml") != -1) {
auxFlag = parseInt(localStorage.getItem("isFlag"));
petInputs = getPetList();

if (auxFlag == 1){
hidestart();

counter = parseInt(localStorage.getItem("Rounds"));
counter++;
localStorage.setItem("Rounds", counter);
auxPet = localStorage.getItem("thePet");
sendPet(auxPet, petInputs);
}
else{
// add startButton onClick
startButton.addEventListener("click", () => {
const selectedPet = petChoice.selectedIndex;
const chosenPetname = petChoice.options[selectedPet].value;
localStorage.setItem("thePet", chosenPetname);
console.log(selectedPet)
if (selectedPet != 0) {
//while(flag == 1){
flag = 1;
localStorage.setItem("isFlag", flag);
counter = 1;
localStorage.setItem("Rounds", counter);
sendPet(chosenPetname, petInputs);
//}
}
});

}

// add stopButton onClick
stopButton.addEventListener("click", () => { stopall() });
}

if(document.URL.indexOf("worlds/roo/merrygoround2.phtml") != -1) {
auxFlag = parseInt(localStorage.getItem("isFlag"));

if(auxFlag == 0){
hideall();
}
else{
hidestart();

// Get pet's mood
auxPet = localStorage.getItem("thePet");
//moodPet = asyncCall(auxPet);
moodPet = await getMood(auxPet);
counter = parseInt(localStorage.getItem("Rounds"));
merryStatus.innerHTML = "Mood: " + moodPet + " | Rounds: " + counter.toString();
merryStatus.style.display = "inline-block";

// add stopButton onClick
stopButton.addEventListener("click", () => { stopall() });

timeout2 = setTimeout(function () { goback();},6000);
}
}

function sendPet(petname, petInps){
// Select radio button
for (const input of petInps) {
if (input.value == petname) {
input.click();
}
}

timeout1 = setTimeout(function () { roundForm[0].submit();},5000);
}

function getPetList(){
let petNameInputs = []; // checkbox inputs
let petNamesList = []; // petnames

// Get list of inputs values (names) whose parent is a list item
const inputList = document.querySelectorAll("li > input");
for (const input of inputList) {
// petname
const name = input.value;
// build names list
if (!petNamesList.includes(name)) {
const option = document.createElement("option");
option.value = name;
option.text = name;
petChoice.add(option);
petNameInputs.push(input);
petNamesList.push(name);
}
}
return petNameInputs;
}

async function getMood(petname){
const response = await fetch("[Only registered and activated users can see links]");
// Get HTML page as text
const text = await response.text();
const div = document.createElement('div');
div.innerHTML = text;

// Get mood info
console.log(div.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`).innerText);
return div.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`).innerText;
}

function getMood1(petname,t){
fetch("[Only registered and activated users can see links]").then(response=>response.text()).
then(response=>parser.parseFromString(response,"text/html")).
then(response=>{const t=response.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`);
return t?t.textContent:0});
}

function goback() {
window.location.href = '[Only registered and activated users can see links]';
}

function stopall(){
clearTimeout(timeout1);
clearTimeout(timeout2);
flag = 0;
counter = 0;
localStorage.setItem("isFlag", flag);
localStorage.setItem("Rounds", counter);

if(document.URL.indexOf("worlds/roo/merrygoround.phtml") != -1) {
hidestop();
}
if(document.URL.indexOf("worlds/roo/merrygoround2.phtml") != -1) {
hideall();
}

goback();
}

function hidestart(){
startButton.style.display = "none";
petChoice.style.display = "none";
merryStatus.style.display = "none";
stopButton.style.display = "inline-block";
}

function hidestop(){
startButton.style.display = "inline-block";
petChoice.style.display = "inline-block";
stopButton.style.display = "none";
}

function hideall(){
merryRefresher.style.display = "none";
}

async function asyncCall(petname) {
console.log('calling');
const result = await getMood(petname,t);
console.log(result);
console.log(t);
return result;
}

})();

Nyu
02-16-2024, 09:55 PM
I'm sorry to keep bothering, the eslint error came back again. Could it be possible that my TM is outdated or sth? :S



Don't worry, I'm happy to help! c:
The problem is the same, but you have the whole script inside a function that is not async.

Just take out the wrapping code and it should work
I mean this



(function() {
'use strict';
...

})();







// ==UserScript==
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
// ==/UserScript==




/* global $ */
const parser=new DOMParser;
const quickref="[Only registered and activated users can see links]";
let t;


let timeout1, timeout2;
let flag, counter, moodPet;
let auxFlag, auxCounter, auxPet, auxInps, auxInput;
let petInputs = []; // global checkbox inputs
const roundForm = $("form[action='merrygoround2.phtml' i]");


// Upperbar
var uppertext=$("[class='content']")[0];


const merryRefresher = document.createElement("div");
merryRefresher.style.cssText = "text-align: center; padding: 10px;";
merryRefresher.setAttribute("id", "merryRefresher");
merryRefresher.innerHTML = "<br><br><b>-* Merry Go Round Refresher *-</b><br><br>";
uppertext.prepend(merryRefresher);


// Pet Status
const merryStatus = document.createElement("p");
merryStatus.style.cssText = "text-align: center; display: none";
merryStatus.setAttribute("id", "merryStatus");
merryRefresher.appendChild(merryStatus);


// Combobox
const petChoice = document.createElement("select");
petChoice.style.cssText = "text-align: center";
petChoice.setAttribute("id", "petChoice");
petChoice.innerHTML = "<option disabled selected hidden>Choose your pet</option>";
merryRefresher.appendChild(petChoice);


const lineBreak = document.createElement("br");
merryRefresher.appendChild(lineBreak);


const startButton = document.createElement("button");
startButton.style.cssText = "text-align: center; margin-top: 20px";
startButton.setAttribute("id", "startButton");
startButton.innerHTML = "Start";
merryRefresher.appendChild(startButton);


const stopButton = document.createElement("button");
stopButton.style.cssText = "text-align: center; margin-top: 20px; display: none";
stopButton.setAttribute("id", "stopButton");
stopButton.innerHTML = "Stop";
merryRefresher.appendChild(stopButton);


if(document.URL.indexOf("worlds/roo/merrygoround.phtml") != -1) {
auxFlag = parseInt(localStorage.getItem("isFlag"));
petInputs = getPetList();


if (auxFlag == 1){
hidestart();


counter = parseInt(localStorage.getItem("Rounds"));
counter++;
localStorage.setItem("Rounds", counter);
auxPet = localStorage.getItem("thePet");
sendPet(auxPet, petInputs);
}
else{
// add startButton onClick
startButton.addEventListener("click", () => {
const selectedPet = petChoice.selectedIndex;
const chosenPetname = petChoice.options[selectedPet].value;
localStorage.setItem("thePet", chosenPetname);
console.log(selectedPet)
if (selectedPet != 0) {
//while(flag == 1){
flag = 1;
localStorage.setItem("isFlag", flag);
counter = 1;
localStorage.setItem("Rounds", counter);
sendPet(chosenPetname, petInputs);
//}
}
});


}


// add stopButton onClick
stopButton.addEventListener("click", () => { stopall() });
}


if(document.URL.indexOf("worlds/roo/merrygoround2.phtml") != -1) {
auxFlag = parseInt(localStorage.getItem("isFlag"));


if(auxFlag == 0){
hideall();
}
else{
hidestart();


// Get pet's mood
auxPet = localStorage.getItem("thePet");
//moodPet = asyncCall(auxPet);
moodPet = await getMood(auxPet);
counter = parseInt(localStorage.getItem("Rounds"));
merryStatus.innerHTML = "Mood: " + moodPet + " | Rounds: " + counter.toString();
merryStatus.style.display = "inline-block";


// add stopButton onClick
stopButton.addEventListener("click", () => { stopall() });


timeout2 = setTimeout(function () { goback();},6000);
}
}


function sendPet(petname, petInps){
// Select radio button
for (const input of petInps) {
if (input.value == petname) {
input.click();
}
}


timeout1 = setTimeout(function () { roundForm[0].submit();},5000);
}


function getPetList(){
let petNameInputs = []; // checkbox inputs
let petNamesList = []; // petnames


// Get list of inputs values (names) whose parent is a list item
const inputList = document.querySelectorAll("li > input");
for (const input of inputList) {
// petname
const name = input.value;
// build names list
if (!petNamesList.includes(name)) {
const option = document.createElement("option");
option.value = name;
option.text = name;
petChoice.add(option);
petNameInputs.push(input);
petNamesList.push(name);
}
}
return petNameInputs;
}


async function getMood(petname){
const response = await fetch("[Only registered and activated users can see links]");
// Get HTML page as text
const text = await response.text();
const div = document.createElement('div');
div.innerHTML = text;


// Get mood info
console.log(div.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`).innerText);
return div.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`).innerText;
}


function getMood1(petname,t){
fetch("[Only registered and activated users can see links]").then(response=>response.text()).
then(response=>parser.parseFromString(response,"text/html")).
then(response=>{const t=response.querySelector(`#${petname}_details .pet_stats tr:nth-child(7) td`);
return t?t.textContent:0});
}


function goback() {
window.location.href = '[Only registered and activated users can see links]';
}


function stopall(){
clearTimeout(timeout1);
clearTimeout(timeout2);
flag = 0;
counter = 0;
localStorage.setItem("isFlag", flag);
localStorage.setItem("Rounds", counter);


if(document.URL.indexOf("worlds/roo/merrygoround.phtml") != -1) {
hidestop();
}
if(document.URL.indexOf("worlds/roo/merrygoround2.phtml") != -1) {
hideall();
}


goback();
}


function hidestart(){
startButton.style.display = "none";
petChoice.style.display = "none";
merryStatus.style.display = "none";
stopButton.style.display = "inline-block";
}


function hidestop(){
startButton.style.display = "inline-block";
petChoice.style.display = "inline-block";
stopButton.style.display = "none";
}


function hideall(){
merryRefresher.style.display = "none";
}


async function asyncCall(petname) {
console.log('calling');
const result = await getMood(petname,t);
console.log(result);
console.log(t);
return result;
}

DarkSkies
02-17-2024, 12:56 AM
Hey folks, wondering if you both landed on a final version of this script? My depressed pet is making me ya know... depressed haha

Here it is! ([Only registered and activated users can see links])

Let me know if you have any trouble Praise :tiger:


Nyu that made it work btw! Thank you very very much :3