electron-cast 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.md +122 -12
  3. package/dist/cjs/client/auth/build-time.d.ts +7 -0
  4. package/dist/cjs/client/auth/build-time.js +11 -0
  5. package/dist/cjs/client/auth/build-time.js.map +1 -0
  6. package/dist/cjs/client/auth/cast-auth.d.ts +44 -0
  7. package/dist/cjs/client/auth/cast-auth.js +145 -0
  8. package/dist/cjs/client/auth/cast-auth.js.map +1 -0
  9. package/dist/cjs/client/auth/cast-cert-validator.d.ts +34 -0
  10. package/dist/cjs/client/auth/cast-cert-validator.js +89 -0
  11. package/dist/cjs/client/auth/cast-cert-validator.js.map +1 -0
  12. package/dist/cjs/client/auth/crl.d.ts +45 -0
  13. package/dist/cjs/client/auth/crl.js +227 -0
  14. package/dist/cjs/client/auth/crl.js.map +1 -0
  15. package/dist/cjs/client/auth/der.d.ts +62 -0
  16. package/dist/cjs/client/auth/der.js +208 -0
  17. package/dist/cjs/client/auth/der.js.map +1 -0
  18. package/dist/cjs/client/auth/names.d.ts +52 -0
  19. package/dist/cjs/client/auth/names.js +447 -0
  20. package/dist/cjs/client/auth/names.js.map +1 -0
  21. package/dist/cjs/client/auth/path-validator.d.ts +29 -0
  22. package/dist/cjs/client/auth/path-validator.js +421 -0
  23. package/dist/cjs/client/auth/path-validator.js.map +1 -0
  24. package/dist/cjs/client/auth/trust-data.d.ts +24 -0
  25. package/dist/cjs/client/auth/trust-data.js +97 -0
  26. package/dist/cjs/client/auth/trust-data.js.map +1 -0
  27. package/dist/cjs/client/auth/x509.d.ts +109 -0
  28. package/dist/cjs/client/auth/x509.js +442 -0
  29. package/dist/cjs/client/auth/x509.js.map +1 -0
  30. package/dist/cjs/client/cast-client.d.ts +111 -0
  31. package/dist/cjs/client/cast-client.js +505 -0
  32. package/dist/cjs/client/cast-client.js.map +1 -0
  33. package/dist/cjs/client/errors.d.ts +28 -0
  34. package/dist/cjs/client/errors.js +50 -0
  35. package/dist/cjs/client/errors.js.map +1 -0
  36. package/dist/cjs/client/index.d.ts +6 -0
  37. package/dist/cjs/client/index.js +21 -0
  38. package/dist/cjs/client/index.js.map +1 -0
  39. package/dist/cjs/client/namespaces.d.ts +9 -0
  40. package/dist/cjs/client/namespaces.js +13 -0
  41. package/dist/cjs/client/namespaces.js.map +1 -0
  42. package/dist/cjs/client/types.d.ts +27 -0
  43. package/dist/cjs/client/types.js +3 -0
  44. package/dist/cjs/client/types.js.map +1 -0
  45. package/dist/cjs/discovery/cast-discovery.d.ts +103 -0
  46. package/dist/cjs/discovery/cast-discovery.js +323 -0
  47. package/dist/cjs/discovery/cast-discovery.js.map +1 -0
  48. package/dist/cjs/discovery/device-cache.d.ts +45 -0
  49. package/dist/cjs/discovery/device-cache.js +240 -0
  50. package/dist/cjs/discovery/device-cache.js.map +1 -0
  51. package/dist/cjs/discovery/dns-packet.d.ts +70 -0
  52. package/dist/cjs/discovery/dns-packet.js +251 -0
  53. package/dist/cjs/discovery/dns-packet.js.map +1 -0
  54. package/dist/cjs/discovery/index.d.ts +4 -0
  55. package/dist/cjs/discovery/index.js +21 -0
  56. package/dist/cjs/discovery/index.js.map +1 -0
  57. package/dist/cjs/discovery/udp-socket.d.ts +16 -0
  58. package/dist/cjs/discovery/udp-socket.js +163 -0
  59. package/dist/cjs/discovery/udp-socket.js.map +1 -0
  60. package/dist/cjs/electron/main.d.ts +86 -0
  61. package/dist/cjs/electron/main.js +408 -0
  62. package/dist/cjs/electron/main.js.map +1 -0
  63. package/dist/cjs/electron/manager-api.d.ts +132 -0
  64. package/dist/cjs/electron/manager-api.js +207 -0
  65. package/dist/cjs/electron/manager-api.js.map +1 -0
  66. package/dist/cjs/electron/preload.d.ts +39 -0
  67. package/dist/cjs/electron/preload.js +96 -0
  68. package/dist/cjs/electron/preload.js.map +1 -0
  69. package/dist/cjs/electron/presentation-envelope.d.ts +60 -0
  70. package/dist/cjs/electron/presentation-envelope.js +318 -0
  71. package/dist/cjs/electron/presentation-envelope.js.map +1 -0
  72. package/dist/cjs/electron/presentation-handler.d.ts +100 -0
  73. package/dist/cjs/electron/presentation-handler.js +499 -0
  74. package/dist/cjs/electron/presentation-handler.js.map +1 -0
  75. package/dist/cjs/electron/session-service.d.ts +174 -0
  76. package/dist/cjs/electron/session-service.js +891 -0
  77. package/dist/cjs/electron/session-service.js.map +1 -0
  78. package/dist/cjs/index.d.ts +8 -0
  79. package/dist/cjs/index.js +26 -0
  80. package/dist/cjs/index.js.map +1 -0
  81. package/dist/cjs/package.json +1 -0
  82. package/dist/cjs/polyfill/bridge-types.d.ts +86 -0
  83. package/dist/cjs/polyfill/bridge-types.js +3 -0
  84. package/dist/cjs/polyfill/bridge-types.js.map +1 -0
  85. package/dist/cjs/polyfill/install.d.ts +12 -0
  86. package/dist/cjs/polyfill/install.js +1569 -0
  87. package/dist/cjs/polyfill/install.js.map +1 -0
  88. package/dist/cjs/polyfill/presentation-standin.d.ts +13 -0
  89. package/dist/cjs/polyfill/presentation-standin.js +279 -0
  90. package/dist/cjs/polyfill/presentation-standin.js.map +1 -0
  91. package/dist/cjs/protocol/cast-message.d.ts +20 -0
  92. package/dist/cjs/protocol/cast-message.js +171 -0
  93. package/dist/cjs/protocol/cast-message.js.map +1 -0
  94. package/dist/cjs/protocol/device-auth.d.ts +61 -0
  95. package/dist/cjs/protocol/device-auth.js +113 -0
  96. package/dist/cjs/protocol/device-auth.js.map +1 -0
  97. package/dist/cjs/protocol/errors.d.ts +4 -0
  98. package/dist/cjs/protocol/errors.js +12 -0
  99. package/dist/cjs/protocol/errors.js.map +1 -0
  100. package/dist/cjs/protocol/framing.d.ts +18 -0
  101. package/dist/cjs/protocol/framing.js +100 -0
  102. package/dist/cjs/protocol/framing.js.map +1 -0
  103. package/dist/cjs/protocol/index.d.ts +4 -0
  104. package/dist/cjs/protocol/index.js +21 -0
  105. package/dist/cjs/protocol/index.js.map +1 -0
  106. package/dist/cjs/protocol/proto-wire.d.ts +11 -0
  107. package/dist/cjs/protocol/proto-wire.js +64 -0
  108. package/dist/cjs/protocol/proto-wire.js.map +1 -0
  109. package/dist/client/auth/build-time.d.ts +7 -0
  110. package/dist/client/auth/build-time.js +8 -0
  111. package/dist/client/auth/build-time.js.map +1 -0
  112. package/dist/client/auth/cast-auth.d.ts +44 -0
  113. package/dist/client/auth/cast-auth.js +141 -0
  114. package/dist/client/auth/cast-auth.js.map +1 -0
  115. package/dist/client/auth/cast-cert-validator.d.ts +34 -0
  116. package/dist/client/auth/cast-cert-validator.js +86 -0
  117. package/dist/client/auth/cast-cert-validator.js.map +1 -0
  118. package/dist/client/auth/crl.d.ts +45 -0
  119. package/dist/client/auth/crl.js +221 -0
  120. package/dist/client/auth/crl.js.map +1 -0
  121. package/dist/client/auth/der.d.ts +62 -0
  122. package/dist/client/auth/der.js +196 -0
  123. package/dist/client/auth/der.js.map +1 -0
  124. package/dist/client/auth/names.d.ts +52 -0
  125. package/dist/client/auth/names.js +438 -0
  126. package/dist/client/auth/names.js.map +1 -0
  127. package/dist/client/auth/path-validator.d.ts +29 -0
  128. package/dist/client/auth/path-validator.js +417 -0
  129. package/dist/client/auth/path-validator.js.map +1 -0
  130. package/dist/client/auth/trust-data.d.ts +24 -0
  131. package/dist/client/auth/trust-data.js +94 -0
  132. package/dist/client/auth/trust-data.js.map +1 -0
  133. package/dist/client/auth/x509.d.ts +109 -0
  134. package/dist/client/auth/x509.js +430 -0
  135. package/dist/client/auth/x509.js.map +1 -0
  136. package/dist/client/cast-client.d.ts +20 -1
  137. package/dist/client/cast-client.js +74 -3
  138. package/dist/client/cast-client.js.map +1 -1
  139. package/dist/client/errors.d.ts +11 -0
  140. package/dist/client/errors.js +13 -0
  141. package/dist/client/errors.js.map +1 -1
  142. package/dist/client/index.d.ts +2 -0
  143. package/dist/discovery/cast-discovery.d.ts +21 -2
  144. package/dist/discovery/cast-discovery.js +94 -14
  145. package/dist/discovery/cast-discovery.js.map +1 -1
  146. package/dist/discovery/device-cache.d.ts +6 -1
  147. package/dist/discovery/device-cache.js +31 -7
  148. package/dist/discovery/device-cache.js.map +1 -1
  149. package/dist/discovery/udp-socket.d.ts +5 -1
  150. package/dist/discovery/udp-socket.js +56 -5
  151. package/dist/discovery/udp-socket.js.map +1 -1
  152. package/dist/electron/main.d.ts +26 -4
  153. package/dist/electron/main.js +193 -55
  154. package/dist/electron/main.js.map +1 -1
  155. package/dist/electron/manager-api.d.ts +132 -0
  156. package/dist/electron/manager-api.js +198 -0
  157. package/dist/electron/manager-api.js.map +1 -0
  158. package/dist/electron/preload.d.ts +5 -0
  159. package/dist/electron/preload.js +9 -1
  160. package/dist/electron/preload.js.map +1 -1
  161. package/dist/electron/presentation-envelope.js +7 -1
  162. package/dist/electron/presentation-envelope.js.map +1 -1
  163. package/dist/electron/presentation-handler.js +16 -9
  164. package/dist/electron/presentation-handler.js.map +1 -1
  165. package/dist/electron/session-service.d.ts +50 -1
  166. package/dist/electron/session-service.js +256 -33
  167. package/dist/electron/session-service.js.map +1 -1
  168. package/dist/polyfill/presentation-standin.js +3 -2
  169. package/dist/polyfill/presentation-standin.js.map +1 -1
  170. package/dist/protocol/device-auth.d.ts +61 -0
  171. package/dist/protocol/device-auth.js +106 -0
  172. package/dist/protocol/device-auth.js.map +1 -0
  173. package/dist/protocol/index.d.ts +1 -0
  174. package/dist/protocol/index.js +1 -0
  175. package/dist/protocol/index.js.map +1 -1
  176. package/dist/protocol/proto-wire.d.ts +11 -0
  177. package/dist/protocol/proto-wire.js +60 -0
  178. package/dist/protocol/proto-wire.js.map +1 -0
  179. package/package.json +47 -12
  180. package/src/client/auth/build-time.ts +7 -0
  181. package/src/client/auth/cast-auth.ts +184 -0
  182. package/src/client/auth/cast-cert-validator.ts +133 -0
  183. package/src/client/auth/crl.ts +228 -0
  184. package/src/client/auth/der.ts +193 -0
  185. package/src/client/auth/names.ts +411 -0
  186. package/src/client/auth/path-validator.ts +449 -0
  187. package/src/client/auth/trust-data.ts +95 -0
  188. package/src/client/auth/x509.ts +488 -0
  189. package/src/client/cast-client.ts +85 -3
  190. package/src/client/errors.ts +29 -0
  191. package/src/client/index.ts +2 -0
  192. package/src/discovery/cast-discovery.ts +104 -19
  193. package/src/discovery/device-cache.ts +38 -8
  194. package/src/discovery/udp-socket.ts +63 -4
  195. package/src/electron/main.ts +220 -60
  196. package/src/electron/manager-api.ts +312 -0
  197. package/src/electron/preload.ts +15 -1
  198. package/src/electron/presentation-envelope.ts +7 -1
  199. package/src/electron/presentation-handler.ts +16 -10
  200. package/src/electron/session-service.ts +265 -34
  201. package/src/polyfill/presentation-standin.ts +3 -2
  202. package/src/protocol/device-auth.ts +126 -0
  203. package/src/protocol/index.ts +1 -0
  204. package/src/protocol/proto-wire.ts +55 -0
@@ -0,0 +1,1569 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.installChromeCast = installChromeCast;
4
+ /**
5
+ * Installs the legacy Google Cast sender API (`chrome.cast`, version [1, 2]) on the page's global object,
6
+ * forwarding every operation to the electron-cast bridge that the preload script published at
7
+ * `globalThis[bridgeKey]`. Returns `true` when the polyfill was installed, `false` when the bridge is
8
+ * missing or `chrome.cast` already exists (in which case nothing is changed).
9
+ *
10
+ * Electron runs this function in the page's main world through `contextBridge.executeInMainWorld`, which
11
+ * serializes it to source; the body must therefore stay self-contained: no imports, no module-level
12
+ * helpers or state. Type annotations are stripped and leave no runtime references behind.
13
+ */
14
+ function installChromeCast(bridgeKey) {
15
+ const g = globalThis;
16
+ const rawBridge = g[bridgeKey];
17
+ if (!rawBridge || typeof rawBridge.call !== "function")
18
+ return false;
19
+ const bridge = rawBridge;
20
+ if (g.chrome && g.chrome.cast)
21
+ return false;
22
+ g.chrome ??= {};
23
+ const VERSION = [1, 2];
24
+ const AutoJoinPolicy = { TAB_AND_ORIGIN_SCOPED: "tab_and_origin_scoped", ORIGIN_SCOPED: "origin_scoped", PAGE_SCOPED: "page_scoped" };
25
+ const Capability = { VIDEO_OUT: "video_out", AUDIO_OUT: "audio_out", VIDEO_IN: "video_in", AUDIO_IN: "audio_in", MULTIZONE_GROUP: "multizone_group" };
26
+ const DefaultActionPolicy = { CREATE_SESSION: "create_session", CAST_THIS_TAB: "cast_this_tab" };
27
+ const DialAppState = { RUNNING: "running", STOPPED: "stopped", ERROR: "error" };
28
+ const ErrorCode = {
29
+ CANCEL: "cancel", TIMEOUT: "timeout", API_NOT_INITIALIZED: "api_not_initialized", INVALID_PARAMETER: "invalid_parameter",
30
+ EXTENSION_NOT_COMPATIBLE: "extension_not_compatible", EXTENSION_MISSING: "extension_missing",
31
+ RECEIVER_UNAVAILABLE: "receiver_unavailable", SESSION_ERROR: "session_error", CHANNEL_ERROR: "channel_error",
32
+ LOAD_MEDIA_FAILED: "load_media_failed",
33
+ };
34
+ const ReceiverAction = { CAST: "cast", STOP: "stop" };
35
+ const ReceiverAvailability = { AVAILABLE: "available", UNAVAILABLE: "unavailable" };
36
+ const ReceiverType = { CAST: "cast", DIAL: "dial", HANGOUT: "hangout", CUSTOM: "custom" };
37
+ const SenderPlatform = { CHROME: "chrome", IOS: "ios", ANDROID: "android" };
38
+ const SessionStatus = { CONNECTED: "connected", DISCONNECTED: "disconnected", STOPPED: "stopped" };
39
+ const VolumeControlType = { ATTENUATION: "attenuation", FIXED: "fixed", MASTER: "master" };
40
+ const MEDIA_NAMESPACE = "urn:x-cast:com.google.cast.media";
41
+ class Timeout {
42
+ requestSession = 60000;
43
+ leaveSession = 3000;
44
+ stopSession = 3000;
45
+ setReceiverVolume = 3000;
46
+ sendCustomMessage = 3000;
47
+ }
48
+ class ApiConfig {
49
+ sessionRequest;
50
+ sessionListener;
51
+ receiverListener;
52
+ autoJoinPolicy;
53
+ defaultActionPolicy;
54
+ additionalSessionRequests;
55
+ customDialLaunchCallback;
56
+ invisibleSender;
57
+ constructor(sessionRequest, sessionListener, receiverListener, autoJoinPolicy = "tab_and_origin_scoped", defaultActionPolicy = "create_session") {
58
+ this.sessionRequest = sessionRequest;
59
+ this.sessionListener = sessionListener;
60
+ this.receiverListener = receiverListener;
61
+ this.autoJoinPolicy = autoJoinPolicy;
62
+ this.defaultActionPolicy = defaultActionPolicy;
63
+ this.additionalSessionRequests = [];
64
+ this.customDialLaunchCallback = null;
65
+ this.invisibleSender = false;
66
+ }
67
+ }
68
+ class SessionRequest {
69
+ appId;
70
+ capabilities;
71
+ requestSessionTimeout;
72
+ language;
73
+ dialRequest;
74
+ androidReceiverCompatible;
75
+ credentialsData;
76
+ constructor(appId, capabilities = ["video_out", "audio_out"], timeout = 60000, androidReceiverCompatible = false, credentialsData = null) {
77
+ this.appId = appId;
78
+ this.capabilities = capabilities;
79
+ this.requestSessionTimeout = timeout;
80
+ this.language = null;
81
+ this.dialRequest = null;
82
+ this.androidReceiverCompatible = androidReceiverCompatible;
83
+ this.credentialsData = credentialsData;
84
+ }
85
+ }
86
+ class CastError {
87
+ code;
88
+ description;
89
+ details;
90
+ constructor(code, description = null, details = null) {
91
+ this.code = code;
92
+ this.description = description;
93
+ this.details = details;
94
+ }
95
+ }
96
+ class Image {
97
+ url;
98
+ height;
99
+ width;
100
+ constructor(url) {
101
+ this.url = url;
102
+ this.height = null;
103
+ this.width = null;
104
+ }
105
+ }
106
+ class Receiver {
107
+ label;
108
+ friendlyName;
109
+ capabilities;
110
+ volume;
111
+ receiverType;
112
+ isActiveInput;
113
+ displayStatus;
114
+ constructor(label, friendlyName, capabilities = [], volume = null) {
115
+ this.label = label;
116
+ this.friendlyName = friendlyName;
117
+ this.capabilities = capabilities;
118
+ this.volume = volume;
119
+ this.receiverType = "cast";
120
+ this.isActiveInput = null;
121
+ this.displayStatus = null;
122
+ }
123
+ }
124
+ class ReceiverDisplayStatus {
125
+ statusText;
126
+ appImages;
127
+ constructor(statusText, appImages) {
128
+ this.statusText = statusText;
129
+ this.appImages = appImages;
130
+ }
131
+ }
132
+ class SenderApplication {
133
+ platform;
134
+ url;
135
+ packageId;
136
+ constructor(platform) {
137
+ this.platform = platform;
138
+ this.url = null;
139
+ this.packageId = null;
140
+ }
141
+ }
142
+ class Volume {
143
+ level;
144
+ muted;
145
+ constructor(level = null, muted = null) {
146
+ this.level = level;
147
+ this.muted = muted;
148
+ }
149
+ }
150
+ class DialRequest {
151
+ launchUrl;
152
+ extraParameters;
153
+ constructor(launchUrl, extraParameters = {}) {
154
+ this.launchUrl = launchUrl;
155
+ this.extraParameters = extraParameters;
156
+ }
157
+ }
158
+ class CredentialsData {
159
+ credentials;
160
+ constructor(credentials) {
161
+ this.credentials = credentials;
162
+ }
163
+ }
164
+ const PlayerState = { IDLE: "IDLE", PLAYING: "PLAYING", PAUSED: "PAUSED", BUFFERING: "BUFFERING" };
165
+ const MediaCommand = { PAUSE: "pause", SEEK: "seek", STREAM_VOLUME: "stream_volume", STREAM_MUTE: "stream_mute" };
166
+ const MetadataType = { GENERIC: 0, MOVIE: 1, TV_SHOW: 2, MUSIC_TRACK: 3, PHOTO: 4 };
167
+ const RepeatMode = { OFF: "REPEAT_OFF", ALL: "REPEAT_ALL", SINGLE: "REPEAT_SINGLE", ALL_AND_SHUFFLE: "REPEAT_ALL_AND_SHUFFLE" };
168
+ const ResumeState = { PLAYBACK_START: "PLAYBACK_START", PLAYBACK_PAUSE: "PLAYBACK_PAUSE" };
169
+ const StreamType = { BUFFERED: "BUFFERED", LIVE: "LIVE", OTHER: "OTHER" };
170
+ const IdleReason = { CANCELLED: "CANCELLED", INTERRUPTED: "INTERRUPTED", FINISHED: "FINISHED", ERROR: "ERROR" };
171
+ const TrackType = { TEXT: "TEXT", AUDIO: "AUDIO", VIDEO: "VIDEO" };
172
+ const TextTrackType = { SUBTITLES: "SUBTITLES", CAPTIONS: "CAPTIONS", DESCRIPTIONS: "DESCRIPTIONS", CHAPTERS: "CHAPTERS", METADATA: "METADATA" };
173
+ const TextTrackEdgeType = { NONE: "NONE", OUTLINE: "OUTLINE", DROP_SHADOW: "DROP_SHADOW", RAISED: "RAISED", DEPRESSED: "DEPRESSED" };
174
+ const TextTrackWindowType = { NONE: "NONE", NORMAL: "NORMAL", ROUNDED_CORNERS: "ROUNDED_CORNERS" };
175
+ const TextTrackFontGenericFamily = { SANS_SERIF: "SANS_SERIF", MONOSPACED_SANS_SERIF: "MONOSPACED_SANS_SERIF", SERIF: "SERIF", MONOSPACED_SERIF: "MONOSPACED_SERIF", CASUAL: "CASUAL", CURSIVE: "CURSIVE", SMALL_CAPITALS: "SMALL_CAPITALS" };
176
+ const TextTrackFontStyle = { NORMAL: "NORMAL", BOLD: "BOLD", BOLD_ITALIC: "BOLD_ITALIC", ITALIC: "ITALIC" };
177
+ class MediaInfo {
178
+ contentId;
179
+ streamType;
180
+ contentType;
181
+ metadata;
182
+ duration;
183
+ tracks;
184
+ textTrackStyle;
185
+ customData;
186
+ constructor(contentId, contentType) {
187
+ this.contentId = contentId;
188
+ this.streamType = "BUFFERED";
189
+ this.contentType = contentType;
190
+ this.metadata = null;
191
+ this.duration = null;
192
+ this.tracks = null;
193
+ this.textTrackStyle = null;
194
+ this.customData = null;
195
+ }
196
+ }
197
+ class LoadRequest {
198
+ type;
199
+ requestId;
200
+ sessionId;
201
+ media;
202
+ activeTrackIds;
203
+ autoplay;
204
+ currentTime;
205
+ customData;
206
+ constructor(media) {
207
+ this.type = "LOAD";
208
+ this.requestId = 0;
209
+ this.sessionId = null;
210
+ this.media = media;
211
+ this.activeTrackIds = null;
212
+ this.autoplay = true;
213
+ this.currentTime = null;
214
+ this.customData = null;
215
+ }
216
+ }
217
+ class QueueItem {
218
+ itemId;
219
+ media;
220
+ autoplay;
221
+ startTime;
222
+ playbackDuration;
223
+ preloadTime;
224
+ activeTrackIds;
225
+ customData;
226
+ constructor(media) {
227
+ this.itemId = null;
228
+ this.media = media;
229
+ this.autoplay = true;
230
+ this.startTime = 0;
231
+ this.playbackDuration = null;
232
+ this.preloadTime = 0;
233
+ this.activeTrackIds = null;
234
+ this.customData = null;
235
+ }
236
+ }
237
+ class QueueLoadRequest {
238
+ type;
239
+ requestId;
240
+ sessionId;
241
+ items;
242
+ startIndex;
243
+ repeatMode;
244
+ customData;
245
+ constructor(items) {
246
+ this.type = "QUEUE_LOAD";
247
+ this.requestId = null;
248
+ this.sessionId = null;
249
+ this.items = items;
250
+ this.startIndex = 0;
251
+ this.repeatMode = "REPEAT_OFF";
252
+ this.customData = null;
253
+ }
254
+ }
255
+ class QueueInsertItemsRequest {
256
+ type;
257
+ requestId;
258
+ sessionId;
259
+ items;
260
+ insertBefore;
261
+ customData;
262
+ constructor(items) {
263
+ this.type = "QUEUE_INSERT";
264
+ this.requestId = null;
265
+ this.sessionId = null;
266
+ this.items = items;
267
+ this.insertBefore = null;
268
+ this.customData = null;
269
+ }
270
+ }
271
+ class QueueRemoveItemsRequest {
272
+ type;
273
+ requestId;
274
+ sessionId;
275
+ itemIds;
276
+ customData;
277
+ constructor(itemIds) {
278
+ this.type = "QUEUE_REMOVE";
279
+ this.requestId = null;
280
+ this.sessionId = null;
281
+ this.itemIds = itemIds;
282
+ this.customData = null;
283
+ }
284
+ }
285
+ class QueueReorderItemsRequest {
286
+ type;
287
+ requestId;
288
+ sessionId;
289
+ itemIds;
290
+ insertBefore;
291
+ customData;
292
+ constructor(itemIds) {
293
+ this.type = "QUEUE_REORDER";
294
+ this.requestId = null;
295
+ this.sessionId = null;
296
+ this.itemIds = itemIds;
297
+ this.insertBefore = null;
298
+ this.customData = null;
299
+ }
300
+ }
301
+ class QueueUpdateItemsRequest {
302
+ type;
303
+ requestId;
304
+ sessionId;
305
+ items;
306
+ customData;
307
+ constructor(items) {
308
+ this.type = "QUEUE_UPDATE";
309
+ this.requestId = null;
310
+ this.sessionId = null;
311
+ this.items = items;
312
+ this.customData = null;
313
+ }
314
+ }
315
+ class QueueSetPropertiesRequest {
316
+ type;
317
+ requestId;
318
+ sessionId;
319
+ repeatMode;
320
+ customData;
321
+ constructor() {
322
+ this.type = "QUEUE_UPDATE";
323
+ this.requestId = null;
324
+ this.sessionId = null;
325
+ this.repeatMode = null;
326
+ this.customData = null;
327
+ }
328
+ }
329
+ class QueueJumpRequest {
330
+ type;
331
+ requestId;
332
+ sessionId;
333
+ currentItemId;
334
+ jump;
335
+ customData;
336
+ constructor() {
337
+ this.type = "QUEUE_UPDATE";
338
+ this.requestId = null;
339
+ this.sessionId = null;
340
+ this.currentItemId = null;
341
+ this.jump = null;
342
+ this.customData = null;
343
+ }
344
+ }
345
+ class SeekRequest {
346
+ currentTime;
347
+ resumeState;
348
+ customData;
349
+ constructor() {
350
+ this.currentTime = null;
351
+ this.resumeState = null;
352
+ this.customData = null;
353
+ }
354
+ }
355
+ class VolumeRequest {
356
+ volume;
357
+ customData;
358
+ constructor(volume) {
359
+ this.volume = volume;
360
+ this.customData = null;
361
+ }
362
+ }
363
+ class PlayRequest {
364
+ customData;
365
+ constructor() {
366
+ this.customData = null;
367
+ }
368
+ }
369
+ class PauseRequest {
370
+ customData;
371
+ constructor() {
372
+ this.customData = null;
373
+ }
374
+ }
375
+ class StopRequest {
376
+ customData;
377
+ constructor() {
378
+ this.customData = null;
379
+ }
380
+ }
381
+ class GetStatusRequest {
382
+ customData;
383
+ constructor() {
384
+ this.customData = null;
385
+ }
386
+ }
387
+ class EditTracksInfoRequest {
388
+ requestId;
389
+ activeTrackIds;
390
+ textTrackStyle;
391
+ constructor(activeTrackIds, textTrackStyle) {
392
+ this.requestId = 0;
393
+ this.activeTrackIds = activeTrackIds ?? null;
394
+ this.textTrackStyle = textTrackStyle ?? null;
395
+ }
396
+ }
397
+ class Track {
398
+ trackId;
399
+ trackContentId;
400
+ trackContentType;
401
+ type;
402
+ name;
403
+ language;
404
+ subtype;
405
+ customData;
406
+ constructor(trackId, type) {
407
+ this.trackId = trackId;
408
+ this.trackContentId = null;
409
+ this.trackContentType = null;
410
+ this.type = type;
411
+ this.name = null;
412
+ this.language = null;
413
+ this.subtype = null;
414
+ this.customData = null;
415
+ }
416
+ }
417
+ class TextTrackStyle {
418
+ foregroundColor;
419
+ backgroundColor;
420
+ edgeType;
421
+ edgeColor;
422
+ windowType;
423
+ windowColor;
424
+ windowRoundedCornerRadius;
425
+ fontScale;
426
+ fontFamily;
427
+ fontGenericFamily;
428
+ fontStyle;
429
+ customData;
430
+ constructor() {
431
+ this.foregroundColor = null;
432
+ this.backgroundColor = null;
433
+ this.edgeType = null;
434
+ this.edgeColor = null;
435
+ this.windowType = null;
436
+ this.windowColor = null;
437
+ this.windowRoundedCornerRadius = null;
438
+ this.fontScale = null;
439
+ this.fontFamily = null;
440
+ this.fontGenericFamily = null;
441
+ this.fontStyle = null;
442
+ this.customData = null;
443
+ }
444
+ }
445
+ class GenericMediaMetadata {
446
+ type;
447
+ metadataType;
448
+ title;
449
+ subtitle;
450
+ images;
451
+ releaseYear;
452
+ releaseDate;
453
+ constructor() {
454
+ this.type = 0;
455
+ this.metadataType = 0;
456
+ this.title = null;
457
+ this.subtitle = null;
458
+ this.images = null;
459
+ this.releaseYear = null;
460
+ this.releaseDate = null;
461
+ }
462
+ }
463
+ class MovieMediaMetadata {
464
+ type;
465
+ metadataType;
466
+ title;
467
+ studio;
468
+ subtitle;
469
+ images;
470
+ releaseYear;
471
+ releaseDate;
472
+ constructor() {
473
+ this.type = 1;
474
+ this.metadataType = 1;
475
+ this.title = null;
476
+ this.studio = null;
477
+ this.subtitle = null;
478
+ this.images = null;
479
+ this.releaseYear = null;
480
+ this.releaseDate = null;
481
+ }
482
+ }
483
+ class TvShowMediaMetadata {
484
+ type;
485
+ metadataType;
486
+ seriesTitle;
487
+ title;
488
+ episodeTitle;
489
+ seasonNumber;
490
+ season;
491
+ episodeNumber;
492
+ episode;
493
+ images;
494
+ releaseYear;
495
+ originalAirdate;
496
+ constructor() {
497
+ this.type = 2;
498
+ this.metadataType = 2;
499
+ this.seriesTitle = null;
500
+ this.title = null;
501
+ this.episodeTitle = null;
502
+ this.seasonNumber = null;
503
+ this.season = null;
504
+ this.episodeNumber = null;
505
+ this.episode = null;
506
+ this.images = null;
507
+ this.releaseYear = null;
508
+ this.originalAirdate = null;
509
+ }
510
+ }
511
+ class MusicTrackMediaMetadata {
512
+ type;
513
+ metadataType;
514
+ albumName;
515
+ title;
516
+ albumArtist;
517
+ artist;
518
+ composer;
519
+ songName;
520
+ artistName;
521
+ trackNumber;
522
+ discNumber;
523
+ images;
524
+ releaseYear;
525
+ releaseDate;
526
+ constructor() {
527
+ this.type = 3;
528
+ this.metadataType = 3;
529
+ this.albumName = null;
530
+ this.title = null;
531
+ this.albumArtist = null;
532
+ this.artist = null;
533
+ this.composer = null;
534
+ this.songName = null;
535
+ this.artistName = null;
536
+ this.trackNumber = null;
537
+ this.discNumber = null;
538
+ this.images = null;
539
+ this.releaseYear = null;
540
+ this.releaseDate = null;
541
+ }
542
+ }
543
+ class PhotoMediaMetadata {
544
+ type;
545
+ metadataType;
546
+ title;
547
+ artist;
548
+ location;
549
+ images;
550
+ latitude;
551
+ longitude;
552
+ width;
553
+ height;
554
+ creationDateTime;
555
+ constructor() {
556
+ this.type = 4;
557
+ this.metadataType = 4;
558
+ this.title = null;
559
+ this.artist = null;
560
+ this.location = null;
561
+ this.images = null;
562
+ this.latitude = null;
563
+ this.longitude = null;
564
+ this.width = null;
565
+ this.height = null;
566
+ this.creationDateTime = null;
567
+ }
568
+ }
569
+ class Media {
570
+ sessionId;
571
+ mediaSessionId;
572
+ media;
573
+ playbackRate;
574
+ playerState;
575
+ currentTime;
576
+ supportedMediaCommands;
577
+ volume;
578
+ idleReason;
579
+ activeTrackIds;
580
+ customData;
581
+ currentItemId;
582
+ loadingItemId;
583
+ preloadedItemId;
584
+ items;
585
+ repeatMode;
586
+ liveSeekableRange;
587
+ breakStatus;
588
+ videoInfo;
589
+ queueData;
590
+ _updateListeners;
591
+ _lastUpdate;
592
+ _announced;
593
+ constructor(sessionId, mediaSessionId) {
594
+ this.sessionId = sessionId;
595
+ this.mediaSessionId = mediaSessionId;
596
+ this.media = null;
597
+ this.playbackRate = 1;
598
+ this.playerState = "IDLE";
599
+ this.currentTime = 0;
600
+ this.supportedMediaCommands = [];
601
+ this.volume = new Volume();
602
+ this.idleReason = null;
603
+ this.activeTrackIds = null;
604
+ this.customData = null;
605
+ this.currentItemId = null;
606
+ this.loadingItemId = null;
607
+ this.preloadedItemId = null;
608
+ this.items = null;
609
+ this.repeatMode = "REPEAT_OFF";
610
+ this.liveSeekableRange = null;
611
+ this.breakStatus = null;
612
+ this.videoInfo = null;
613
+ this.queueData = null;
614
+ this._updateListeners = [];
615
+ this._lastUpdate = 0;
616
+ this._announced = false;
617
+ }
618
+ addUpdateListener(fn) {
619
+ if (typeof fn === "function" && !this._updateListeners.includes(fn))
620
+ this._updateListeners.push(fn);
621
+ }
622
+ removeUpdateListener(fn) {
623
+ const i = this._updateListeners.indexOf(fn);
624
+ if (i >= 0)
625
+ this._updateListeners.splice(i, 1);
626
+ }
627
+ supportsCommand(cmd) {
628
+ return this.supportedMediaCommands.includes(cmd);
629
+ }
630
+ getEstimatedTime() {
631
+ if (this.playerState === "PLAYING") {
632
+ let t = this.currentTime + this.playbackRate * (Date.now() - this._lastUpdate) / 1000;
633
+ const duration = this.media && this.media.duration;
634
+ if (typeof duration === "number")
635
+ t = Math.min(t, duration);
636
+ return t;
637
+ }
638
+ return this.currentTime;
639
+ }
640
+ getEstimatedBreakTime() {
641
+ const base = this.breakStatus && this.breakStatus.currentBreakTime;
642
+ if (typeof base !== "number")
643
+ return undefined;
644
+ return base + this._estimateElapsed();
645
+ }
646
+ getEstimatedBreakClipTime() {
647
+ const base = this.breakStatus && this.breakStatus.currentBreakClipTime;
648
+ if (typeof base !== "number")
649
+ return undefined;
650
+ return base + this._estimateElapsed();
651
+ }
652
+ getEstimatedLiveSeekableRange() {
653
+ const r = this.liveSeekableRange;
654
+ if (r == null)
655
+ return undefined;
656
+ const elapsed = this._estimateElapsed();
657
+ const out = {
658
+ start: r.start,
659
+ end: r.end,
660
+ isMovingWindow: r.isMovingWindow,
661
+ isLiveDone: r.isLiveDone,
662
+ };
663
+ if (r.isLiveDone !== true) {
664
+ if (typeof out.end === "number")
665
+ out.end += elapsed;
666
+ if (r.isMovingWindow === true && typeof out.start === "number")
667
+ out.start += elapsed;
668
+ }
669
+ return out;
670
+ }
671
+ /** Seconds advanced since the last status update: playbackRate * elapsed while PLAYING, else 0. */
672
+ _estimateElapsed() {
673
+ if (this.playerState !== "PLAYING")
674
+ return 0;
675
+ return this.playbackRate * (Date.now() - this._lastUpdate) / 1000;
676
+ }
677
+ /** Send a command on the media namespace; resolves with no arguments on the matching MEDIA_STATUS. */
678
+ sendCommand(type, extra, success, error) {
679
+ const sess = sessions.get(this.sessionId);
680
+ if (!sess || sess.status !== "connected") {
681
+ fail(error, "session_error");
682
+ return;
683
+ }
684
+ const requestId = nextRequestId();
685
+ const message = stripNulls({ type, requestId, mediaSessionId: this.mediaSessionId, ...extra });
686
+ sendMediaRequest(sess, requestId, message, MEDIA_TIMEOUT_KEYS[type] ?? "getStatus", success, error);
687
+ }
688
+ getStatus(req, success, error) {
689
+ this.sendCommand("GET_STATUS", { customData: req?.customData ?? null }, success, error);
690
+ }
691
+ play(req, success, error) {
692
+ this.sendCommand("PLAY", { customData: req?.customData ?? null }, success, error);
693
+ }
694
+ pause(req, success, error) {
695
+ this.sendCommand("PAUSE", { customData: req?.customData ?? null }, success, error);
696
+ }
697
+ seek(seekRequest, success, error) {
698
+ this.sendCommand("SEEK", {
699
+ currentTime: seekRequest?.currentTime ?? null,
700
+ resumeState: seekRequest?.resumeState ?? null,
701
+ customData: seekRequest?.customData ?? null,
702
+ }, success, error);
703
+ }
704
+ stop(req, success, error) {
705
+ this.sendCommand("STOP", { customData: req?.customData ?? null }, success, error);
706
+ }
707
+ setVolume(volumeRequest, success, error) {
708
+ const vol = volumeRequest?.volume;
709
+ this.sendCommand("SET_VOLUME", {
710
+ volume: vol ? { level: vol.level ?? null, muted: vol.muted ?? null } : null,
711
+ customData: volumeRequest?.customData ?? null,
712
+ }, success, error);
713
+ }
714
+ editTracksInfo(req, success, error) {
715
+ this.sendCommand("EDIT_TRACKS_INFO", {
716
+ activeTrackIds: req?.activeTrackIds ?? null,
717
+ textTrackStyle: req?.textTrackStyle ?? null,
718
+ }, success, error);
719
+ }
720
+ queueInsertItems(req, success, error) {
721
+ this.sendCommand("QUEUE_INSERT", {
722
+ items: req?.items ?? null,
723
+ insertBefore: req?.insertBefore ?? null,
724
+ customData: req?.customData ?? null,
725
+ }, success, error);
726
+ }
727
+ queueAppendItem(item, success, error) {
728
+ this.sendCommand("QUEUE_INSERT", { items: [item] }, success, error);
729
+ }
730
+ queueUpdateItems(req, success, error) {
731
+ this.sendCommand("QUEUE_UPDATE", {
732
+ items: req?.items ?? null,
733
+ customData: req?.customData ?? null,
734
+ }, success, error);
735
+ }
736
+ queueJumpToItem(itemId, success, error) {
737
+ this.sendCommand("QUEUE_UPDATE", { currentItemId: itemId }, success, error);
738
+ }
739
+ queueNext(success, error) {
740
+ this.sendCommand("QUEUE_UPDATE", { jump: 1 }, success, error);
741
+ }
742
+ queuePrev(success, error) {
743
+ this.sendCommand("QUEUE_UPDATE", { jump: -1 }, success, error);
744
+ }
745
+ queueSetRepeatMode(mode, success, error) {
746
+ this.sendCommand("QUEUE_UPDATE", { repeatMode: mode }, success, error);
747
+ }
748
+ queueRemoveItems(req, success, error) {
749
+ this.sendCommand("QUEUE_REMOVE", {
750
+ itemIds: req?.itemIds ?? null,
751
+ customData: req?.customData ?? null,
752
+ }, success, error);
753
+ }
754
+ queueRemoveItem(itemId, success, error) {
755
+ this.sendCommand("QUEUE_REMOVE", { itemIds: [itemId] }, success, error);
756
+ }
757
+ queueReorderItems(req, success, error) {
758
+ this.sendCommand("QUEUE_REORDER", {
759
+ itemIds: req?.itemIds ?? null,
760
+ insertBefore: req?.insertBefore ?? null,
761
+ customData: req?.customData ?? null,
762
+ }, success, error);
763
+ }
764
+ queueMoveItemToNewIndex(itemId, newIndex, success, error) {
765
+ const items = Array.isArray(this.items) ? this.items : null;
766
+ if (!items || !items.some((i) => i && i.itemId === itemId) || !Number.isInteger(newIndex) || newIndex < 0) {
767
+ fail(error, "invalid_parameter");
768
+ return;
769
+ }
770
+ const remaining = items.map((i) => i.itemId).filter((id) => id !== itemId);
771
+ const extra = newIndex >= remaining.length
772
+ ? { itemIds: [itemId] }
773
+ : { itemIds: [itemId], insertBefore: remaining[newIndex] };
774
+ this.sendCommand("QUEUE_REORDER", extra, success, error);
775
+ }
776
+ }
777
+ const media = {
778
+ DEFAULT_MEDIA_RECEIVER_APP_ID: "CC1AD845",
779
+ PlayerState,
780
+ MediaCommand,
781
+ MetadataType,
782
+ RepeatMode,
783
+ ResumeState,
784
+ StreamType,
785
+ IdleReason,
786
+ TrackType,
787
+ TextTrackType,
788
+ TextTrackEdgeType,
789
+ TextTrackWindowType,
790
+ TextTrackFontGenericFamily,
791
+ TextTrackFontStyle,
792
+ timeout: { load: 15000, getStatus: 15000, play: 15000, pause: 15000, seek: 15000, stop: 15000, setVolume: 15000, editTracksInfo: 15000, queue: 15000 },
793
+ MediaInfo,
794
+ LoadRequest,
795
+ QueueItem,
796
+ QueueLoadRequest,
797
+ QueueInsertItemsRequest,
798
+ QueueRemoveItemsRequest,
799
+ QueueReorderItemsRequest,
800
+ QueueUpdateItemsRequest,
801
+ QueueSetPropertiesRequest,
802
+ QueueJumpRequest,
803
+ SeekRequest,
804
+ VolumeRequest,
805
+ PlayRequest,
806
+ PauseRequest,
807
+ StopRequest,
808
+ GetStatusRequest,
809
+ EditTracksInfoRequest,
810
+ Track,
811
+ TextTrackStyle,
812
+ GenericMediaMetadata,
813
+ MovieMediaMetadata,
814
+ TvShowMediaMetadata,
815
+ MusicTrackMediaMetadata,
816
+ PhotoMediaMetadata,
817
+ Media,
818
+ };
819
+ const MEDIA_TIMEOUT_KEYS = {
820
+ LOAD: "load",
821
+ GET_STATUS: "getStatus",
822
+ PLAY: "play",
823
+ PAUSE: "pause",
824
+ SEEK: "seek",
825
+ STOP: "stop",
826
+ SET_VOLUME: "setVolume",
827
+ EDIT_TRACKS_INFO: "editTracksInfo",
828
+ QUEUE_LOAD: "queue",
829
+ QUEUE_INSERT: "queue",
830
+ QUEUE_REMOVE: "queue",
831
+ QUEUE_REORDER: "queue",
832
+ QUEUE_UPDATE: "queue",
833
+ };
834
+ function safe(fn, ...args) {
835
+ if (typeof fn !== "function")
836
+ return;
837
+ try {
838
+ fn(...args);
839
+ }
840
+ catch (err) {
841
+ console.error(err);
842
+ }
843
+ }
844
+ function fail(error, code, description) {
845
+ setTimeout(() => safe(error, new CastError(code, description ?? null)), 0);
846
+ }
847
+ const REQ_ID_MAX = 2 ** 30;
848
+ let requestIdCounter = Math.floor(Math.random() * (REQ_ID_MAX - 1)) + 1;
849
+ /** Positive integer requestIds, unique within the page, starting at a random value in [1, 2^30). */
850
+ function nextRequestId() {
851
+ const id = requestIdCounter;
852
+ requestIdCounter += 1;
853
+ if (requestIdCounter >= REQ_ID_MAX)
854
+ requestIdCounter = 1;
855
+ return id;
856
+ }
857
+ /** A timer that does not keep a Node test process alive; plain in the browser. */
858
+ function later(ms, fn) {
859
+ const t = setTimeout(fn, ms);
860
+ if (t && typeof t.unref === "function")
861
+ t.unref();
862
+ return t;
863
+ }
864
+ /** Remove every key whose value is null or undefined, recursively through objects and arrays. */
865
+ function stripNulls(value) {
866
+ if (Array.isArray(value))
867
+ return value.map(stripNulls).filter((v) => v !== null && v !== undefined);
868
+ if (value && typeof value === "object") {
869
+ const out = {};
870
+ for (const key of Object.keys(value)) {
871
+ const v = value[key];
872
+ if (v === null || v === undefined)
873
+ continue;
874
+ out[key] = stripNulls(v);
875
+ }
876
+ return out;
877
+ }
878
+ return value;
879
+ }
880
+ function settlePendingMedia(sess, requestId, outcome) {
881
+ const p = sess._pendingMedia.get(requestId);
882
+ if (!p)
883
+ return;
884
+ sess._pendingMedia.delete(requestId);
885
+ if (p.timer)
886
+ clearTimeout(p.timer);
887
+ outcome();
888
+ }
889
+ /** Send one media-namespace message and track it until its reply (or timeout) arrives. */
890
+ function sendMediaRequest(sess, requestId, message, timeoutKey, success, error) {
891
+ const pending = { kind: message.type, success, error, timer: null };
892
+ const timeoutMs = media.timeout[timeoutKey];
893
+ if (typeof timeoutMs === "number" && timeoutMs > 0) {
894
+ pending.timer = later(timeoutMs, () => settlePendingMedia(sess, requestId, () => fail(error, "timeout")));
895
+ }
896
+ sess._pendingMedia.set(requestId, pending);
897
+ bridge.call("sendMessage", { sessionId: sess.sessionId, namespace: MEDIA_NAMESPACE, message: JSON.stringify(message) })
898
+ .then((res) => {
899
+ if (!res.ok)
900
+ settlePendingMedia(sess, requestId, () => fail(error, res.code, res.description));
901
+ })
902
+ .catch((e) => settlePendingMedia(sess, requestId, () => fail(error, "session_error", String(e))));
903
+ }
904
+ /** Kill every Media of the session (update listeners get false) without touching pending requests. */
905
+ function killAllMedia(sess) {
906
+ for (const m of [...sess.media]) {
907
+ for (const fn of [...m._updateListeners])
908
+ safe(fn, false);
909
+ }
910
+ sess.media = [];
911
+ }
912
+ /** The session is gone: kill its media and fail every pending media request with session_error. */
913
+ function killSessionMedia(sess) {
914
+ killAllMedia(sess);
915
+ for (const p of [...sess._pendingMedia.values()]) {
916
+ if (p.timer)
917
+ clearTimeout(p.timer);
918
+ fail(p.error, "session_error");
919
+ }
920
+ sess._pendingMedia.clear();
921
+ }
922
+ function applyMediaStatus(sess, msg) {
923
+ const entries = Array.isArray(msg.status) ? msg.status : msg.status && typeof msg.status === "object" ? [msg.status] : [];
924
+ if (entries.length === 0) {
925
+ killAllMedia(sess);
926
+ return;
927
+ }
928
+ const pending = typeof msg.requestId === "number" ? sess._pendingMedia.get(msg.requestId) : undefined;
929
+ const suppressAnnounce = !!pending && (pending.kind === "LOAD" || pending.kind === "QUEUE_LOAD");
930
+ for (const e of entries) {
931
+ if (!e || typeof e !== "object" || e.mediaSessionId == null)
932
+ continue;
933
+ let m = sess.media.find((x) => x.mediaSessionId === e.mediaSessionId);
934
+ const isNew = !m;
935
+ if (isNew) {
936
+ m = new Media(sess.sessionId, e.mediaSessionId);
937
+ sess.media.push(m);
938
+ }
939
+ if (e.media != null)
940
+ m.media = e.media;
941
+ if (e.playbackRate != null)
942
+ m.playbackRate = e.playbackRate;
943
+ if (e.playerState != null)
944
+ m.playerState = e.playerState;
945
+ if (e.currentTime != null)
946
+ m.currentTime = e.currentTime;
947
+ if (e.activeTrackIds != null)
948
+ m.activeTrackIds = e.activeTrackIds;
949
+ if (e.customData != null)
950
+ m.customData = e.customData;
951
+ if (e.currentItemId != null)
952
+ m.currentItemId = e.currentItemId;
953
+ if (e.loadingItemId != null)
954
+ m.loadingItemId = e.loadingItemId;
955
+ if (e.preloadedItemId != null)
956
+ m.preloadedItemId = e.preloadedItemId;
957
+ if (e.items != null)
958
+ m.items = e.items;
959
+ if (e.repeatMode != null)
960
+ m.repeatMode = e.repeatMode;
961
+ // A MEDIA_STATUS entry is the receiver's full status except that unchanged fields may be left out:
962
+ // an ad break that ended, or a stream that is no longer live, drops the field, so these default to null.
963
+ m.liveSeekableRange = e.liveSeekableRange ?? null;
964
+ m.breakStatus = e.breakStatus ?? null;
965
+ if (e.videoInfo != null)
966
+ m.videoInfo = e.videoInfo;
967
+ if (e.queueData != null)
968
+ m.queueData = e.queueData;
969
+ m.idleReason = e.idleReason ?? null;
970
+ if (e.volume != null)
971
+ m.volume = new Volume(e.volume.level ?? null, e.volume.muted ?? null);
972
+ if (e.supportedMediaCommands != null) {
973
+ const bits = [MediaCommand.PAUSE, MediaCommand.SEEK, MediaCommand.STREAM_VOLUME, MediaCommand.STREAM_MUTE];
974
+ m.supportedMediaCommands = bits.filter((_, i) => (e.supportedMediaCommands & (1 << i)) !== 0);
975
+ }
976
+ m._lastUpdate = Date.now();
977
+ const alive = !(m.playerState === PlayerState.IDLE && m.idleReason !== null);
978
+ for (const fn of [...m._updateListeners])
979
+ safe(fn, alive);
980
+ if (!alive) {
981
+ const i = sess.media.indexOf(m);
982
+ if (i >= 0)
983
+ sess.media.splice(i, 1);
984
+ }
985
+ else if (isNew && !m._announced) {
986
+ m._announced = true;
987
+ if (!suppressAnnounce)
988
+ for (const fn of [...sess._mediaListeners])
989
+ safe(fn, m);
990
+ }
991
+ }
992
+ }
993
+ /** Resolve a pending media request from a reply on the media namespace. */
994
+ function resolveMediaReply(sess, msg) {
995
+ if (!msg || typeof msg !== "object" || typeof msg.requestId !== "number")
996
+ return;
997
+ const p = sess._pendingMedia.get(msg.requestId);
998
+ if (!p)
999
+ return;
1000
+ switch (msg.type) {
1001
+ case "MEDIA_STATUS": {
1002
+ if (p.kind === "LOAD" || p.kind === "QUEUE_LOAD") {
1003
+ // Some receivers (a Nest Hub) answer a LOAD with an interim MEDIA_STATUS whose entry still
1004
+ // reports extendedStatus.playerState "LOADING"; keep waiting for the final status.
1005
+ const entries = Array.isArray(msg.status) ? msg.status : msg.status && typeof msg.status === "object" ? [msg.status] : [];
1006
+ const interim = entries.some((e) => e && e.extendedStatus && e.extendedStatus.playerState === "LOADING");
1007
+ if (interim)
1008
+ return;
1009
+ const first = Array.isArray(msg.status) ? msg.status[0] : msg.status;
1010
+ const m = first && sess.media.find((x) => x.mediaSessionId === first.mediaSessionId);
1011
+ settlePendingMedia(sess, msg.requestId, () => (m ? safe(p.success, m) : fail(p.error, "load_media_failed")));
1012
+ }
1013
+ else {
1014
+ settlePendingMedia(sess, msg.requestId, () => safe(p.success));
1015
+ }
1016
+ return;
1017
+ }
1018
+ case "LOAD_FAILED":
1019
+ settlePendingMedia(sess, msg.requestId, () => fail(p.error, "load_media_failed"));
1020
+ return;
1021
+ case "LOAD_CANCELLED":
1022
+ settlePendingMedia(sess, msg.requestId, () => fail(p.error, "cancel"));
1023
+ return;
1024
+ case "INVALID_REQUEST":
1025
+ case "INVALID_PLAYER_STATE":
1026
+ settlePendingMedia(sess, msg.requestId, () => fail(p.error, "session_error", msg.reason));
1027
+ return;
1028
+ }
1029
+ }
1030
+ function handleMediaMessage(sess, raw) {
1031
+ let msg;
1032
+ try {
1033
+ msg = typeof raw === "string" ? JSON.parse(raw) : raw;
1034
+ }
1035
+ catch {
1036
+ return;
1037
+ }
1038
+ if (!msg || typeof msg !== "object")
1039
+ return;
1040
+ if (msg.type === "MEDIA_STATUS")
1041
+ applyMediaStatus(sess, msg);
1042
+ resolveMediaReply(sess, msg);
1043
+ }
1044
+ function toImage(img) {
1045
+ const out = new Image(img.url);
1046
+ if (img.height != null)
1047
+ out.height = img.height;
1048
+ if (img.width != null)
1049
+ out.width = img.width;
1050
+ return out;
1051
+ }
1052
+ function toVolume(vol) {
1053
+ const out = new Volume(vol.level ?? null, vol.muted ?? null);
1054
+ if (vol.controlType != null)
1055
+ out.controlType = vol.controlType;
1056
+ if (vol.stepInterval != null)
1057
+ out.stepInterval = vol.stepInterval;
1058
+ return out;
1059
+ }
1060
+ function toReceiver(rcv) {
1061
+ const out = new Receiver(rcv.label, rcv.friendlyName, Array.isArray(rcv.capabilities) ? [...rcv.capabilities] : [], rcv.volume ? toVolume(rcv.volume) : null);
1062
+ if (rcv.isActiveInput != null)
1063
+ out.isActiveInput = rcv.isActiveInput;
1064
+ if (rcv.displayStatus) {
1065
+ out.displayStatus = new ReceiverDisplayStatus(rcv.displayStatus.statusText, (rcv.displayStatus.appImages ?? []).map(toImage));
1066
+ }
1067
+ return out;
1068
+ }
1069
+ function applySnapshot(sess, snap) {
1070
+ sess.appId = snap.appId;
1071
+ sess.displayName = snap.displayName;
1072
+ sess.appImages = (snap.appImages ?? []).map(toImage);
1073
+ sess.statusText = snap.statusText ?? null;
1074
+ sess.namespaces = (snap.namespaces ?? []).map((n) => ({ name: n.name }));
1075
+ sess.transportId = snap.transportId;
1076
+ sess.receiver = toReceiver(snap.receiver);
1077
+ if (Array.isArray(snap.senderApps)) {
1078
+ sess.senderApps = snap.senderApps.map((a) => {
1079
+ const sa = new SenderApplication(a.platform);
1080
+ if (a.url != null)
1081
+ sa.url = a.url;
1082
+ if (a.packageId != null)
1083
+ sa.packageId = a.packageId;
1084
+ return sa;
1085
+ });
1086
+ }
1087
+ }
1088
+ class Session {
1089
+ sessionId;
1090
+ appId;
1091
+ displayName;
1092
+ appImages;
1093
+ receiver;
1094
+ senderApps;
1095
+ namespaces;
1096
+ media;
1097
+ status;
1098
+ statusText;
1099
+ transportId;
1100
+ updateListeners;
1101
+ messageListeners;
1102
+ _mediaListeners;
1103
+ _pendingMedia;
1104
+ constructor(sessionId, appId, displayName, appImages, receiver) {
1105
+ this.sessionId = sessionId;
1106
+ this.appId = appId;
1107
+ this.displayName = displayName;
1108
+ this.appImages = appImages;
1109
+ this.receiver = receiver;
1110
+ this.senderApps = [];
1111
+ this.namespaces = [];
1112
+ this.media = [];
1113
+ this.status = "connected";
1114
+ this.statusText = null;
1115
+ this.transportId = sessionId;
1116
+ this.updateListeners = [];
1117
+ this.messageListeners = new Map();
1118
+ this._mediaListeners = [];
1119
+ this._pendingMedia = new Map();
1120
+ }
1121
+ sendMessage(namespace, message, success, error) {
1122
+ const payload = typeof message === "string" ? message : JSON.stringify(message);
1123
+ bridge.call("sendMessage", { sessionId: this.sessionId, namespace, message: payload }).then((res) => {
1124
+ if (res.ok) {
1125
+ safe(success);
1126
+ return;
1127
+ }
1128
+ fail(error, res.code, res.description);
1129
+ }).catch((e) => fail(error, "session_error", String(e)));
1130
+ }
1131
+ addMessageListener(namespace, fn) {
1132
+ if (typeof fn !== "function")
1133
+ return;
1134
+ let list = this.messageListeners.get(namespace);
1135
+ if (!list) {
1136
+ list = [];
1137
+ this.messageListeners.set(namespace, list);
1138
+ }
1139
+ if (!list.includes(fn))
1140
+ list.push(fn);
1141
+ }
1142
+ removeMessageListener(namespace, fn) {
1143
+ const list = this.messageListeners.get(namespace);
1144
+ if (!list)
1145
+ return;
1146
+ const i = list.indexOf(fn);
1147
+ if (i >= 0)
1148
+ list.splice(i, 1);
1149
+ }
1150
+ addUpdateListener(fn) {
1151
+ if (typeof fn === "function" && !this.updateListeners.includes(fn))
1152
+ this.updateListeners.push(fn);
1153
+ }
1154
+ removeUpdateListener(fn) {
1155
+ const i = this.updateListeners.indexOf(fn);
1156
+ if (i >= 0)
1157
+ this.updateListeners.splice(i, 1);
1158
+ }
1159
+ addMediaListener(fn) {
1160
+ if (typeof fn === "function" && !this._mediaListeners.includes(fn))
1161
+ this._mediaListeners.push(fn);
1162
+ }
1163
+ removeMediaListener(fn) {
1164
+ const i = this._mediaListeners.indexOf(fn);
1165
+ if (i >= 0)
1166
+ this._mediaListeners.splice(i, 1);
1167
+ }
1168
+ stop(success, error) {
1169
+ bridge.call("stopSession", { sessionId: this.sessionId }).then((res) => {
1170
+ if (!res.ok) {
1171
+ fail(error, res.code, res.description);
1172
+ return;
1173
+ }
1174
+ endSession(this, "stopped");
1175
+ safe(success);
1176
+ }).catch((e) => fail(error, "session_error", String(e)));
1177
+ }
1178
+ leave(success, error) {
1179
+ bridge.call("leaveSession", { sessionId: this.sessionId }).then((res) => {
1180
+ if (!res.ok) {
1181
+ fail(error, res.code, res.description);
1182
+ return;
1183
+ }
1184
+ endSession(this, "disconnected");
1185
+ safe(success);
1186
+ }).catch((e) => fail(error, "session_error", String(e)));
1187
+ }
1188
+ setReceiverVolumeLevel(level, success, error) {
1189
+ if (typeof level !== "number" || Number.isNaN(level) || level < 0 || level > 1) {
1190
+ fail(error, "invalid_parameter");
1191
+ return;
1192
+ }
1193
+ bridge.call("setReceiverVolume", { sessionId: this.sessionId, level }).then((res) => {
1194
+ if (res.ok) {
1195
+ safe(success);
1196
+ return;
1197
+ }
1198
+ fail(error, res.code, res.description);
1199
+ }).catch((e) => fail(error, "session_error", String(e)));
1200
+ }
1201
+ setReceiverMuted(muted, success, error) {
1202
+ bridge.call("setReceiverMuted", { sessionId: this.sessionId, muted }).then((res) => {
1203
+ if (res.ok) {
1204
+ safe(success);
1205
+ return;
1206
+ }
1207
+ fail(error, res.code, res.description);
1208
+ }).catch((e) => fail(error, "session_error", String(e)));
1209
+ }
1210
+ loadMedia(req, success, error) {
1211
+ if (!req || typeof req !== "object" || !req.media) {
1212
+ fail(error, "invalid_parameter");
1213
+ return;
1214
+ }
1215
+ if (this.status !== "connected") {
1216
+ fail(error, "session_error");
1217
+ return;
1218
+ }
1219
+ const requestId = nextRequestId();
1220
+ const message = stripNulls({
1221
+ type: "LOAD",
1222
+ requestId,
1223
+ sessionId: this.sessionId,
1224
+ media: req.media,
1225
+ autoplay: req.autoplay ?? true,
1226
+ currentTime: req.currentTime ?? null,
1227
+ activeTrackIds: req.activeTrackIds ?? null,
1228
+ customData: req.customData ?? null,
1229
+ });
1230
+ sendMediaRequest(this, requestId, message, "load", success, error);
1231
+ }
1232
+ queueLoad(req, success, error) {
1233
+ if (!req || typeof req !== "object" || !Array.isArray(req.items)) {
1234
+ fail(error, "invalid_parameter");
1235
+ return;
1236
+ }
1237
+ if (this.status !== "connected") {
1238
+ fail(error, "session_error");
1239
+ return;
1240
+ }
1241
+ const requestId = nextRequestId();
1242
+ const message = stripNulls({
1243
+ type: "QUEUE_LOAD",
1244
+ requestId,
1245
+ sessionId: this.sessionId,
1246
+ items: req.items,
1247
+ startIndex: req.startIndex ?? 0,
1248
+ repeatMode: req.repeatMode ?? "REPEAT_OFF",
1249
+ customData: req.customData ?? null,
1250
+ });
1251
+ sendMediaRequest(this, requestId, message, "queue", success, error);
1252
+ }
1253
+ }
1254
+ const sessions = new Map();
1255
+ /** End a session: notify, then drop it from the session map so later events for it are ignored. */
1256
+ function endSession(sess, status) {
1257
+ sess.status = status;
1258
+ killSessionMedia(sess);
1259
+ notifyUpdate(sess, false);
1260
+ sessions.delete(sess.sessionId);
1261
+ }
1262
+ /**
1263
+ * Report a session to a sessionListener function at most once: remember, per listener function, the
1264
+ * session ids already announced to it, so a second initialize (or a requestSessionById) does not
1265
+ * re-announce a session that function already got.
1266
+ */
1267
+ const announcedTo = new WeakMap();
1268
+ function announce(sess, listener) {
1269
+ if (!sess || typeof listener !== "function")
1270
+ return;
1271
+ let set = announcedTo.get(listener);
1272
+ if (!set) {
1273
+ set = new Set();
1274
+ announcedTo.set(listener, set);
1275
+ }
1276
+ if (set.has(sess.sessionId))
1277
+ return;
1278
+ set.add(sess.sessionId);
1279
+ safe(listener, sess);
1280
+ }
1281
+ function upsertSession(snap) {
1282
+ let sess = sessions.get(snap.sessionId);
1283
+ const created = !sess;
1284
+ if (created) {
1285
+ sess = new Session(snap.sessionId, snap.appId, snap.displayName, [], toReceiver(snap.receiver));
1286
+ sessions.set(snap.sessionId, sess);
1287
+ }
1288
+ applySnapshot(sess, snap);
1289
+ if (created && sess.namespaces.some((n) => n.name === MEDIA_NAMESPACE)) {
1290
+ // Discover media the receiver already has; its failure is ignored.
1291
+ const requestId = nextRequestId();
1292
+ sendMediaRequest(sess, requestId, { type: "GET_STATUS", requestId }, "getStatus", () => { }, () => { });
1293
+ }
1294
+ return sess;
1295
+ }
1296
+ let initialized = false;
1297
+ let apiConfig = null;
1298
+ let pendingRequest = false;
1299
+ let lastAvailability = null;
1300
+ const receiverActionListeners = [];
1301
+ function notifyUpdate(sess, alive) {
1302
+ for (const fn of [...sess.updateListeners])
1303
+ safe(fn, alive);
1304
+ }
1305
+ bridge.onEvent((event) => {
1306
+ switch (event.type) {
1307
+ case "receiverAvailability": {
1308
+ const value = event.available ? "available" : "unavailable";
1309
+ if (value === lastAvailability)
1310
+ return;
1311
+ lastAvailability = value;
1312
+ if (apiConfig)
1313
+ safe(apiConfig.receiverListener, value);
1314
+ return;
1315
+ }
1316
+ case "sessionUpdated": {
1317
+ const sess = sessions.get(event.session.sessionId);
1318
+ if (!sess)
1319
+ return;
1320
+ applySnapshot(sess, event.session);
1321
+ notifyUpdate(sess, true);
1322
+ return;
1323
+ }
1324
+ case "sessionEnded": {
1325
+ const sess = sessions.get(event.sessionId);
1326
+ if (!sess)
1327
+ return;
1328
+ endSession(sess, event.status);
1329
+ return;
1330
+ }
1331
+ case "receiverAction": {
1332
+ // A live session only: unknown ids and sessions that already ended are ignored.
1333
+ const sess = sessions.get(event.sessionId);
1334
+ if (!sess)
1335
+ return;
1336
+ for (const fn of [...receiverActionListeners])
1337
+ safe(fn, sess.receiver, ReceiverAction.STOP);
1338
+ return;
1339
+ }
1340
+ case "message": {
1341
+ const sess = sessions.get(event.sessionId);
1342
+ if (!sess)
1343
+ return;
1344
+ const fns = sess.messageListeners.get(event.namespace) ?? [];
1345
+ for (const fn of [...fns])
1346
+ safe(fn, event.namespace, event.message);
1347
+ if (event.namespace === MEDIA_NAMESPACE)
1348
+ handleMediaMessage(sess, event.message);
1349
+ return;
1350
+ }
1351
+ }
1352
+ });
1353
+ function initialize(apiCfg, success, error) {
1354
+ const req = apiCfg && apiCfg.sessionRequest;
1355
+ const valid = !!req && typeof req === "object" && typeof req.appId === "string"
1356
+ && typeof apiCfg.sessionListener === "function" && typeof apiCfg.receiverListener === "function";
1357
+ if (!valid) {
1358
+ fail(error, "invalid_parameter");
1359
+ return;
1360
+ }
1361
+ bridge.call("initialize", {
1362
+ appId: req.appId,
1363
+ capabilities: Array.isArray(req.capabilities) ? req.capabilities : [],
1364
+ autoJoinPolicy: apiCfg.autoJoinPolicy ?? "tab_and_origin_scoped",
1365
+ }).then((res) => {
1366
+ if (!res.ok) {
1367
+ fail(error, res.code, res.description);
1368
+ return;
1369
+ }
1370
+ const value = res.value ?? {};
1371
+ initialized = true;
1372
+ apiConfig = apiCfg;
1373
+ const availability = value.available ? "available" : "unavailable";
1374
+ lastAvailability = availability;
1375
+ safe(success);
1376
+ safe(apiCfg.receiverListener, availability);
1377
+ for (const snap of value.sessions ?? [])
1378
+ announce(upsertSession(snap), apiCfg.sessionListener);
1379
+ }).catch((e) => fail(error, "session_error", String(e)));
1380
+ }
1381
+ function requestSession(success, error, optSessionRequest) {
1382
+ if (!initialized) {
1383
+ fail(error, "api_not_initialized");
1384
+ return;
1385
+ }
1386
+ if (pendingRequest) {
1387
+ fail(error, "invalid_parameter");
1388
+ return;
1389
+ }
1390
+ const req = optSessionRequest ?? apiConfig.sessionRequest;
1391
+ pendingRequest = true;
1392
+ bridge.call("requestSession", {
1393
+ appId: req.appId,
1394
+ capabilities: Array.isArray(req.capabilities) ? req.capabilities : [],
1395
+ timeoutMs: req.requestSessionTimeout ?? 60000,
1396
+ }).then((res) => {
1397
+ pendingRequest = false;
1398
+ if (!res.ok) {
1399
+ fail(error, res.code, res.description);
1400
+ return;
1401
+ }
1402
+ const sess = upsertSession(res.value);
1403
+ for (const fn of [...receiverActionListeners])
1404
+ safe(fn, sess.receiver, ReceiverAction.CAST);
1405
+ safe(success, sess);
1406
+ }).catch((e) => {
1407
+ pendingRequest = false;
1408
+ fail(error, "session_error", String(e));
1409
+ });
1410
+ }
1411
+ function requestSessionById(sessionId) {
1412
+ bridge.call("joinSession", { sessionId }).then((res) => {
1413
+ if (!res.ok)
1414
+ return;
1415
+ const sess = upsertSession(res.value);
1416
+ if (apiConfig)
1417
+ announce(sess, apiConfig.sessionListener);
1418
+ }).catch((e) => console.error(e));
1419
+ }
1420
+ function addReceiverActionListener(fn) {
1421
+ if (typeof fn === "function" && !receiverActionListeners.includes(fn))
1422
+ receiverActionListeners.push(fn);
1423
+ }
1424
+ function removeReceiverActionListener(fn) {
1425
+ const i = receiverActionListeners.indexOf(fn);
1426
+ if (i >= 0)
1427
+ receiverActionListeners.splice(i, 1);
1428
+ }
1429
+ function setCustomReceivers(_receivers, success, _error) {
1430
+ setTimeout(() => safe(success), 0);
1431
+ }
1432
+ function setReceiverDisplayStatus(_receiver, success, _error) {
1433
+ setTimeout(() => safe(success), 0);
1434
+ }
1435
+ function logMessage(_msg) { }
1436
+ function precache(_data) { }
1437
+ function unescape(s) {
1438
+ try {
1439
+ return decodeURIComponent(s);
1440
+ }
1441
+ catch {
1442
+ return s;
1443
+ }
1444
+ }
1445
+ g.chrome.cast = {
1446
+ VERSION,
1447
+ isAvailable: true,
1448
+ AutoJoinPolicy,
1449
+ Capability,
1450
+ DefaultActionPolicy,
1451
+ DialAppState,
1452
+ ErrorCode,
1453
+ ReceiverAction,
1454
+ ReceiverAvailability,
1455
+ ReceiverType,
1456
+ SenderPlatform,
1457
+ SessionStatus,
1458
+ VolumeControlType,
1459
+ timeout: new Timeout(),
1460
+ media,
1461
+ ApiConfig,
1462
+ SessionRequest,
1463
+ Error: CastError,
1464
+ Image,
1465
+ Receiver,
1466
+ ReceiverDisplayStatus,
1467
+ SenderApplication,
1468
+ Volume,
1469
+ Timeout,
1470
+ DialRequest,
1471
+ CredentialsData,
1472
+ Session,
1473
+ initialize,
1474
+ requestSession,
1475
+ requestSessionById,
1476
+ addReceiverActionListener,
1477
+ removeReceiverActionListener,
1478
+ setCustomReceivers,
1479
+ setReceiverDisplayStatus,
1480
+ logMessage,
1481
+ precache,
1482
+ unescape,
1483
+ };
1484
+ // Google's cast_sender.js loader (it takes the desktop-Chrome path because Electron exposes
1485
+ // navigator.presentation) injects its own chrome.cast implementations from gstatic/eureka/clank and
1486
+ // from the two Cast extensions. The first one that loads would overwrite this polyfill with an
1487
+ // implementation on the Presentation API, which finds no devices in Electron, so those scripts are
1488
+ // blocked before they can load. A blocked element ends up with no src, so it never fires load or
1489
+ // error and the loader's chain simply stops there.
1490
+ const blockedClankRe = /^(https?:)?\/\/www\.gstatic\.com\/eureka\/clank\/(\d+\/)?cast_sender\.js/i;
1491
+ const blockedExtensionRe = /^chrome-extension:\/\/(pkedcjkdefgpdelpbcmbmeomcjbeemfm|enhhojjnijigcajfphajepfemndkmdlo)\//i;
1492
+ const isBlockedSrc = (value) => {
1493
+ let url;
1494
+ try {
1495
+ url = String(value);
1496
+ }
1497
+ catch {
1498
+ return false;
1499
+ }
1500
+ return blockedClankRe.test(url) || blockedExtensionRe.test(url);
1501
+ };
1502
+ if (typeof HTMLScriptElement === "function") {
1503
+ let proto = HTMLScriptElement.prototype;
1504
+ let srcDesc = proto ? Object.getOwnPropertyDescriptor(proto, "src") : undefined;
1505
+ while (!srcDesc && proto) {
1506
+ proto = Object.getPrototypeOf(proto);
1507
+ if (proto)
1508
+ srcDesc = Object.getOwnPropertyDescriptor(proto, "src");
1509
+ }
1510
+ if (srcDesc && typeof srcDesc.set === "function") {
1511
+ const originalSetSrc = srcDesc.set;
1512
+ Object.defineProperty(HTMLScriptElement.prototype, "src", {
1513
+ configurable: srcDesc.configurable === true,
1514
+ enumerable: srcDesc.enumerable === true,
1515
+ get: srcDesc.get,
1516
+ set(v) {
1517
+ if (isBlockedSrc(v))
1518
+ return;
1519
+ originalSetSrc.call(this, v);
1520
+ },
1521
+ });
1522
+ }
1523
+ }
1524
+ if (typeof Element === "function" && typeof Element.prototype.setAttribute === "function") {
1525
+ const originalSetAttribute = Element.prototype.setAttribute;
1526
+ Element.prototype.setAttribute = function (name, value) {
1527
+ if (typeof HTMLScriptElement === "function"
1528
+ && this instanceof HTMLScriptElement
1529
+ && typeof name === "string"
1530
+ && name.toLowerCase() === "src"
1531
+ && isBlockedSrc(value)) {
1532
+ return undefined;
1533
+ }
1534
+ return originalSetAttribute.call(this, name, value);
1535
+ };
1536
+ }
1537
+ // While the document is still loading, a page assigns its __onGCastApiAvailable callback and then the
1538
+ // parser loads cast_sender.js; calling the callback on a timer would run it before the page's scripts
1539
+ // (and cast.framework) exist. So until DOMContentLoaded only store the callback, then call whatever is
1540
+ // stored at that moment. With Google's loader that is its counter wrapper, which stands in for the
1541
+ // second report-in and fires the page's callback once the framework has loaded as well.
1542
+ const docIsLoading = () => typeof document !== "undefined" && document.readyState === "loading";
1543
+ let onGCastApiAvailable = g.__onGCastApiAvailable;
1544
+ const notifiedCallbacks = new WeakSet();
1545
+ const notifyGCastApiAvailable = (fn) => {
1546
+ if (typeof fn !== "function" || notifiedCallbacks.has(fn))
1547
+ return;
1548
+ notifiedCallbacks.add(fn);
1549
+ setTimeout(() => safe(fn, true), 0);
1550
+ };
1551
+ if (docIsLoading() && typeof document.addEventListener === "function") {
1552
+ document.addEventListener("DOMContentLoaded", () => notifyGCastApiAvailable(onGCastApiAvailable));
1553
+ }
1554
+ else {
1555
+ notifyGCastApiAvailable(onGCastApiAvailable);
1556
+ }
1557
+ Object.defineProperty(g, "__onGCastApiAvailable", {
1558
+ configurable: true,
1559
+ enumerable: true,
1560
+ get: () => onGCastApiAvailable,
1561
+ set: (v) => {
1562
+ onGCastApiAvailable = v;
1563
+ if (typeof v === "function" && !docIsLoading())
1564
+ notifyGCastApiAvailable(v);
1565
+ },
1566
+ });
1567
+ return true;
1568
+ }
1569
+ //# sourceMappingURL=install.js.map