javascript. alert when sound plays - javascript

I am basically trying to make a auto better on earn.gg (dice)
Is there a way to alert("win") if a sound is playing in the chrome tab? I will run the script in the console of that tab.
I have tried to do "roll();" in the console and it rolled the dice and returned me "value:false" because i lost and when i win it returns me value:true.So i figured to use that and make the code below. That simply didn't work and returned me "win" every time. Screenshot: http://prntscr.com/fpeoxb
roll();
if(value=true){
console.log("win");
}else if(value=false){
console.log("lose");
}

Related

Audio files (.mp3) don't play on page reload, only once when VS code/Live Server extension auto-reloads on save

Tested on Chrome/Firefox/Edge. I'm working on Win10 with VS code and the "Live Server" extension.
The page loads, first audio file(.mp3) plays, then different elements appear with Jquery delays, etc. Upon a certain < ul > fading in, the second audio file plays.
With "Live Server" 's auto-reloading on save feature or by clicking on my navbar's link to the same page (testing purposes), the sounds and Jquery functions work fine, but if I refresh with the browser itself, the sound files don't play again.
I tried solutions found on google, like setting currentTime, onLoad, but they didn't really answer a similar problem, the most success I've had was achieving the same results I had from the start.
I'm just starting to learn JS and Jquery, so I'm sorry if the code is messy.
<script type="text/javascript" src="script/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="script/main.js"></script>
<script>
var audio1 = $("#mantra")[0];
var audio2 = $("#wind")[0];
audio1.play()
$("document").ready(function () {
$(".choix").hide() // .choix is the class of fig1, fig2, fig3, fig4
$("#fig1").delay(9500).fadeIn(3000);
$("#fig2").delay(9550).fadeIn(3000);
$("#fig3").delay(9600).fadeIn(3000);
$("#fig4").delay(9650).fadeIn(3000);
$("#tagline").hide().delay(2000).fadeIn(1500)
.delay(1500).fadeOut(2000).queue(function(n) {
$(this).html("<br> Start here");
n();
}).delay(700).fadeIn(2000);
$(".textNav").hide().delay(800).fadeIn(1500);
});
$("#wind").stop("true").delay(9400).queue(function() { //9400ms delay to start just before fig1+
audio2.play()
});
});
</script>
In cases when the audio is not playing, check the console and you will find an error stating
DOMException: play() failed because the user didn't interact with the
document first.
This is because browsers don't allow music autoplay on page-load. It is necessary for the user to make some interaction (click, tap, etc.) with the window before playing the audio.
Source

Chrome inactive tab not executing setInterval

I like listening music with a Chrome tab on Youtube while I'm working, but every hour the music stops and a message "Are you still there?" is displayed on it.
Yes, I'm still there, like everyday, 7 times a day :)
So I installed a Chrome extension called "Custom Stylesheet & Script" to run this simple JavaScript stuff:
setInterval(function(){
let overlay = document.querySelector('iron-overlay-backdrop');
if(overlay) overlay.click();
}, 1000);
This works great, but only if I am on the YouTube tab. If I'm working on another one the music eventually stops and I have to click on the YT tab for this JavaScript to run (so not really a win after all).
I found some posts mentioning that setInterval in inactive tabs have a low priority and will only trigger once or twice a second, in my case they don't trigger at all.
Thanks for any hint on what to look at :)
After some tests, I found that the click on the "Ok" button to confirm was disabled by YouTube if the chrome tab isn't active, the setInterval was perfectly working after all.
If anyone is interested, I tried the MutationObserver method suggested by wOxxOm, and instead of clicking the confirmation button I just reload the page.
Here is the code I'm using now:
if(-1 !== window.location.host.indexOf('youtube')) {
document.addEventListener('DOMContentLoaded', (event) => {
let popupContainer = document.querySelector('ytd-popup-container');
let observer = new MutationObserver(function(mutationsList) {
let confirmButton = popupContainer.querySelector('.yt-confirm-dialog-renderer paper-button');
if(confirmButton){
// confirmButton.click();
document.location.reload(true);
}
});
observer.observe(popupContainer, {childList: true});
});
}

Check if Hulu is Playing in Applescript/Javascript

I use BetterTouchTool to run Applescripts on my Macbook Pro touch bar to navigate Netflix and YouTube. I just subscribed to Hulu, so I'm trying to make the scripts work for that as well. I know how to pause the player—I tell the tab Hulu is playing in to run this Javascript:
execute javascript "document.getElementsByClassName('controls__playback-button keep-mouse-active controls__playback-button--playing')[0].click()"
Resuming is similar:
execute javascript "document.getElementsByClassName('controls__playback-button keep-mouse-active controls__playback-button--paused')[0].click()"
What I need to do is check whether or not Hulu is currently playing video. I'm not familiar with Javascript or HTML, but what I think I need to do is use
set huluState to execute javascript "xyz"
to check if the "PLAY" thingy is there in this class in the Hulu Player:
<div class="controls__playback-button-playing-tip button-tool-tips">PLAY</div>
Then I would use
if huluState = "PLAY" then
return "Playing"
else if huluState = "PAUSE" then
return "Paused"
end if
to determine what shows up on the BTT button and a similar snippet of code to either play or pause.
TL;DR: I need to get the "PLAY" or "PAUSE" from the class within the Hulu player with Javascript.

Custom javascript extension wont execute my script

I have this code I want to execute everytime I watch a youtube video:
alert("Test alert");
var title = $(".style-scope ytd-video-primary-info-renderer").textContent.toUpperCase();
var blackword = "fortnite".toUpperCase();
if (title.includes(blackword)) {
alert("Video contains fortnite!");
window.location.href = "https://www.youtube.com";
}
When I visit www.youtube.com I get the test alert and nothing else happens which is fine because the script should execute only when the youtube title contains the word 'fortnite'.
I have two problems;
when I click on the video, even the test alert doesn't execute. But if I refresh the video window (with the URL something like https://www.youtube.com/watch?v=XYZ), test alert shows up, but other script doesn't execute. (When I input it manually in the console, it works..)
Does anybody seem to know what is the problem?
I am using chrome extension Custom Javascript.

InvalidStateError in IE11 when trying to rewind and pause video

I am building a website where videos start to play when you move the mousepointer over them. When a user leaves the video area it pauses and jumps back to the first frame. This works perfectly in every browser besides IE. When I open the dev console it shows me an "InvalidStateError" right above the part of code that handles the stop function. Why is IE behaving like that? Thanks for any input on this.
Here is the part of code that triggers the error:
var figure = $('.servus_video').hover(playVideo, stopVideo);
function playVideo(e) {
$('video', this).get(0).play();
}
function stopVideo(e) {
$('video', this).get(0).currentTime = 0;
$('video', this).get(0).pause();
}
Screenshot from IE11 debugger
Ok, after hours of troubleshooting I realized that IE responded with "Invalid Source" which I couldn't see because I disabled controls for the video. After tripple checking my encoder settings and verifying that they were correct I stumbled upon a document in which MS states that the maximum supported height of a video file is 1088px. 1088!? My videos were 720x1280px (portrait). After changing the resolution to 612x1088px everything worked.
https://msdn.microsoft.com/en-us/library/windows/desktop/dd797815(v=vs.85).aspx

Categories