user34537
user34537

Reputation:

How do this script pause youtube videos?

I noticed this userscript (which is fairly popular for good reason) pauses the video on another tab whenever I press play on a video in this tab. The source was minified but it appears this is it https://github.com/YePpHa/YouTubeCenter/blob/master/src/YouTubeCenter.user.js

I had no idea where to start so I search all instance of 'pause'. I thought line 22801 was it but I don't think so anymore. Also I couldn't comprehend what was happening. Typing ytcenter._intercomOnPlayer in the console gave me undefined. 8 lines below it (22809) it says "ytcenter._intercomOnPlayer" so I don't think I am reading the right area. I see "@grant GM_xmlhttpRequest". I looked at the network tab in firebug and saw nothing, at least nothing that appears to be related.

How is it communicating with another tab to pause the video?

Upvotes: 3

Views: 134

Answers (1)

Entity Black
Entity Black

Reputation: 3491

I guess this is responsible:

ytcenter.player.network.pause();

line 22893

I found it via wiki github link https://github.com/YePpHa/YouTubeCenter/wiki/Features#Only_One_Player_Instance_Playing in the code I was looking for "Only_One_Player_Instance_Playing" phrase

Also as I though, tabs can't communicate with each other of course. It has to be done thru youtube api...

UPDATE

We have a new question over there. How does that line ytcenter.player.network.pause() works?

I werent able to run code on my own. There was some error and I was too lazy to fix it. But I was following pause method. First I think that it is native function of youtube api but I was wrong. It is line 18600. Inside is another key

intercom.emit("player", {
        action: "pause",
        origin: intercom.origin
      });

which leads deeper to emit function. As you could guess, emit is also not native function of youtube API. It calls _emit function and some event. _emit is the key one on line 10754. This is the end, some magic is done inside including localStorage and settimeout. I belive this is the answer. Local storage is shared and I saw somewhere on my way setInterval function. Once video is paused, it emits message into local storage, which is keep just for a short period. Other tabs will notice that via setInterval.

Upvotes: 1

Related Questions