|
| 1 | +export default async function ({ feature, console }) { |
| 2 | + await new Promise(async (resolve, reject) => { |
| 3 | + (async () => { |
| 4 | + const rem = await ScratchTools.waitForElement(".preview .inner .flex-row.action-buttons") |
| 5 | + resolve(rem); |
| 6 | + })(); |
| 7 | + (async () => { |
| 8 | + const rem = await ScratchTools.waitForElement(".menu-bar_account-info-group_MeJZP") |
| 9 | + resolve(rem); |
| 10 | + })(); |
| 11 | + }) |
| 12 | + |
| 13 | + let openPopup = document.createElement("button"); |
| 14 | + |
| 15 | + ScratchTools.waitForElements(".preview .inner .flex-row.action-buttons", async function (row) { |
| 16 | + if (row.querySelector(".ste-video-recorder-open")) return; |
| 17 | + openPopup = document.createElement("button"); |
| 18 | + openPopup.className = "button action-button ste-video-recorder-open"; |
| 19 | + openPopup.textContent = "Record Video"; |
| 20 | + row.insertAdjacentElement("afterbegin", openPopup); |
| 21 | + openPopup.addEventListener('click', () => { |
| 22 | + document.body.append(popup) |
| 23 | + }) |
| 24 | + }) |
| 25 | + |
| 26 | + ScratchTools.waitForElements(".menu-bar_account-info-group_MeJZP", async function (row) { |
| 27 | + if (row.querySelector(".ste-video-recorder-open")) return; |
| 28 | + openPopup = document.createElement("div"); |
| 29 | + openPopup.className = "menu-bar_menu-bar-item_oLDa- menu-bar_hoverable_c6WFB"; |
| 30 | + openPopup.style.padding = "0 0.75rem" |
| 31 | + let rem = document.createElement("div"); |
| 32 | + rem.textContent = "Record Video"; |
| 33 | + openPopup.append(rem); |
| 34 | + row.insertAdjacentElement("afterbegin", openPopup); |
| 35 | + openPopup.addEventListener('click', () => { |
| 36 | + document.body.append(popup) |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + let popup = document.createElement("div"); |
| 41 | + popup.insertAdjacentHTML("afterbegin", await (await fetch(feature.self.getResource("popup-html"))).text()) |
| 42 | + popup = popup.querySelector("div.ReactModalPortal") |
| 43 | + |
| 44 | + let stopButton = popup.querySelector(".stopButton"); |
| 45 | + let startButton = popup.querySelector(".startButton"); |
| 46 | + let closeButton = popup.querySelector(".close-button_close-button_lOp2G"); |
| 47 | + let downloadButton = popup.querySelector(".downloadButton"); |
| 48 | + let lastDownloadFunction = () => { } |
| 49 | + let mimeType = popup.querySelector("select"); |
| 50 | + let microphoneCheckbox = popup.querySelector(".microphoneCheckbox"); |
| 51 | + let desktopSoundCheckbox = popup.querySelector(".desktopSoundCheckbox"); |
| 52 | + |
| 53 | + closeButton.addEventListener('click', () => { |
| 54 | + document.querySelector(".STE-ReactModalPortal").remove() |
| 55 | + }) |
| 56 | + addEventListener("keydown", (e) => { |
| 57 | + if (e.key === "Escape") { |
| 58 | + document.querySelector(".STE-ReactModalPortal").remove() |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + const canvas = feature.traps.vm.renderer.canvas; |
| 63 | + const preview = popup.querySelector("video") |
| 64 | + |
| 65 | + await new Promise(async (resolve, reject) => { |
| 66 | + (async () => { |
| 67 | + const rem = await ScratchTools.waitForElement("input.inplace-input") |
| 68 | + resolve(rem); |
| 69 | + })(); |
| 70 | + (async () => { |
| 71 | + const rem = await ScratchTools.waitForElement("input.project-title-input_title-field_en5Gd") |
| 72 | + resolve(rem); |
| 73 | + })(); |
| 74 | + (async () => { |
| 75 | + const rem = await ScratchTools.waitForElement(".project-title") |
| 76 | + resolve(rem); |
| 77 | + })(); |
| 78 | + }) |
| 79 | + |
| 80 | + let projectTitle = document.querySelector("input.inplace-input") || document.querySelector("input.project-title-input_title-field_en5Gd") || document.querySelector(".project-title"); |
| 81 | + |
| 82 | + ScratchTools.waitForElements("input.inplace-input", async function (_projectTitle) { |
| 83 | + projectTitle = _projectTitle |
| 84 | + }) |
| 85 | + |
| 86 | + ScratchTools.waitForElements("input.project-title-input_title-field_en5Gd", async function (_projectTitle) { |
| 87 | + projectTitle = _projectTitle |
| 88 | + }) |
| 89 | + |
| 90 | + ScratchTools.waitForElements(".project-title", async function (_projectTitle) { |
| 91 | + projectTitle = _projectTitle |
| 92 | + }) |
| 93 | + |
| 94 | + |
| 95 | + let mediaRecorder; |
| 96 | + let recordedChunks = []; |
| 97 | + |
| 98 | + startButton.addEventListener('click', async () => { |
| 99 | + startButton.classList.add("STE-hide-button"); |
| 100 | + stopButton.classList.remove("STE-hide-button"); |
| 101 | + |
| 102 | + // Capture the canvas element as a stream |
| 103 | + const canvasStream = canvas.captureStream(30); // 30 FPS |
| 104 | + |
| 105 | + // Get the audio context from the Scratch VM |
| 106 | + const audioContext = feature.traps.vm.runtime.audioEngine.audioContext; |
| 107 | + const audioDestination = audioContext.createMediaStreamDestination(); |
| 108 | + |
| 109 | + if (microphoneCheckbox.checked) { |
| 110 | + // Capture the microphone audio |
| 111 | + let micStream; |
| 112 | + try { |
| 113 | + micStream = await navigator.mediaDevices.getUserMedia({ audio: true }); |
| 114 | + } catch (err) { |
| 115 | + console.error("Error capturing microphone audio:", err); |
| 116 | + } |
| 117 | + |
| 118 | + if (micStream) { |
| 119 | + const micSource = audioContext.createMediaStreamSource(micStream); |
| 120 | + micSource.connect(audioDestination); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Connect the audio engine's output |
| 125 | + if (desktopSoundCheckbox.checked) { |
| 126 | + feature.traps.vm.runtime.audioEngine.inputNode.connect(audioDestination); |
| 127 | + } |
| 128 | + |
| 129 | + // Combine the canvas video track and audio tracks |
| 130 | + const combinedStream = new MediaStream(); |
| 131 | + canvasStream.getVideoTracks().forEach(track => combinedStream.addTrack(track)); |
| 132 | + if (microphoneCheckbox.checked || desktopSoundCheckbox.checked) { |
| 133 | + audioDestination.stream.getAudioTracks().forEach(track => combinedStream.addTrack(track)); |
| 134 | + } |
| 135 | + |
| 136 | + mediaRecorder = new MediaRecorder(combinedStream); |
| 137 | + |
| 138 | + mediaRecorder.ondataavailable = function (event) { |
| 139 | + if (event.data.size > 0) { |
| 140 | + recordedChunks.push(event.data); |
| 141 | + } |
| 142 | + }; |
| 143 | + |
| 144 | + mediaRecorder.onstop = function () { |
| 145 | + const blob = new Blob(recordedChunks, { |
| 146 | + type: `video/${mimeType.value}` |
| 147 | + }); |
| 148 | + preview.src = URL.createObjectURL(blob); |
| 149 | + preview.controls = true; |
| 150 | + // console.log(projectTitle) |
| 151 | + preview.download = `${projectTitle.value}.${mimeType.value}`; |
| 152 | + downloadButton.removeEventListener("click", lastDownloadFunction); |
| 153 | + lastDownloadFunction = async () => { |
| 154 | + const url = URL.createObjectURL(blob); |
| 155 | + const a = document.createElement('a'); |
| 156 | + a.href = url; |
| 157 | + a.download = `${projectTitle.value}.${mimeType.value}`; |
| 158 | + document.body.appendChild(a); |
| 159 | + a.click(); |
| 160 | + document.body.removeChild(a); |
| 161 | + URL.revokeObjectURL(url); |
| 162 | + } |
| 163 | + downloadButton.addEventListener("click", lastDownloadFunction); |
| 164 | + recordedChunks = []; |
| 165 | + }; |
| 166 | + |
| 167 | + mediaRecorder.start(); |
| 168 | + startButton.disabled = true; |
| 169 | + stopButton.disabled = false; |
| 170 | + }); |
| 171 | + |
| 172 | + stopButton.addEventListener('click', () => { |
| 173 | + mediaRecorder.stop(); |
| 174 | + startButton.disabled = false; |
| 175 | + stopButton.disabled = true; |
| 176 | + |
| 177 | + stopButton.classList.add("STE-hide-button"); |
| 178 | + startButton.classList.remove("STE-hide-button"); |
| 179 | + }); |
| 180 | +} |
0 commit comments