in " const { stash, Stash, waitForElementId, waitForElementClass, waitForElementByXpath,…
- User
in " const { stash, Stash, waitForElementId, waitForElementClass, waitForElementByXpath, getElementByXpath, getClosestAncestor, updateTextInput, } = unsafeWindow.stash;
code// Function to unset rating and send it to the graphql function function unsetRating() { // Remove "selected" class from all stars ratingStars.forEach((star) => { star.classList.remove('selected'); }); // reset currentRating variable currentRating = 0; // Send the unset rating to the GraphQL function sendRatingToGraphQL(currentRating); }let sceneId; let currentrating stash.addEventListener("page:scene", async function() {
function updateSceneId() { const currentUrl = window.location.href.split('?')[0]; sceneId = currentUrl.substring(currentUrl.lastIndexOf('/') + 1); console.log(sceneId); }
updateSceneId();
async function querySceneRating() { const gqlQuery = { 'query':
query { findScene(id: ${sceneId}){ rating } }}; return stash.callGQL(gqlQuery); }const rating = (await querySceneRating()).data.findScene.rating; currentrating = rating; console.log("currentrating 1: " +currentrating);
codewaitForElementByXpath("//div[contains(@class, 'VideoPlayer')]", function (xpath, el) { if (!document.getElementById('custom-stars-row')) { const control = el.querySelector('.vjs-control-bar'); let ratingStarsNode = document.createElement('div'); ratingStarsNode.setAttribute('id', 'custom-stars-row'); ratingStarsNode.innerHTML = ratingstars; control.appendChild(ratingStarsNode); control.insertBefore(ratingStarsNode, control.childNodes[9]); } const ratingButtons = document.querySelectorAll('.rating-stars button'); - Assistant
It seems like the issue you're facing is due to the scope of the
currentRatingvariable. If you want to accesscurrentRatinginside the event listeners, you need to ensure that it's defined in a scope that's accessible to those listeners.Here's a way to refactor your code to make accessible:
- User
I need to remove these lines "const currentRating = getCurrentRating();" and if (parseInt(currentRating) !== "0" && parseInt(currentRating) should get current rating from where it's first defined but when I do this, it's inaccessible?
- Assistant
It seems like the issue you're facing is due to the scope of the
currentRatingvariable. If you want to accesscurrentRatinginside the event listeners, you need to ensure that it's defined in a scope that's accessible to those listeners.Here's a way to refactor your code to make
currentRatingaccessible:-
Define
currentRatingin the scope where is defined. This way, it will be accessible inside the event listeners.
-
- User
I already specified let currentrating it isn't working
- Assistant
I apologize for the confusion. If you've already defined
currentratingand it's not working, it might be due to the asynchronous nature of JavaScript.The
currentratingvariable is being updated inside an asynchronous functionquerySceneRating(). This means that the value ofcurrentratingmight not be immediately available when the event listeners are being set up.To ensure that
currentratingis available when the event listeners are set up, you can move the event listener setup inside thethenblock of the Promise returned byquerySceneRating(). Here's how you can do it:javascript - User
how do I do that? I need the value of currentrating from here " async function querySceneRating() { const gqlQuery = { 'query':
query { findScene(id: ${sceneId}){ rating } }}; return stash.callGQL(gqlQuery); }const rating = (await querySceneRating()).data.findScene.rating; currentrating = rating; console.log("currentrating 1: " +currentrating);" and I need that value in here " ratingButtons.forEach((btn, index) => { index += 1 btn.addEventListener('mouseover', event => {
codeif (parseInt(currentRating) !== "0" && parseInt(currentRating) !== index) { updateTitles(index); updateClasses(index); } });", do I simply add currentrating = querySceneRating() - Assistant
I apologize for the confusion. You can't directly assign the result of
querySceneRating()tocurrentratingbecausequerySceneRating()is an asynchronous function and returns a Promise.Here's how you can get the value of
currentratinginside the event listener:-
Define
currentratingin the scope whereratingButtonsis defined. This way, it will be accessible inside the event listeners. -
Update the value of
currentratinginside thethenblock of the Promise returned byquerySceneRating().
Here's how you can do it:
javascript -
- User
I can't pass it as an argument in "btn.addEventListener('mouseover', event => { if (parseInt(currentrating) !== "0" && parseInt(currentrating) !== index) { updateTitles(currentrating, index); updateClasses(currentrating, index); } });" currentrating isn't accessible from async function querySceneRating() { const gqlQuery = { 'query':
query { findScene(id: ${sceneId}){ rating } }}; return stash.callGQL(gqlQuery); }const rating = (await querySceneRating()).data.findScene.rating; currentrating = rating;
- Assistant
I see the issue now. Since
currentratingis defined asynchronously, it won't be accessible inside the event listener. Instead of passingcurrentratingas an argument toupdateTitlesandupdateClasses, you can passindexand calculate the current rating inside those functions. Here's how you can do it:-
Remove
currentratingfrom the event listener's argument list. -
Update
updateTitlesandupdateClassesfunctions to acceptindexas an argument and calculate the current rating usingcurrentrating.
Here's how you can refactor your code:
javascript -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.