11export default async function ( { feature, console } ) {
2- const canvas = feature . traps . vm . renderer . canvas ;
3-
4- let video = document . createElement ( "video" ) ;
5- // video.setAttribute("controls", "controls");
6- video . setAttribute ( "autoplay" , "autoplay" ) ;
7- video . setAttribute ( "style" , "width: 100%; height: 100%" ) ;
8- // document.querySelector(".preview .inner").append(video);
9-
10- video . srcObject = canvas . captureStream ( 30 )
11-
122 await new Promise ( async ( resolve , reject ) => {
133 ( async ( ) => {
144 const rem = await ScratchTools . waitForElement ( ".preview .inner .flex-row.action-buttons" )
@@ -20,6 +10,7 @@ export default async function ({ feature, console }) {
2010 } ) ( ) ;
2111 } )
2212
13+ const canvas = feature . traps . vm . renderer . canvas ;
2314 let openPopup = document . createElement ( "button" ) ;
2415
2516 ScratchTools . waitForElements ( ".preview .inner .flex-row.action-buttons" , async function ( row ) {
@@ -45,12 +36,110 @@ export default async function ({ feature, console }) {
4536 } )
4637 } )
4738
48- function popup ( ) {
49- try {
50- video . requestPictureInPicture ( )
39+ let popup ;
40+
41+ if ( feature . settings . get ( "interactivity-PiP" ) ) {
42+ if ( ! "documentPictureInPicture" in window ) console . error ( "Picture in Picture not supported" )
43+
44+ let pipWindow
45+
46+ let docPopup = document . createElement ( "div" ) ;
47+ docPopup . insertAdjacentHTML ( "afterbegin" , await ( await fetch ( feature . self . getResource ( "popup-html" ) ) ) . text ( ) )
48+ docPopup = docPopup . querySelector ( "div.popup-GUI" )
49+
50+ let video = docPopup . querySelector ( "video" ) ;
51+
52+ const greenFlag = document . querySelector ( ".green-flag_green-flag_1kiAo" )
53+ docPopup . querySelector ( ".popup-greenflag" ) . addEventListener ( "click" , ( ) => {
54+ greenFlag . click ( )
55+ } ) ;
56+ const redFlag = document . querySelector ( ".stop-all_stop-all_1Y8P9" )
57+ docPopup . querySelector ( ".popup-redflag" ) . addEventListener ( "click" , ( ) => {
58+ redFlag . click ( )
59+ } ) ;
60+
61+ // video.addEventListener("mousedown", (old_event) => {
62+ function translateEvent_pointer ( old_event ) {
63+ // Calculate the canvas position relative to the viewport
64+ const a_rect = canvas . getBoundingClientRect ( ) ;
65+ const b_rect = video . getBoundingClientRect ( ) ;
66+
67+ // console.log(old_event)
68+ // Create a new event with the adjusted coordinates
69+
70+ let new_event = new old_event . constructor ( old_event . type , {
71+ bubbles : old_event . bubbles ,
72+ cancelable : old_event . cancelable ,
73+ clientX : ( old_event . clientX - b_rect . left ) * ( a_rect . width / b_rect . width ) + a_rect . left ,
74+ clientY : ( old_event . clientY - b_rect . top ) * ( a_rect . height / b_rect . height ) + a_rect . top ,
75+ // Copy over other necessary properties from the old event
76+ screenX : ( old_event . screenX - pipWindow . screenLeft + window . screenLeft - b_rect . left ) * ( a_rect . width / b_rect . width ) + a_rect . left ,
77+ screenY : ( old_event . screenY - pipWindow . screenTop + window . screenTop - b_rect . top ) * ( a_rect . height / b_rect . height ) + a_rect . top ,
78+ layerX : old_event . layerX ,
79+ layerY : old_event . layerY ,
80+ button : old_event . button ,
81+ buttons : old_event . buttons ,
82+ relatedTarget : old_event . relatedTarget ,
83+ altKey : old_event . altKey ,
84+ ctrlKey : old_event . ctrlKey ,
85+ shiftKey : old_event . shiftKey ,
86+ metaKey : old_event . metaKey ,
87+ movementX : old_event . movementX ,
88+ movementY : old_event . movementY ,
89+ } ) ;
90+
91+ // Dispatch the new event
92+ canvas . dispatchEvent ( new_event ) ;
93+ }
94+ video . addEventListener ( "mousedown" , translateEvent_pointer )
95+ video . addEventListener ( "mouseup" , translateEvent_pointer )
96+ video . addEventListener ( "mousemove" , translateEvent_pointer )
97+ video . addEventListener ( "wheel" , translateEvent_pointer )
98+ video . addEventListener ( "touchstart" , translateEvent_pointer )
99+ video . addEventListener ( "touchend" , translateEvent_pointer )
100+ video . addEventListener ( "touchmove" , translateEvent_pointer )
101+
102+ function translateEvent_key ( old_event ) {
103+ let new_event = new KeyboardEvent ( old_event . type , old_event )
104+ document . dispatchEvent ( new_event ) ;
51105 }
52- catch {
53- console . log ( "Picture in Picture not supported or failed to request" )
106+
107+ let buttonClickedTimes = 0
108+ popup = async function ( ) {
109+ if ( buttonClickedTimes === 0 ) {
110+ video . srcObject = canvas . captureStream ( )
111+ buttonClickedTimes ++
112+ }
113+ // Open a Picture-in-Picture window.
114+ pipWindow = await window . documentPictureInPicture . requestWindow ( {
115+ width : canvas . width ,
116+ height : canvas . height + 20 + 6 * 2 ,
117+ } ) ;
118+
119+ // Move the player to the Picture-in-Picture window.
120+ pipWindow . document . body . append ( docPopup ) ;
121+
122+ pipWindow . document . addEventListener ( "keydown" , translateEvent_key )
123+ pipWindow . document . addEventListener ( "keypress" , translateEvent_key )
124+ pipWindow . document . addEventListener ( "keyup" , translateEvent_key )
125+ }
126+ }
127+ else {
128+ let video = document . createElement ( "video" ) ;
129+ // video.setAttribute("controls", "controls");
130+ video . setAttribute ( "autoplay" , "autoplay" ) ;
131+ video . setAttribute ( "style" , "width: 100%; height: 100%" ) ;
132+ // document.querySelector(".preview .inner").append(video);
133+
134+ video . srcObject = canvas . captureStream ( )
135+
136+ popup = function ( ) {
137+ try {
138+ video . requestPictureInPicture ( )
139+ }
140+ catch {
141+ console . log ( "Picture in Picture not supported or failed to request" )
142+ }
54143 }
55144 }
56145}
0 commit comments