async function querySceneRating(sceneId) {
const gqlQuery = {
'query': `query {
findScene(id: ${sceneId}){
rating
}
}`
};
let rating = await stash.callGQL(gqlQuery);
if (!rating.data.findScene) {
// handle the error here
console.log("Error with querySceneRating GraphQL: " + rating);
}
return rating
}
async function updateSceneRating(sceneId, rating) {
console.log("Updating Rating")
console.log("updateSceneRating sceneId " + sceneId)
console.log("updateSceneRating rating " + rating)
const reqData = {
"operationName": "sceneUpdate",
"variables": {
"input": {
"id": sceneId,
"rating100": rating * 20
}
},
"query": `mutation sceneUpdate($input: SceneUpdateInput!){
sceneUpdate(input: $input){
id
rating100
}
}`
}
const response = await stash.callGQL(reqData);
currentRating = rating;
setTimeout(function() {
updateElements(currentRating);
}, 2000)
console.log("updateSceneRating: rating: " + currentRating)
}
function updateElements(rating) {
let sceneId = updateSceneId();
console.log("updateElements sceneId" + sceneId);
console.log("updateElements rating" + rating);
// code to update the elements here
waitForElementId("video-stars", async function(IdName, el) {
console.log("updateElements: rating: " + rating);
currentRating = rating;
console.log("updateElements: currentRating: " + currentRating)
const customdivElements = document.getElementById('video-stars')
const ratingButtons = customdivElements.querySelectorAll('.rating-stars button');
// Set Classes & Titles inital values
if (currentRating == null) {
console.log("updateElements - rating == null: ");
updateTitles(0);
updateClasses(0);
} else {
console.log("updateElements - rating != null - rating: " + rating);
updateTitles(currentRating);
updateClasses(currentRating);
}
})
return
}
function updateSceneId() {
const currentUrl = window.location.href.split('?')[0];
const sceneId = currentUrl.substring(currentUrl.lastIndexOf('/') + 1);
console.log("updateSceneId" + sceneId);
return sceneId
}
function updateTitles(num) {
const customdivstitle = document.getElementById('video-stars')
const ratingButtons = customdivstitle.querySelectorAll('.rating-stars button');
ratingButtons.forEach(btn => {
btn.title = num;
});
}
function updateClasses(num) {
const customdivclasses = document.getElementById('video-stars')
const ratingButtons = customdivclasses.querySelectorAll('.rating-stars button');
ratingButtons.forEach((btn, i) => {
if (i < num) {
btn.classList.remove('star-fill-0');
btn.classList.add('star-fill-100');
} else {
btn.classList.add('star-fill-0');
btn.classList.remove('star-fill-100');
}
});
}
stash.addEventListener("page:scene", async function() {
let sceneId = updateSceneId();
// let rating = (await querySceneRating(sceneId)).data.findScene.rating;
waitForElementClass("rating-stars", function () {
const n = document.getElementsByClassName('rating-stars')[1];
let node = n.cloneNode(true);
waitForElementByXpath("//div[contains(@class, 'VideoPlayer')]", function(xpath, el) {
if (!document.getElementById('video-stars')) {
const control = el.querySelector('.vjs-control-bar');
node.setAttribute('id', 'video-stars');
// ratingStarsNode.innerHTML = ratingstars;
control.appendChild(node);
control.insertBefore(node, control.childNodes[9]);
}
// Get current set rating or if on first pass get rating from HTMl
let rating = currentRating || node.querySelector('button').title * 20;
console.log("xpath rating: " + rating);
const customdiv = document.getElementById('video-stars');
const ratingButtons = customdiv.querySelectorAll('button');
const unfilledstar = customdiv.querySelectorAll('.unfilled-star div');
// const sceneid = updateSceneId();
rating = Math.floor(rating / 20);
ratingButtons.forEach((btn, index) => {
index += 1
btn.addEventListener('mouseover', event => {
if (rating !== "0" && rating !== index) {
console.log('mouseover-rating !== "0" && rating !== index - ' + "index: " + index + " rating: " + rating);
updateTitles(index);
updateClasses(index);
}
});
ratingButtons.forEach((btn, index) => {
index += 1
btn.addEventListener('mouseover', event => {
if (index === rating) {
ratingButtons.forEach(button => {
console.log('index === rating - ' + "index: " + index + " rating: " + rating)
updateTitles("Unset");
})
}
}
)
})
btn.addEventListener('mouseout', event => {
if (rating !== "0" && rating !== btn.title) {
console.log('mouseout: rating !== "0" && rating !== event.target.title-: ' + 'btn.title: ' + btn.title + ' event.target.title: ' + event.target.title + ' rating: ' + rating);
if (rating !== "0") {
updateTitles(rating);
updateClasses(rating);
console.log('mouseout: rating: ' + rating);
}
}});
btn.addEventListener('click', event => {
if (btn.title !== "Unset") {
console.log("click: btn: Not unset - btn.title - " + btn.title);
updateSceneRating(sceneId, btn.title);
} else {
updateTitles("0");
updateClasses(0);
console.log("click: btn: Unset: " + "0");
updateSceneRating(sceneId, null);
}
});
});
unfilledstar.forEach((div, index) => {
index += 1
unfilledstar.addEventListener('click', event => {
if (ratingButtons.title == "Unset") {
console.log("click: unfilled star: unset");
updateTitles("0");
updateClasses(0);
updateSceneRating(sceneId, null);
} else {
console.log("click: unfilled star: Title != unfill");
updateTitles(event.target.title);
updateClasses(event.target.title);
updateSceneRating(sceneId, event.target.title);
}
});
});
document.addEventListener("DOMContentLoaded", () => {
console.log("Loaded")
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
Array.from(mutation.addedNodes).forEach(addedNode => {
if (addedNode.matches && addedNode.matches('.fade.toast.success.show')) {
setTimeout(function() {
updateElements();
}, 2000);
}
});
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
})
})
})
})();" on the first pass through the button event listeners use let "node.querySelector('button').title * 20;" to determine the current rating. Once a rating star has been clicked the button passes the rating to the update rating function and that to the updateElements function. Then it's supposed to follow this logic if hovering or clicking on the button event listeners a second time and so on. To use " let rating = currentRating || node.querySelector('button').title * 20;" to get to current most up to date rating value (The one that was just set) and have the event listeners use the updated value instead of the old one. But it isn't working.
Lastly, make sure that sceneId is available in the scope of the event listeners. If it's not, you might need to pass it as an argument to the function where you're defining the event listeners.