@@ -1294,7 +1294,7 @@ function initScoreboardSubmissions() {
12941294}
12951295
12961296$ ( function ( ) {
1297- function checkExperimentalFeature ( ) {
1297+ window . checkExperimentalFeature = function ( ) {
12981298 if ( "documentPictureInPicture" in window ) {
12991299 // Only the team interface has this button.
13001300 const togglePipButton = document . querySelector ( "#pop-out-button" ) ;
@@ -1308,6 +1308,81 @@ $(function () {
13081308 pipMessage . style . display = 'inline' ;
13091309 }
13101310 }
1311- }
1311+ } ;
13121312 checkExperimentalFeature ( ) ;
1313- } ) ;
1313+
1314+ // Heavily based on: https://github.com/mdn/dom-examples/tree/main/document-picture-in-picture
1315+ const popOutFrame = document . querySelector ( "#pop-out-frame" ) ;
1316+ const popOutContainer = document . querySelector ( "#pop-out-container" ) ;
1317+ async function togglePictureInPicture ( ) {
1318+ if ( ! ( popOutFrame && popOutContainer ) ) {
1319+ console . log ( "Unexpected page, we only run if both are available." ) ;
1320+ return ;
1321+ }
1322+
1323+ popOutContainer . style . display = 'block' ;
1324+ // Early return if there's already a Picture-in-Picture window open
1325+ if ( window . documentPictureInPicture . window ) {
1326+ popOutContainer . append ( popOutFrame ) ;
1327+ window . documentPictureInPicture . window . close ( ) ;
1328+ return ;
1329+ }
1330+
1331+ // Open a Picture-in-Picture window.
1332+ const pipWindow = await window . documentPictureInPicture . requestWindow ( {
1333+ width : popOutFrame . clientWidth ,
1334+ height : popOutFrame . clientHeight
1335+ } ) ;
1336+
1337+ pipWindow . document . body . style . padding = 0 ;
1338+ pipWindow . document . body . style . margin = 0 ;
1339+ const scoreBoards = document . querySelectorAll ( "#pop-out-frame .scoreboard" ) ;
1340+ scoreBoards . forEach ( ( scoreBoard ) => {
1341+ scoreBoard . style . margin = 0 ;
1342+ } ) ;
1343+ console . log ( "sb" , scoreBoards ) ;
1344+
1345+ // Add pagehide listener to handle the case of the pip window being closed using the browser X button
1346+ pipWindow . addEventListener ( "pagehide" , ( event ) => {
1347+ popOutContainer . style . display = 'none' ;
1348+ popOutContainer . append ( popOutFrame ) ;
1349+ } ) ;
1350+
1351+
1352+ // Copy style sheets over from the initial document
1353+ // so that the player looks the same.
1354+ [ ...document . styleSheets ] . forEach ( ( styleSheet ) => {
1355+ try {
1356+ if ( styleSheet . href ) {
1357+ const link = document . createElement ( "link" ) ;
1358+
1359+ link . rel = "stylesheet" ;
1360+ link . type = styleSheet . type ;
1361+ link . media = styleSheet . media ;
1362+ link . href = styleSheet . href ;
1363+
1364+ pipWindow . document . head . appendChild ( link ) ;
1365+ } else {
1366+ const cssRules = [ ...styleSheet . cssRules ]
1367+ . map ( ( rule ) => rule . cssText )
1368+ . join ( "" ) ;
1369+ const style = document . createElement ( "style" ) ;
1370+
1371+ style . textContent = cssRules ;
1372+ pipWindow . document . head . appendChild ( style ) ;
1373+ }
1374+ } catch ( e ) {
1375+ const link = document . createElement ( "link" ) ;
1376+
1377+ link . rel = "stylesheet" ;
1378+ link . type = styleSheet . type ;
1379+ link . media = styleSheet . media ;
1380+ link . href = styleSheet . href ;
1381+
1382+ pipWindow . document . head . appendChild ( link ) ;
1383+ }
1384+ } ) ;
1385+
1386+ pipWindow . document . body . append ( popOutFrame ) ;
1387+ }
1388+ } ) ;
0 commit comments