@frameset/plex-player 1.0.5 → 2.0.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.
@@ -1,903 +0,0 @@
1
- /*!
2
- * @frameset/plex-player v1.0.5
3
- * Professional video player with VAST ads, Chromecast, PiP, subtitles, playlists and more. Built by FRAMESET Studio.
4
- * (c) 2026 FRAMESET Studio
5
- * Released under the MIT License
6
- * https://frameset.dev/plex-player
7
- */
8
- 'use strict';
9
-
10
- Object.defineProperty(exports, '__esModule', { value: true });
11
-
12
- /**
13
- * @frameset/plex-player - Core Entry Point
14
- * Professional Video Player by FRAMESET Studio
15
- * https://frameset.dev
16
- */
17
-
18
- /**
19
- * Utility Functions
20
- */
21
- const Utils = {
22
- formatTime(seconds) {
23
- if (isNaN(seconds) || !isFinite(seconds)) return '0:00';
24
- seconds = Math.max(0, Math.floor(seconds));
25
- const hours = Math.floor(seconds / 3600);
26
- const minutes = Math.floor(seconds % 3600 / 60);
27
- const secs = seconds % 60;
28
- if (hours > 0) {
29
- return `${hours}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
30
- }
31
- return `${minutes}:${secs.toString().padStart(2, '0')}`;
32
- },
33
- clamp(num, min, max) {
34
- return Math.min(Math.max(num, min), max);
35
- },
36
- throttle(fn, delay) {
37
- let lastCall = 0;
38
- return function (...args) {
39
- const now = Date.now();
40
- if (now - lastCall >= delay) {
41
- lastCall = now;
42
- return fn.apply(this, args);
43
- }
44
- };
45
- },
46
- debounce(fn, delay) {
47
- let timeoutId;
48
- return function (...args) {
49
- clearTimeout(timeoutId);
50
- timeoutId = setTimeout(() => fn.apply(this, args), delay);
51
- };
52
- }
53
- };
54
-
55
- /**
56
- * PlexPlayer - Main Player Class
57
- * A complete, standalone video player
58
- */
59
- class PlexPlayer {
60
- constructor(options = {}) {
61
- // Validate container
62
- if (!options.container) {
63
- throw new Error('PlexPlayer: container option is required');
64
- }
65
-
66
- // Get container element
67
- this.container = typeof options.container === 'string' ? document.querySelector(options.container) : options.container;
68
- if (!this.container) {
69
- throw new Error('PlexPlayer: container element not found');
70
- }
71
-
72
- // Store options with defaults
73
- this.options = {
74
- autoplay: false,
75
- muted: false,
76
- loop: false,
77
- volume: 1,
78
- poster: '',
79
- preload: 'metadata',
80
- keyboard: true,
81
- touch: true,
82
- pip: true,
83
- cast: true,
84
- fullscreen: true,
85
- controlsHideDelay: 3000,
86
- theme: {},
87
- subtitles: {},
88
- ads: {
89
- enabled: false
90
- },
91
- i18n: {},
92
- ...options
93
- };
94
-
95
- // Internationalization with defaults
96
- this.i18n = {
97
- play: 'Play',
98
- pause: 'Pause',
99
- mute: 'Mute',
100
- unmute: 'Unmute',
101
- fullscreen: 'Fullscreen',
102
- exitFullscreen: 'Exit Fullscreen',
103
- pip: 'Picture-in-Picture',
104
- exitPip: 'Exit Picture-in-Picture',
105
- settings: 'Settings',
106
- speed: 'Speed',
107
- quality: 'Quality',
108
- subtitles: 'Subtitles',
109
- off: 'Off',
110
- normal: 'Normal',
111
- captions: 'Captions',
112
- audio: 'Audio',
113
- volume: 'Volume',
114
- seekForward: 'Seek Forward',
115
- seekBackward: 'Seek Backward',
116
- ...this.options.i18n
117
- };
118
-
119
- // Internal state
120
- this._eventListeners = new Map();
121
- this._state = {
122
- playing: false,
123
- paused: true,
124
- muted: this.options.muted,
125
- volume: this.options.volume,
126
- currentTime: 0,
127
- duration: 0,
128
- buffered: 0,
129
- fullscreen: false,
130
- pip: false,
131
- playbackRate: 1
132
- };
133
-
134
- // Playlist state
135
- this._playlist = [];
136
- this._currentIndex = 0;
137
-
138
- // Controls timeout
139
- this._controlsTimeout = null;
140
-
141
- // Initialize player
142
- this._init();
143
- }
144
- _init() {
145
- // Add player class
146
- this.container.classList.add('plex-player');
147
- // Show controls initially
148
- this.container.classList.add('plex-controls-visible');
149
-
150
- // Apply theme
151
- this._applyTheme();
152
-
153
- // Create DOM structure
154
- this._createDOM();
155
-
156
- // Bind events
157
- this._bindEvents();
158
-
159
- // Initialize features
160
- if (this.options.keyboard) this._initKeyboard();
161
- if (this.options.touch) this._initTouch();
162
- if (this.options.pip) this._initPiP();
163
- if (this.options.cast) this._initCast();
164
-
165
- // Emit ready event
166
- setTimeout(() => this._emit('ready'), 0);
167
- }
168
- _applyTheme() {
169
- const {
170
- theme
171
- } = this.options;
172
- if (theme.primary) this.container.style.setProperty('--plex-primary', theme.primary);
173
- if (theme.background) this.container.style.setProperty('--plex-bg', theme.background);
174
- if (theme.text) this.container.style.setProperty('--plex-text', theme.text);
175
- if (theme.borderRadius) this.container.style.setProperty('--plex-border-radius', theme.borderRadius);
176
- }
177
- _createDOM() {
178
- // Video element
179
- this.video = document.createElement('video');
180
- this.video.className = 'plex-video';
181
- this.video.setAttribute('playsinline', '');
182
- this.video.setAttribute('webkit-playsinline', '');
183
- this.video.preload = this.options.preload;
184
- this.video.muted = this.options.muted;
185
- this.video.volume = this.options.volume;
186
- this.video.loop = this.options.loop;
187
- if (this.options.poster) this.video.poster = this.options.poster;
188
- this.container.appendChild(this.video);
189
-
190
- // Controls overlay
191
- this.controls = document.createElement('div');
192
- this.controls.className = 'plex-controls';
193
- this.controls.innerHTML = this._getControlsHTML();
194
- this.container.appendChild(this.controls);
195
-
196
- // Cache control elements
197
- this._cacheElements();
198
-
199
- // Big play button
200
- this.bigPlayBtn = document.createElement('div');
201
- this.bigPlayBtn.className = 'plex-big-play';
202
- this.bigPlayBtn.innerHTML = `<svg viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>`;
203
- this.container.appendChild(this.bigPlayBtn);
204
-
205
- // Loading spinner
206
- this.loader = document.createElement('div');
207
- this.loader.className = 'plex-loader';
208
- this.loader.innerHTML = '<div class="plex-spinner"></div>';
209
- this.container.appendChild(this.loader);
210
- }
211
- _getControlsHTML() {
212
- const i18n = this.i18n;
213
- return `
214
- <div class="plex-progress-container">
215
- <div class="plex-progress-bar">
216
- <div class="plex-progress-buffered"></div>
217
- <div class="plex-progress-played"></div>
218
- <div class="plex-progress-handle"></div>
219
- </div>
220
- <div class="plex-progress-preview">
221
- <div class="plex-preview-time">0:00</div>
222
- </div>
223
- </div>
224
- <div class="plex-controls-bar">
225
- <div class="plex-controls-left">
226
- <button class="plex-btn plex-play-btn" title="${i18n.play || 'Play'}">
227
- <svg class="plex-icon-play" viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>
228
- <svg class="plex-icon-pause" viewBox="0 0 24 24"><path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/></svg>
229
- </button>
230
- <button class="plex-btn plex-prev-btn" title="${i18n.previous || 'Previous'}">
231
- <svg viewBox="0 0 24 24"><path d="M6 6h2v12H6zm3.5 6l8.5 6V6z"/></svg>
232
- </button>
233
- <button class="plex-btn plex-next-btn" title="${i18n.next || 'Next'}">
234
- <svg viewBox="0 0 24 24"><path d="M6 18l8.5-6L6 6v12zM16 6v12h2V6h-2z"/></svg>
235
- </button>
236
- <div class="plex-volume-container">
237
- <button class="plex-btn plex-volume-btn" title="${i18n.mute || 'Mute'}">
238
- <svg class="plex-icon-volume-high" viewBox="0 0 24 24"><path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z"/></svg>
239
- <svg class="plex-icon-volume-low" viewBox="0 0 24 24"><path d="M18.5 12c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM5 9v6h4l5 5V4L9 9H5z"/></svg>
240
- <svg class="plex-icon-volume-mute" viewBox="0 0 24 24"><path d="M16.5 12c0-1.77-1.02-3.29-2.5-4.03v2.21l2.45 2.45c.03-.2.05-.41.05-.63zm2.5 0c0 .94-.2 1.82-.54 2.64l1.51 1.51C20.63 14.91 21 13.5 21 12c0-4.28-2.99-7.86-7-8.77v2.06c2.89.86 5 3.54 5 6.71zM4.27 3L3 4.27 7.73 9H3v6h4l5 5v-6.73l4.25 4.25c-.67.52-1.42.93-2.25 1.18v2.06c1.38-.31 2.63-.95 3.69-1.81L19.73 21 21 19.73l-9-9L4.27 3zM12 4L9.91 6.09 12 8.18V4z"/></svg>
241
- </button>
242
- <div class="plex-volume-slider">
243
- <div class="plex-volume-track">
244
- <div class="plex-volume-level"></div>
245
- <div class="plex-volume-handle"></div>
246
- </div>
247
- </div>
248
- </div>
249
- <div class="plex-time">
250
- <span class="plex-time-current">0:00</span>
251
- <span class="plex-time-separator">/</span>
252
- <span class="plex-time-duration">0:00</span>
253
- </div>
254
- </div>
255
- <div class="plex-controls-right">
256
- <button class="plex-btn plex-settings-btn" title="${i18n.settings || 'Settings'}">
257
- <svg viewBox="0 0 24 24"><path d="M19.14,12.94c0.04-0.3,0.06-0.61,0.06-0.94c0-0.32-0.02-0.64-0.07-0.94l2.03-1.58c0.18-0.14,0.23-0.41,0.12-0.61 l-1.92-3.32c-0.12-0.22-0.37-0.29-0.59-0.22l-2.39,0.96c-0.5-0.38-1.03-0.7-1.62-0.94L14.4,2.81c-0.04-0.24-0.24-0.41-0.48-0.41 h-3.84c-0.24,0-0.43,0.17-0.47,0.41L9.25,5.35C8.66,5.59,8.12,5.92,7.63,6.29L5.24,5.33c-0.22-0.08-0.47,0-0.59,0.22L2.74,8.87 C2.62,9.08,2.66,9.34,2.86,9.48l2.03,1.58C4.84,11.36,4.8,11.69,4.8,12s0.02,0.64,0.07,0.94l-2.03,1.58 c-0.18,0.14-0.23,0.41-0.12,0.61l1.92,3.32c0.12,0.22,0.37,0.29,0.59,0.22l2.39-0.96c0.5,0.38,1.03,0.7,1.62,0.94l0.36,2.54 c0.05,0.24,0.24,0.41,0.48,0.41h3.84c0.24,0,0.44-0.17,0.47-0.41l0.36-2.54c0.59-0.24,1.13-0.56,1.62-0.94l2.39,0.96 c0.22,0.08,0.47,0,0.59-0.22l1.92-3.32c0.12-0.22,0.07-0.47-0.12-0.61L19.14,12.94z M12,15.6c-1.98,0-3.6-1.62-3.6-3.6 s1.62-3.6,3.6-3.6s3.6,1.62,3.6,3.6S13.98,15.6,12,15.6z"/></svg>
258
- </button>
259
- ${this.options.pip ? `
260
- <button class="plex-btn plex-pip-btn" title="${i18n.pip || 'Picture-in-Picture'}">
261
- <svg viewBox="0 0 24 24"><path d="M19 7h-8v6h8V7zm2-4H3c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h18c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm0 16H3V5h18v14z"/></svg>
262
- </button>
263
- ` : ''}
264
- ${this.options.cast ? `
265
- <button class="plex-btn plex-cast-btn" title="${i18n.cast || 'Cast'}">
266
- <svg viewBox="0 0 24 24"><path d="M21 3H3c-1.1 0-2 .9-2 2v3h2V5h18v14h-7v2h7c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM1 18v3h3c0-1.66-1.34-3-3-3zm0-4v2c2.76 0 5 2.24 5 5h2c0-3.87-3.13-7-7-7zm0-4v2c4.97 0 9 4.03 9 9h2c0-6.08-4.93-11-11-11z"/></svg>
267
- </button>
268
- ` : ''}
269
- ${this.options.fullscreen ? `
270
- <button class="plex-btn plex-fullscreen-btn" title="${i18n.fullscreen || 'Fullscreen'}">
271
- <svg class="plex-icon-fullscreen" viewBox="0 0 24 24"><path d="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z"/></svg>
272
- <svg class="plex-icon-fullscreen-exit" viewBox="0 0 24 24"><path d="M5 16h3v3h2v-5H5v2zm3-8H5v2h5V5H8v3zm6 11h2v-3h3v-2h-5v5zm2-11V5h-2v5h5V8h-3z"/></svg>
273
- </button>
274
- ` : ''}
275
- </div>
276
- </div>
277
- `;
278
- }
279
- _cacheElements() {
280
- const $ = sel => this.controls.querySelector(sel);
281
- this.els = {
282
- progressContainer: $('.plex-progress-container'),
283
- progressBar: $('.plex-progress-bar'),
284
- progressBuffered: $('.plex-progress-buffered'),
285
- progressPlayed: $('.plex-progress-played'),
286
- progressHandle: $('.plex-progress-handle'),
287
- progressPreview: $('.plex-progress-preview'),
288
- previewTime: $('.plex-preview-time'),
289
- playBtn: $('.plex-play-btn'),
290
- prevBtn: $('.plex-prev-btn'),
291
- nextBtn: $('.plex-next-btn'),
292
- volumeBtn: $('.plex-volume-btn'),
293
- volumeSlider: $('.plex-volume-slider'),
294
- volumeTrack: $('.plex-volume-track'),
295
- volumeLevel: $('.plex-volume-level'),
296
- volumeHandle: $('.plex-volume-handle'),
297
- timeCurrent: $('.plex-time-current'),
298
- timeDuration: $('.plex-time-duration'),
299
- settingsBtn: $('.plex-settings-btn'),
300
- pipBtn: $('.plex-pip-btn'),
301
- castBtn: $('.plex-cast-btn'),
302
- fullscreenBtn: $('.plex-fullscreen-btn')
303
- };
304
- }
305
- _bindEvents() {
306
- // Video events
307
- this.video.addEventListener('play', () => this._onPlay());
308
- this.video.addEventListener('pause', () => this._onPause());
309
- this.video.addEventListener('ended', () => this._onEnded());
310
- this.video.addEventListener('timeupdate', () => this._onTimeUpdate());
311
- this.video.addEventListener('progress', () => this._onProgress());
312
- this.video.addEventListener('loadedmetadata', () => this._onLoadedMetadata());
313
- this.video.addEventListener('volumechange', () => this._onVolumeChange());
314
- this.video.addEventListener('waiting', () => this._onWaiting());
315
- this.video.addEventListener('canplay', () => this._onCanPlay());
316
- this.video.addEventListener('error', e => this._onError(e));
317
-
318
- // Control events
319
- this.bigPlayBtn.addEventListener('click', () => this.togglePlay());
320
- this.video.addEventListener('click', () => this.togglePlay());
321
- this.els.playBtn?.addEventListener('click', () => this.togglePlay());
322
- this.els.prevBtn?.addEventListener('click', () => this.previous());
323
- this.els.nextBtn?.addEventListener('click', () => this.next());
324
- this.els.volumeBtn?.addEventListener('click', () => this.toggleMute());
325
- this.els.pipBtn?.addEventListener('click', () => this.togglePiP());
326
- this.els.fullscreenBtn?.addEventListener('click', () => this.toggleFullscreen());
327
-
328
- // Progress bar
329
- this._bindProgressEvents();
330
-
331
- // Volume slider
332
- this._bindVolumeEvents();
333
-
334
- // Controls visibility
335
- this._bindControlsVisibility();
336
-
337
- // Fullscreen change
338
- document.addEventListener('fullscreenchange', () => this._onFullscreenChange());
339
- document.addEventListener('webkitfullscreenchange', () => this._onFullscreenChange());
340
- }
341
- _bindProgressEvents() {
342
- const progress = this.els.progressContainer;
343
- if (!progress) return;
344
- let isDragging = false;
345
- const seek = e => {
346
- const rect = this.els.progressBar.getBoundingClientRect();
347
- const percent = Utils.clamp((e.clientX - rect.left) / rect.width, 0, 1);
348
- this.seekPercent(percent * 100);
349
- };
350
- const updatePreview = e => {
351
- const rect = this.els.progressBar.getBoundingClientRect();
352
- const percent = Utils.clamp((e.clientX - rect.left) / rect.width, 0, 1);
353
- const time = percent * this.video.duration;
354
- if (this.els.previewTime) {
355
- this.els.previewTime.textContent = Utils.formatTime(time);
356
- }
357
- if (this.els.progressPreview) {
358
- const left = Utils.clamp(e.clientX - rect.left, 30, rect.width - 30);
359
- this.els.progressPreview.style.left = `${left}px`;
360
- }
361
- };
362
- progress.addEventListener('mousedown', e => {
363
- isDragging = true;
364
- seek(e);
365
- });
366
- progress.addEventListener('mousemove', e => {
367
- updatePreview(e);
368
- if (isDragging) seek(e);
369
- });
370
- document.addEventListener('mouseup', () => {
371
- isDragging = false;
372
- });
373
- document.addEventListener('mousemove', e => {
374
- if (isDragging) seek(e);
375
- });
376
- }
377
- _bindVolumeEvents() {
378
- const volumeTrack = this.els.volumeTrack;
379
- if (!volumeTrack) return;
380
- let isDragging = false;
381
- const setVolume = e => {
382
- const rect = volumeTrack.getBoundingClientRect();
383
- const percent = Utils.clamp((e.clientX - rect.left) / rect.width, 0, 1);
384
- this.setVolume(percent);
385
- };
386
- volumeTrack.addEventListener('mousedown', e => {
387
- isDragging = true;
388
- setVolume(e);
389
- });
390
- document.addEventListener('mousemove', e => {
391
- if (isDragging) setVolume(e);
392
- });
393
- document.addEventListener('mouseup', () => {
394
- isDragging = false;
395
- });
396
- }
397
- _bindControlsVisibility() {
398
- const showControls = () => {
399
- this.container.classList.add('plex-controls-visible');
400
- clearTimeout(this._controlsTimeout);
401
- if (this._state.playing) {
402
- this._controlsTimeout = setTimeout(() => {
403
- this.container.classList.remove('plex-controls-visible');
404
- }, this.options.controlsHideDelay);
405
- }
406
- };
407
- this.container.addEventListener('mousemove', showControls);
408
- this.container.addEventListener('mouseenter', showControls);
409
- this.container.addEventListener('mouseleave', () => {
410
- if (this._state.playing) {
411
- this.container.classList.remove('plex-controls-visible');
412
- }
413
- });
414
-
415
- // Always show when paused
416
- this.video.addEventListener('pause', showControls);
417
- }
418
-
419
- // Event handlers
420
- _onPlay() {
421
- this._state.playing = true;
422
- this._state.paused = false;
423
- this.container.classList.add('plex-playing');
424
- this.container.classList.remove('plex-paused');
425
- this.bigPlayBtn.style.display = 'none';
426
- this._emit('play');
427
- }
428
- _onPause() {
429
- this._state.playing = false;
430
- this._state.paused = true;
431
- this.container.classList.remove('plex-playing');
432
- this.container.classList.add('plex-paused');
433
- this.bigPlayBtn.style.display = '';
434
- this._emit('pause');
435
- }
436
- _onEnded() {
437
- this._state.playing = false;
438
- this.container.classList.remove('plex-playing');
439
-
440
- // Auto-play next if playlist
441
- if (this._playlist.length > 0 && this._currentIndex < this._playlist.length - 1) {
442
- this.next();
443
- } else {
444
- this._emit('ended');
445
- }
446
- }
447
- _onTimeUpdate() {
448
- this._state.currentTime = this.video.currentTime;
449
- const percent = this.video.currentTime / this.video.duration * 100;
450
- if (this.els.progressPlayed) {
451
- this.els.progressPlayed.style.width = `${percent}%`;
452
- }
453
- if (this.els.progressHandle) {
454
- this.els.progressHandle.style.left = `${percent}%`;
455
- }
456
- if (this.els.timeCurrent) {
457
- this.els.timeCurrent.textContent = Utils.formatTime(this.video.currentTime);
458
- }
459
- this._emit('timeupdate', {
460
- currentTime: this.video.currentTime,
461
- duration: this.video.duration
462
- });
463
- }
464
- _onProgress() {
465
- const buffered = this.video.buffered;
466
- if (buffered.length > 0) {
467
- const percent = buffered.end(buffered.length - 1) / this.video.duration * 100;
468
- this._state.buffered = percent;
469
- if (this.els.progressBuffered) {
470
- this.els.progressBuffered.style.width = `${percent}%`;
471
- }
472
- this._emit('progress', {
473
- buffered: percent
474
- });
475
- }
476
- }
477
- _onLoadedMetadata() {
478
- this._state.duration = this.video.duration;
479
- if (this.els.timeDuration) {
480
- this.els.timeDuration.textContent = Utils.formatTime(this.video.duration);
481
- }
482
- this._emit('loadedmetadata', {
483
- duration: this.video.duration
484
- });
485
- }
486
- _onVolumeChange() {
487
- this._state.volume = this.video.volume;
488
- this._state.muted = this.video.muted;
489
- const volume = this.video.muted ? 0 : this.video.volume;
490
-
491
- // Update volume UI
492
- if (this.els.volumeLevel) {
493
- this.els.volumeLevel.style.width = `${volume * 100}%`;
494
- }
495
- if (this.els.volumeHandle) {
496
- this.els.volumeHandle.style.left = `${volume * 100}%`;
497
- }
498
-
499
- // Update icon
500
- this.container.classList.remove('plex-muted', 'plex-volume-low');
501
- if (this.video.muted || volume === 0) {
502
- this.container.classList.add('plex-muted');
503
- } else if (volume < 0.5) {
504
- this.container.classList.add('plex-volume-low');
505
- }
506
- this._emit('volumechange', {
507
- volume: this.video.volume,
508
- muted: this.video.muted
509
- });
510
- }
511
- _onWaiting() {
512
- this.container.classList.add('plex-loading');
513
- this._emit('waiting');
514
- }
515
- _onCanPlay() {
516
- this.container.classList.remove('plex-loading');
517
- this._emit('canplay');
518
- }
519
- _onError(e) {
520
- this._emit('error', {
521
- code: this.video.error?.code,
522
- message: this.video.error?.message
523
- });
524
- }
525
- _onFullscreenChange() {
526
- const isFullscreen = !!(document.fullscreenElement || document.webkitFullscreenElement);
527
- this._state.fullscreen = isFullscreen;
528
- if (isFullscreen) {
529
- this.container.classList.add('plex-fullscreen');
530
- } else {
531
- this.container.classList.remove('plex-fullscreen');
532
- }
533
- this._emit('fullscreenchange', {
534
- isFullscreen
535
- });
536
- }
537
-
538
- // Keyboard support
539
- _initKeyboard() {
540
- this._keyHandler = e => {
541
- if (!this.container.contains(document.activeElement) && document.activeElement !== document.body) return;
542
- switch (e.key) {
543
- case ' ':
544
- case 'k':
545
- e.preventDefault();
546
- this.togglePlay();
547
- break;
548
- case 'ArrowLeft':
549
- e.preventDefault();
550
- this.seek(this.video.currentTime - 10);
551
- break;
552
- case 'ArrowRight':
553
- e.preventDefault();
554
- this.seek(this.video.currentTime + 10);
555
- break;
556
- case 'ArrowUp':
557
- e.preventDefault();
558
- this.setVolume(Math.min(1, this.video.volume + 0.1));
559
- break;
560
- case 'ArrowDown':
561
- e.preventDefault();
562
- this.setVolume(Math.max(0, this.video.volume - 0.1));
563
- break;
564
- case 'm':
565
- this.toggleMute();
566
- break;
567
- case 'f':
568
- this.toggleFullscreen();
569
- break;
570
- case 'p':
571
- this.togglePiP();
572
- break;
573
- case 'n':
574
- if (e.shiftKey) {
575
- this.previous();
576
- } else {
577
- this.next();
578
- }
579
- break;
580
- }
581
- };
582
- document.addEventListener('keydown', this._keyHandler);
583
- }
584
-
585
- // Touch support
586
- _initTouch() {
587
- let touchStartX = 0;
588
- let touchStartY = 0;
589
- let touchStartTime = 0;
590
- this.video.addEventListener('touchstart', e => {
591
- touchStartX = e.touches[0].clientX;
592
- touchStartY = e.touches[0].clientY;
593
- touchStartTime = Date.now();
594
- });
595
- this.video.addEventListener('touchend', e => {
596
- const touchEndX = e.changedTouches[0].clientX;
597
- const touchEndY = e.changedTouches[0].clientY;
598
- const touchDuration = Date.now() - touchStartTime;
599
- const deltaX = touchEndX - touchStartX;
600
- const deltaY = touchEndY - touchStartY;
601
-
602
- // Tap to toggle play (if quick tap)
603
- if (Math.abs(deltaX) < 30 && Math.abs(deltaY) < 30 && touchDuration < 300) {
604
- this.togglePlay();
605
- }
606
- // Swipe to seek
607
- else if (Math.abs(deltaX) > 50 && Math.abs(deltaY) < 30) {
608
- if (deltaX > 0) {
609
- this.seek(this.video.currentTime + 10);
610
- } else {
611
- this.seek(this.video.currentTime - 10);
612
- }
613
- }
614
- });
615
- }
616
-
617
- // PiP support
618
- _initPiP() {
619
- if (!document.pictureInPictureEnabled) {
620
- if (this.els.pipBtn) {
621
- this.els.pipBtn.style.display = 'none';
622
- }
623
- return;
624
- }
625
- this.video.addEventListener('enterpictureinpicture', () => {
626
- this._state.pip = true;
627
- this.container.classList.add('plex-pip');
628
- this._emit('enterpip');
629
- });
630
- this.video.addEventListener('leavepictureinpicture', () => {
631
- this._state.pip = false;
632
- this.container.classList.remove('plex-pip');
633
- this._emit('leavepip');
634
- });
635
- }
636
-
637
- // Cast support
638
- _initCast() {
639
- // Chromecast requires HTTPS and specific browser
640
- const isChrome = /Chrome/.test(navigator.userAgent) && !/Edge/.test(navigator.userAgent);
641
- const isHTTPS = location.protocol === 'https:' || location.hostname === 'localhost';
642
- if (!isChrome || !isHTTPS) {
643
- if (this.els.castBtn) {
644
- this.els.castBtn.style.display = 'none';
645
- }
646
- return;
647
- }
648
-
649
- // Load Cast SDK if available
650
- if (window.chrome && window.chrome.cast) {
651
- this._initCastApi();
652
- } else {
653
- window['__onGCastApiAvailable'] = isAvailable => {
654
- if (isAvailable) this._initCastApi();
655
- };
656
- }
657
- }
658
- _initCastApi() {
659
- if (this.els.castBtn) {
660
- this.els.castBtn.addEventListener('click', () => {
661
- if (window.cast && window.cast.framework) {
662
- const context = cast.framework.CastContext.getInstance();
663
- context.requestSession();
664
- }
665
- });
666
- }
667
- }
668
-
669
- // Event system
670
- on(event, callback) {
671
- if (!this._eventListeners.has(event)) {
672
- this._eventListeners.set(event, new Set());
673
- }
674
- this._eventListeners.get(event).add(callback);
675
- return this;
676
- }
677
- off(event, callback) {
678
- if (this._eventListeners.has(event)) {
679
- this._eventListeners.get(event).delete(callback);
680
- }
681
- return this;
682
- }
683
- _emit(event, data = {}) {
684
- if (this._eventListeners.has(event)) {
685
- this._eventListeners.get(event).forEach(callback => {
686
- try {
687
- callback(data);
688
- } catch (e) {
689
- console.error(`PlexPlayer: Error in ${event} handler:`, e);
690
- }
691
- });
692
- }
693
- }
694
-
695
- // Public API
696
- load(src, poster) {
697
- this.video.src = src;
698
- if (poster) this.video.poster = poster;
699
- this.video.load();
700
- if (this.options.autoplay) {
701
- this.play();
702
- }
703
- return this;
704
- }
705
- loadPlaylist(items) {
706
- this._playlist = items.map((item, index) => ({
707
- ...item,
708
- index
709
- }));
710
- this._currentIndex = 0;
711
- if (this._playlist.length > 0) {
712
- const first = this._playlist[0];
713
- this.load(first.src, first.poster);
714
- this._emit('playlistload', {
715
- playlist: this._playlist
716
- });
717
- }
718
- return this;
719
- }
720
- play() {
721
- return this.video.play();
722
- }
723
- pause() {
724
- this.video.pause();
725
- return this;
726
- }
727
- togglePlay() {
728
- if (this.video.paused) {
729
- this.play();
730
- } else {
731
- this.pause();
732
- }
733
- return this;
734
- }
735
- seek(time) {
736
- this.video.currentTime = Utils.clamp(time, 0, this.video.duration || 0);
737
- return this;
738
- }
739
- seekPercent(percent) {
740
- const time = percent / 100 * this.video.duration;
741
- return this.seek(time);
742
- }
743
- setVolume(level) {
744
- this.video.volume = Utils.clamp(level, 0, 1);
745
- if (this.video.muted && level > 0) {
746
- this.video.muted = false;
747
- }
748
- return this;
749
- }
750
- getVolume() {
751
- return this.video.volume;
752
- }
753
- mute() {
754
- this.video.muted = true;
755
- return this;
756
- }
757
- unmute() {
758
- this.video.muted = false;
759
- return this;
760
- }
761
- toggleMute() {
762
- this.video.muted = !this.video.muted;
763
- return this;
764
- }
765
- setPlaybackRate(rate) {
766
- this.video.playbackRate = rate;
767
- this._state.playbackRate = rate;
768
- this._emit('ratechange', {
769
- rate
770
- });
771
- return this;
772
- }
773
- getPlaybackRate() {
774
- return this.video.playbackRate;
775
- }
776
- enterFullscreen() {
777
- const el = this.container;
778
- if (el.requestFullscreen) {
779
- return el.requestFullscreen();
780
- } else if (el.webkitRequestFullscreen) {
781
- return el.webkitRequestFullscreen();
782
- }
783
- }
784
- exitFullscreen() {
785
- if (document.exitFullscreen) {
786
- return document.exitFullscreen();
787
- } else if (document.webkitExitFullscreen) {
788
- return document.webkitExitFullscreen();
789
- }
790
- }
791
- toggleFullscreen() {
792
- if (this._state.fullscreen) {
793
- this.exitFullscreen();
794
- } else {
795
- this.enterFullscreen();
796
- }
797
- return this;
798
- }
799
- enterPiP() {
800
- if (document.pictureInPictureEnabled && this.video !== document.pictureInPictureElement) {
801
- return this.video.requestPictureInPicture();
802
- }
803
- }
804
- exitPiP() {
805
- if (document.pictureInPictureElement) {
806
- return document.exitPictureInPicture();
807
- }
808
- }
809
- togglePiP() {
810
- if (this._state.pip) {
811
- this.exitPiP();
812
- } else {
813
- this.enterPiP();
814
- }
815
- return this;
816
- }
817
- next() {
818
- if (this._playlist.length > 0 && this._currentIndex < this._playlist.length - 1) {
819
- this._currentIndex++;
820
- const item = this._playlist[this._currentIndex];
821
- this.load(item.src, item.poster);
822
- this.play();
823
- this._emit('trackchange', {
824
- index: this._currentIndex,
825
- item
826
- });
827
- }
828
- return this;
829
- }
830
- previous() {
831
- if (this._playlist.length > 0 && this._currentIndex > 0) {
832
- this._currentIndex--;
833
- const item = this._playlist[this._currentIndex];
834
- this.load(item.src, item.poster);
835
- this.play();
836
- this._emit('trackchange', {
837
- index: this._currentIndex,
838
- item
839
- });
840
- }
841
- return this;
842
- }
843
- playAt(index) {
844
- if (this._playlist.length > 0 && index >= 0 && index < this._playlist.length) {
845
- this._currentIndex = index;
846
- const item = this._playlist[this._currentIndex];
847
- this.load(item.src, item.poster);
848
- this.play();
849
- this._emit('trackchange', {
850
- index: this._currentIndex,
851
- item
852
- });
853
- }
854
- return this;
855
- }
856
- getState() {
857
- return {
858
- currentTime: this.video.currentTime,
859
- duration: this.video.duration || 0,
860
- volume: this.video.volume,
861
- muted: this.video.muted,
862
- isPlaying: !this.video.paused && !this.video.ended,
863
- isPaused: this.video.paused,
864
- isFullscreen: this._state.fullscreen,
865
- isPiP: this._state.pip,
866
- playbackRate: this.video.playbackRate,
867
- currentTrack: this._currentIndex,
868
- totalTracks: this._playlist.length
869
- };
870
- }
871
- getVideo() {
872
- return this.video;
873
- }
874
- destroy() {
875
- // Clear timeouts
876
- clearTimeout(this._controlsTimeout);
877
-
878
- // Remove keyboard listener
879
- if (this._keyHandler) {
880
- document.removeEventListener('keydown', this._keyHandler);
881
- }
882
-
883
- // Remove event listeners
884
- this._eventListeners.clear();
885
-
886
- // Clear container
887
- this.container.innerHTML = '';
888
- this.container.classList.remove('plex-player');
889
-
890
- // Emit destroy event
891
- this._emit('destroy');
892
- }
893
- }
894
-
895
- // Attach to window for UMD builds
896
- if (typeof window !== 'undefined') {
897
- window.PlexPlayer = PlexPlayer;
898
- }
899
-
900
- exports.PlexPlayer = PlexPlayer;
901
- exports.Utils = Utils;
902
- exports.default = PlexPlayer;
903
- //# sourceMappingURL=plex-player.cjs.js.map