简体   繁体   English

浏览器中的Javascript保存模式

[英]Javascript saving mode in browsers

I made a little code in JavaScript which play music on a streaming music website ( tidal, spotify napster etc... ) and change every x seconds. 我用JavaScript编写了一些代码,该代码在流音乐网站(潮汐,spotify小睡等)上播放音乐,并每隔x秒更改一次。 When the last song is listening it loops and plays the first song of the playlist. 当最后一首歌曲正在收听时,它会循环播放播放列表中的第一首歌曲。 ( I made this because on tidal.com webmaster didn't implement the functionality to loop the entire playlist ). (我这样做是因为在tidal.com上,网站站长未实现循环播放整个播放列表的功能)。

I injected my javascript using Greasemonkey and Tampermonkey on firefox and chrome and i got problems when i have 10 tabs opened ( so 10 javascript loop working together ). 我在Firefox和Chrome上使用Greasemonkey和Tampermonkey注入了我的javascript,当我打开10个选项卡时遇到了问题(因此10个javascript循环协同工作)。 I'm a beginner so maybe my code is really bad, just tell me. 我是一个初学者,所以也许我的代码真的很糟糕,只是告诉我。

So the code isn't complicated, i get the DOM element which will allow me to controls songs ( next / pause / first song ), and i .click() on those buttons when my conditions are true. 因此,代码并不复杂,我得到了DOM元素,该元素使我可以控制歌曲(下一首/暂停/第一首歌曲),当我的条件为真时,可以在这些按钮上单击.click()。 It works when i have ONE tab opened, i can see it is doing its purpose. 当我打开一个选项卡时,它可以工作,我可以看到它正在实现其目的。

But the problem is, when i run it with like 10 or 15 tabs opened, my browser seems to stop my javascript because i'm not on the page. 但是问题是,当我在打开10或15个选项卡的情况下运行它时,我的浏览器似乎停止了我的JavaScript,因为我不在页面上。 I can see that as soon as i come back on the page the script wake up and works. 我可以看到,只要回到页面上,脚本就会唤醒并起作用。 ( For example sometimes i come back on a page and i can see a song which is at 2:00 while i set in my code to go next when time is 35s.. ) (例如,有时我回到页面上,我看到一首歌曲是在2:00,而我将代码设置为在35s时继续播放。)

So : Is my code completely buggy or is it the browser ? 所以:我的代码是完全错误的还是浏览器? Chrome/Firefox do the same. Chrome / Firefox也是如此。 And BTW, i tested by injecting directly with the console ( F12 ) and it's the same. 顺便说一句,我通过直接使用控制台(F12)注入进行了测试,它是相同的。

 //First track of the playlist var firstTrack; //Next button var next; //current time var time; var lastTrackPlaying; //null if we are not on the last track var paused; //null if we are playing function initVar() { firstTrack = document.querySelector("button[title='Lire']"); next = document.getElementsByClassName("play-controls__next js-next")[0]; setInterval(checkTime,5000); //every 5sec, call checkTime } function checkTime() { time = document.getElementsByClassName("js-progress")[0].innerHTML; time = 60*Number(time[0]) + Number( time[2] + time[3]); //convert the time to int format lastTrackPlaying = document.querySelector("tr[class='js-tracklist__row ui-draggable ui-draggable-handle is-active is-playing'][data-track-id='86656183']"); paused = document.getElementsByClassName("play-controls__main-button js-main-button play-controls__main-button--paused")[0]; if( (lastTrackPlaying != null) && ( time >= 35 ) ) firstTrack.click(); //we are on last song, play first else if ( time >= 35 ) next.click(); //next song if 35 sec spent else if ( paused != null) firstTrack.click(); //if the playlist is paused, play the first song } setTimeout(initVar, 5000); //start the code with 5sec delay ( the website load ) 

This occures because browsers tend to slow down intervals in inactive tabs to optimize the performance. 发生这种情况是因为浏览器趋向于减慢非活动选项卡中的间隔以优化性能。

Check out the MDN for setInterval : 签出MDN的setInterval

Inactive tabs Requires Gecko 5.0(Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2) 非活动标签需要Gecko 5.0(Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2)

Starting in Gecko 5.0 (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2), intervals are clamped to fire no more often than once per second in inactive tabs. 从Gecko 5.0(Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2)开始,在不活动的选项卡中,间隔触发频率不超过每秒一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM