@frameset/plex-player 1.0.6 → 2.0.1

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,1027 +0,0 @@
1
- /*!
2
- * @frameset/plex-player v1.0.6
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
- // Settings panel
212
- this.settingsPanel = document.createElement('div');
213
- this.settingsPanel.className = 'plex-settings-panel';
214
- this.settingsPanel.innerHTML = this._getSettingsHTML();
215
- this.container.appendChild(this.settingsPanel);
216
- }
217
- _getSettingsHTML() {
218
- const i18n = this.i18n;
219
- return `
220
- <div class="plex-settings-header">
221
- <span class="plex-settings-title">${i18n.settings || 'Settings'}</span>
222
- <button class="plex-settings-close">
223
- <svg viewBox="0 0 24 24"><path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/></svg>
224
- </button>
225
- </div>
226
- <div class="plex-settings-content">
227
- <div class="plex-settings-item" data-setting="speed">
228
- <span class="plex-settings-label">${i18n.speed || 'Speed'}</span>
229
- <span class="plex-settings-value">1x</span>
230
- </div>
231
- <div class="plex-settings-item" data-setting="quality">
232
- <span class="plex-settings-label">${i18n.quality || 'Quality'}</span>
233
- <span class="plex-settings-value">Auto</span>
234
- </div>
235
- <div class="plex-settings-item" data-setting="subtitles">
236
- <span class="plex-settings-label">${i18n.subtitles || 'Subtitles'}</span>
237
- <span class="plex-settings-value">${i18n.off || 'Off'}</span>
238
- </div>
239
- </div>
240
- <div class="plex-settings-submenu plex-settings-speed" style="display: none;">
241
- <div class="plex-settings-back">
242
- <svg viewBox="0 0 24 24"><path d="M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z"/></svg>
243
- <span>${i18n.speed || 'Speed'}</span>
244
- </div>
245
- <div class="plex-speed-options">
246
- <button class="plex-speed-option" data-speed="0.25">0.25x</button>
247
- <button class="plex-speed-option" data-speed="0.5">0.5x</button>
248
- <button class="plex-speed-option" data-speed="0.75">0.75x</button>
249
- <button class="plex-speed-option active" data-speed="1">1x</button>
250
- <button class="plex-speed-option" data-speed="1.25">1.25x</button>
251
- <button class="plex-speed-option" data-speed="1.5">1.5x</button>
252
- <button class="plex-speed-option" data-speed="1.75">1.75x</button>
253
- <button class="plex-speed-option" data-speed="2">2x</button>
254
- </div>
255
- </div>
256
- `;
257
- }
258
- _getControlsHTML() {
259
- const i18n = this.i18n;
260
- return `
261
- <div class="plex-progress-container">
262
- <div class="plex-progress-bar">
263
- <div class="plex-progress-buffered"></div>
264
- <div class="plex-progress-played"></div>
265
- <div class="plex-progress-handle"></div>
266
- </div>
267
- <div class="plex-progress-preview">
268
- <div class="plex-preview-time">0:00</div>
269
- </div>
270
- </div>
271
- <div class="plex-controls-bar">
272
- <div class="plex-controls-left">
273
- <button class="plex-btn plex-play-btn" title="${i18n.play || 'Play'}">
274
- <svg class="plex-icon-play" viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>
275
- <svg class="plex-icon-pause" viewBox="0 0 24 24"><path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/></svg>
276
- </button>
277
- <button class="plex-btn plex-prev-btn" title="${i18n.previous || 'Previous'}">
278
- <svg viewBox="0 0 24 24"><path d="M6 6h2v12H6zm3.5 6l8.5 6V6z"/></svg>
279
- </button>
280
- <button class="plex-btn plex-next-btn" title="${i18n.next || 'Next'}">
281
- <svg viewBox="0 0 24 24"><path d="M6 18l8.5-6L6 6v12zM16 6v12h2V6h-2z"/></svg>
282
- </button>
283
- <div class="plex-volume-container">
284
- <button class="plex-btn plex-volume-btn" title="${i18n.mute || 'Mute'}">
285
- <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>
286
- <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>
287
- <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>
288
- </button>
289
- <div class="plex-volume-slider">
290
- <div class="plex-volume-track">
291
- <div class="plex-volume-level"></div>
292
- <div class="plex-volume-handle"></div>
293
- </div>
294
- </div>
295
- </div>
296
- <div class="plex-time">
297
- <span class="plex-time-current">0:00</span>
298
- <span class="plex-time-separator">/</span>
299
- <span class="plex-time-duration">0:00</span>
300
- </div>
301
- </div>
302
- <div class="plex-controls-right">
303
- <button class="plex-btn plex-settings-btn" title="${i18n.settings || 'Settings'}">
304
- <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>
305
- </button>
306
- ${this.options.pip ? `
307
- <button class="plex-btn plex-pip-btn" title="${i18n.pip || 'Picture-in-Picture'}">
308
- <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>
309
- </button>
310
- ` : ''}
311
- ${this.options.cast ? `
312
- <button class="plex-btn plex-cast-btn" title="${i18n.cast || 'Cast'}">
313
- <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>
314
- </button>
315
- ` : ''}
316
- ${this.options.fullscreen ? `
317
- <button class="plex-btn plex-fullscreen-btn" title="${i18n.fullscreen || 'Fullscreen'}">
318
- <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>
319
- <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>
320
- </button>
321
- ` : ''}
322
- </div>
323
- </div>
324
- `;
325
- }
326
- _cacheElements() {
327
- const $ = sel => this.controls.querySelector(sel);
328
- this.els = {
329
- progressContainer: $('.plex-progress-container'),
330
- progressBar: $('.plex-progress-bar'),
331
- progressBuffered: $('.plex-progress-buffered'),
332
- progressPlayed: $('.plex-progress-played'),
333
- progressHandle: $('.plex-progress-handle'),
334
- progressPreview: $('.plex-progress-preview'),
335
- previewTime: $('.plex-preview-time'),
336
- playBtn: $('.plex-play-btn'),
337
- prevBtn: $('.plex-prev-btn'),
338
- nextBtn: $('.plex-next-btn'),
339
- volumeBtn: $('.plex-volume-btn'),
340
- volumeSlider: $('.plex-volume-slider'),
341
- volumeTrack: $('.plex-volume-track'),
342
- volumeLevel: $('.plex-volume-level'),
343
- volumeHandle: $('.plex-volume-handle'),
344
- timeCurrent: $('.plex-time-current'),
345
- timeDuration: $('.plex-time-duration'),
346
- settingsBtn: $('.plex-settings-btn'),
347
- pipBtn: $('.plex-pip-btn'),
348
- castBtn: $('.plex-cast-btn'),
349
- fullscreenBtn: $('.plex-fullscreen-btn')
350
- };
351
- }
352
- _bindEvents() {
353
- // Video events
354
- this.video.addEventListener('play', () => this._onPlay());
355
- this.video.addEventListener('pause', () => this._onPause());
356
- this.video.addEventListener('ended', () => this._onEnded());
357
- this.video.addEventListener('timeupdate', () => this._onTimeUpdate());
358
- this.video.addEventListener('progress', () => this._onProgress());
359
- this.video.addEventListener('loadedmetadata', () => this._onLoadedMetadata());
360
- this.video.addEventListener('volumechange', () => this._onVolumeChange());
361
- this.video.addEventListener('waiting', () => this._onWaiting());
362
- this.video.addEventListener('canplay', () => this._onCanPlay());
363
- this.video.addEventListener('error', e => this._onError(e));
364
-
365
- // Control events
366
- this.bigPlayBtn.addEventListener('click', () => this.togglePlay());
367
- this.video.addEventListener('click', () => this.togglePlay());
368
- this.els.playBtn?.addEventListener('click', () => this.togglePlay());
369
- this.els.prevBtn?.addEventListener('click', () => this.previous());
370
- this.els.nextBtn?.addEventListener('click', () => this.next());
371
- this.els.volumeBtn?.addEventListener('click', () => this.toggleMute());
372
- this.els.pipBtn?.addEventListener('click', () => this.togglePiP());
373
- this.els.fullscreenBtn?.addEventListener('click', () => this.toggleFullscreen());
374
- this.els.settingsBtn?.addEventListener('click', () => this.toggleSettings());
375
- this.els.castBtn?.addEventListener('click', () => this.cast());
376
-
377
- // Settings panel events
378
- this._bindSettingsEvents();
379
-
380
- // Progress bar
381
- this._bindProgressEvents();
382
-
383
- // Volume slider
384
- this._bindVolumeEvents();
385
-
386
- // Controls visibility
387
- this._bindControlsVisibility();
388
-
389
- // Fullscreen change
390
- document.addEventListener('fullscreenchange', () => this._onFullscreenChange());
391
- document.addEventListener('webkitfullscreenchange', () => this._onFullscreenChange());
392
- }
393
- _bindProgressEvents() {
394
- const progress = this.els.progressContainer;
395
- if (!progress) return;
396
- let isDragging = false;
397
- const seek = e => {
398
- const rect = this.els.progressBar.getBoundingClientRect();
399
- const percent = Utils.clamp((e.clientX - rect.left) / rect.width, 0, 1);
400
- this.seekPercent(percent * 100);
401
- };
402
- const updatePreview = e => {
403
- const rect = this.els.progressBar.getBoundingClientRect();
404
- const percent = Utils.clamp((e.clientX - rect.left) / rect.width, 0, 1);
405
- const time = percent * this.video.duration;
406
- if (this.els.previewTime) {
407
- this.els.previewTime.textContent = Utils.formatTime(time);
408
- }
409
- if (this.els.progressPreview) {
410
- const left = Utils.clamp(e.clientX - rect.left, 30, rect.width - 30);
411
- this.els.progressPreview.style.left = `${left}px`;
412
- }
413
- };
414
- progress.addEventListener('mousedown', e => {
415
- isDragging = true;
416
- seek(e);
417
- });
418
- progress.addEventListener('mousemove', e => {
419
- updatePreview(e);
420
- if (isDragging) seek(e);
421
- });
422
- document.addEventListener('mouseup', () => {
423
- isDragging = false;
424
- });
425
- document.addEventListener('mousemove', e => {
426
- if (isDragging) seek(e);
427
- });
428
- }
429
- _bindVolumeEvents() {
430
- const volumeTrack = this.els.volumeTrack;
431
- if (!volumeTrack) return;
432
- let isDragging = false;
433
- const setVolume = e => {
434
- const rect = volumeTrack.getBoundingClientRect();
435
- const percent = Utils.clamp((e.clientX - rect.left) / rect.width, 0, 1);
436
- this.setVolume(percent);
437
- };
438
- volumeTrack.addEventListener('mousedown', e => {
439
- isDragging = true;
440
- setVolume(e);
441
- });
442
- document.addEventListener('mousemove', e => {
443
- if (isDragging) setVolume(e);
444
- });
445
- document.addEventListener('mouseup', () => {
446
- isDragging = false;
447
- });
448
- }
449
- _bindControlsVisibility() {
450
- const showControls = () => {
451
- this.container.classList.add('plex-controls-visible');
452
- clearTimeout(this._controlsTimeout);
453
- if (this._state.playing) {
454
- this._controlsTimeout = setTimeout(() => {
455
- this.container.classList.remove('plex-controls-visible');
456
- }, this.options.controlsHideDelay);
457
- }
458
- };
459
- this.container.addEventListener('mousemove', showControls);
460
- this.container.addEventListener('mouseenter', showControls);
461
- this.container.addEventListener('mouseleave', () => {
462
- if (this._state.playing) {
463
- this.container.classList.remove('plex-controls-visible');
464
- }
465
- });
466
-
467
- // Always show when paused
468
- this.video.addEventListener('pause', showControls);
469
- }
470
-
471
- // Event handlers
472
- _onPlay() {
473
- this._state.playing = true;
474
- this._state.paused = false;
475
- this.container.classList.add('plex-playing');
476
- this.container.classList.remove('plex-paused');
477
- this.bigPlayBtn.style.display = 'none';
478
- this._emit('play');
479
- }
480
- _onPause() {
481
- this._state.playing = false;
482
- this._state.paused = true;
483
- this.container.classList.remove('plex-playing');
484
- this.container.classList.add('plex-paused');
485
- this.bigPlayBtn.style.display = '';
486
- this._emit('pause');
487
- }
488
- _onEnded() {
489
- this._state.playing = false;
490
- this.container.classList.remove('plex-playing');
491
-
492
- // Auto-play next if playlist
493
- if (this._playlist.length > 0 && this._currentIndex < this._playlist.length - 1) {
494
- this.next();
495
- } else {
496
- this._emit('ended');
497
- }
498
- }
499
- _onTimeUpdate() {
500
- this._state.currentTime = this.video.currentTime;
501
- const percent = this.video.currentTime / this.video.duration * 100;
502
- if (this.els.progressPlayed) {
503
- this.els.progressPlayed.style.width = `${percent}%`;
504
- }
505
- if (this.els.progressHandle) {
506
- this.els.progressHandle.style.left = `${percent}%`;
507
- }
508
- if (this.els.timeCurrent) {
509
- this.els.timeCurrent.textContent = Utils.formatTime(this.video.currentTime);
510
- }
511
- this._emit('timeupdate', {
512
- currentTime: this.video.currentTime,
513
- duration: this.video.duration
514
- });
515
- }
516
- _onProgress() {
517
- const buffered = this.video.buffered;
518
- if (buffered.length > 0) {
519
- const percent = buffered.end(buffered.length - 1) / this.video.duration * 100;
520
- this._state.buffered = percent;
521
- if (this.els.progressBuffered) {
522
- this.els.progressBuffered.style.width = `${percent}%`;
523
- }
524
- this._emit('progress', {
525
- buffered: percent
526
- });
527
- }
528
- }
529
- _onLoadedMetadata() {
530
- this._state.duration = this.video.duration;
531
- if (this.els.timeDuration) {
532
- this.els.timeDuration.textContent = Utils.formatTime(this.video.duration);
533
- }
534
- this._emit('loadedmetadata', {
535
- duration: this.video.duration
536
- });
537
- }
538
- _onVolumeChange() {
539
- this._state.volume = this.video.volume;
540
- this._state.muted = this.video.muted;
541
- const volume = this.video.muted ? 0 : this.video.volume;
542
-
543
- // Update volume UI
544
- if (this.els.volumeLevel) {
545
- this.els.volumeLevel.style.width = `${volume * 100}%`;
546
- }
547
- if (this.els.volumeHandle) {
548
- this.els.volumeHandle.style.left = `${volume * 100}%`;
549
- }
550
-
551
- // Update icon
552
- this.container.classList.remove('plex-muted', 'plex-volume-low');
553
- if (this.video.muted || volume === 0) {
554
- this.container.classList.add('plex-muted');
555
- } else if (volume < 0.5) {
556
- this.container.classList.add('plex-volume-low');
557
- }
558
- this._emit('volumechange', {
559
- volume: this.video.volume,
560
- muted: this.video.muted
561
- });
562
- }
563
- _onWaiting() {
564
- this.container.classList.add('plex-loading');
565
- this._emit('waiting');
566
- }
567
- _onCanPlay() {
568
- this.container.classList.remove('plex-loading');
569
- this._emit('canplay');
570
- }
571
- _onError(e) {
572
- this._emit('error', {
573
- code: this.video.error?.code,
574
- message: this.video.error?.message
575
- });
576
- }
577
- _onFullscreenChange() {
578
- const isFullscreen = !!(document.fullscreenElement || document.webkitFullscreenElement);
579
- this._state.fullscreen = isFullscreen;
580
- if (isFullscreen) {
581
- this.container.classList.add('plex-fullscreen');
582
- } else {
583
- this.container.classList.remove('plex-fullscreen');
584
- }
585
- this._emit('fullscreenchange', {
586
- isFullscreen
587
- });
588
- }
589
-
590
- // Keyboard support
591
- _initKeyboard() {
592
- this._keyHandler = e => {
593
- if (!this.container.contains(document.activeElement) && document.activeElement !== document.body) return;
594
- switch (e.key) {
595
- case ' ':
596
- case 'k':
597
- e.preventDefault();
598
- this.togglePlay();
599
- break;
600
- case 'ArrowLeft':
601
- e.preventDefault();
602
- this.seek(this.video.currentTime - 10);
603
- break;
604
- case 'ArrowRight':
605
- e.preventDefault();
606
- this.seek(this.video.currentTime + 10);
607
- break;
608
- case 'ArrowUp':
609
- e.preventDefault();
610
- this.setVolume(Math.min(1, this.video.volume + 0.1));
611
- break;
612
- case 'ArrowDown':
613
- e.preventDefault();
614
- this.setVolume(Math.max(0, this.video.volume - 0.1));
615
- break;
616
- case 'm':
617
- this.toggleMute();
618
- break;
619
- case 'f':
620
- this.toggleFullscreen();
621
- break;
622
- case 'p':
623
- this.togglePiP();
624
- break;
625
- case 'n':
626
- if (e.shiftKey) {
627
- this.previous();
628
- } else {
629
- this.next();
630
- }
631
- break;
632
- }
633
- };
634
- document.addEventListener('keydown', this._keyHandler);
635
- }
636
-
637
- // Touch support
638
- _initTouch() {
639
- let touchStartX = 0;
640
- let touchStartY = 0;
641
- let touchStartTime = 0;
642
- this.video.addEventListener('touchstart', e => {
643
- touchStartX = e.touches[0].clientX;
644
- touchStartY = e.touches[0].clientY;
645
- touchStartTime = Date.now();
646
- });
647
- this.video.addEventListener('touchend', e => {
648
- const touchEndX = e.changedTouches[0].clientX;
649
- const touchEndY = e.changedTouches[0].clientY;
650
- const touchDuration = Date.now() - touchStartTime;
651
- const deltaX = touchEndX - touchStartX;
652
- const deltaY = touchEndY - touchStartY;
653
-
654
- // Tap to toggle play (if quick tap)
655
- if (Math.abs(deltaX) < 30 && Math.abs(deltaY) < 30 && touchDuration < 300) {
656
- this.togglePlay();
657
- }
658
- // Swipe to seek
659
- else if (Math.abs(deltaX) > 50 && Math.abs(deltaY) < 30) {
660
- if (deltaX > 0) {
661
- this.seek(this.video.currentTime + 10);
662
- } else {
663
- this.seek(this.video.currentTime - 10);
664
- }
665
- }
666
- });
667
- }
668
-
669
- // PiP support
670
- _initPiP() {
671
- if (!document.pictureInPictureEnabled) {
672
- if (this.els.pipBtn) {
673
- this.els.pipBtn.style.display = 'none';
674
- }
675
- return;
676
- }
677
- this.video.addEventListener('enterpictureinpicture', () => {
678
- this._state.pip = true;
679
- this.container.classList.add('plex-pip');
680
- this._emit('enterpip');
681
- });
682
- this.video.addEventListener('leavepictureinpicture', () => {
683
- this._state.pip = false;
684
- this.container.classList.remove('plex-pip');
685
- this._emit('leavepip');
686
- });
687
- }
688
-
689
- // Cast support
690
- _initCast() {
691
- // Chromecast requires HTTPS and specific browser
692
- const isChrome = /Chrome/.test(navigator.userAgent) && !/Edge/.test(navigator.userAgent);
693
- const isHTTPS = location.protocol === 'https:' || location.hostname === 'localhost';
694
- if (!isChrome || !isHTTPS) {
695
- if (this.els.castBtn) {
696
- this.els.castBtn.style.display = 'none';
697
- }
698
- return;
699
- }
700
-
701
- // Load Cast SDK if available
702
- if (window.chrome && window.chrome.cast) {
703
- this._initCastApi();
704
- } else {
705
- window['__onGCastApiAvailable'] = isAvailable => {
706
- if (isAvailable) this._initCastApi();
707
- };
708
- }
709
- }
710
- _initCastApi() {
711
- if (this.els.castBtn) {
712
- this.els.castBtn.addEventListener('click', () => {
713
- if (window.cast && window.cast.framework) {
714
- const context = cast.framework.CastContext.getInstance();
715
- context.requestSession();
716
- }
717
- });
718
- }
719
- }
720
-
721
- // Event system
722
- on(event, callback) {
723
- if (!this._eventListeners.has(event)) {
724
- this._eventListeners.set(event, new Set());
725
- }
726
- this._eventListeners.get(event).add(callback);
727
- return this;
728
- }
729
- off(event, callback) {
730
- if (this._eventListeners.has(event)) {
731
- this._eventListeners.get(event).delete(callback);
732
- }
733
- return this;
734
- }
735
- _emit(event, data = {}) {
736
- if (this._eventListeners.has(event)) {
737
- this._eventListeners.get(event).forEach(callback => {
738
- try {
739
- callback(data);
740
- } catch (e) {
741
- console.error(`PlexPlayer: Error in ${event} handler:`, e);
742
- }
743
- });
744
- }
745
- }
746
-
747
- // Public API
748
- load(src, poster) {
749
- this.video.src = src;
750
- if (poster) this.video.poster = poster;
751
- this.video.load();
752
- if (this.options.autoplay) {
753
- this.play();
754
- }
755
- return this;
756
- }
757
- loadPlaylist(items) {
758
- this._playlist = items.map((item, index) => ({
759
- ...item,
760
- index
761
- }));
762
- this._currentIndex = 0;
763
- if (this._playlist.length > 0) {
764
- const first = this._playlist[0];
765
- this.load(first.src, first.poster);
766
- this._emit('playlistload', {
767
- playlist: this._playlist
768
- });
769
- }
770
- return this;
771
- }
772
- play() {
773
- return this.video.play();
774
- }
775
- pause() {
776
- this.video.pause();
777
- return this;
778
- }
779
- togglePlay() {
780
- if (this.video.paused) {
781
- this.play();
782
- } else {
783
- this.pause();
784
- }
785
- return this;
786
- }
787
- seek(time) {
788
- this.video.currentTime = Utils.clamp(time, 0, this.video.duration || 0);
789
- return this;
790
- }
791
- seekPercent(percent) {
792
- const time = percent / 100 * this.video.duration;
793
- return this.seek(time);
794
- }
795
- setVolume(level) {
796
- this.video.volume = Utils.clamp(level, 0, 1);
797
- if (this.video.muted && level > 0) {
798
- this.video.muted = false;
799
- }
800
- return this;
801
- }
802
- getVolume() {
803
- return this.video.volume;
804
- }
805
- mute() {
806
- this.video.muted = true;
807
- return this;
808
- }
809
- unmute() {
810
- this.video.muted = false;
811
- return this;
812
- }
813
- toggleMute() {
814
- this.video.muted = !this.video.muted;
815
- return this;
816
- }
817
- setPlaybackRate(rate) {
818
- this.video.playbackRate = rate;
819
- this._state.playbackRate = rate;
820
- this._emit('ratechange', {
821
- rate
822
- });
823
- return this;
824
- }
825
- getPlaybackRate() {
826
- return this.video.playbackRate;
827
- }
828
- enterFullscreen() {
829
- const el = this.container;
830
- if (el.requestFullscreen) {
831
- return el.requestFullscreen();
832
- } else if (el.webkitRequestFullscreen) {
833
- return el.webkitRequestFullscreen();
834
- }
835
- }
836
- exitFullscreen() {
837
- if (document.exitFullscreen) {
838
- return document.exitFullscreen();
839
- } else if (document.webkitExitFullscreen) {
840
- return document.webkitExitFullscreen();
841
- }
842
- }
843
- toggleFullscreen() {
844
- if (this._state.fullscreen) {
845
- this.exitFullscreen();
846
- } else {
847
- this.enterFullscreen();
848
- }
849
- return this;
850
- }
851
- enterPiP() {
852
- if (document.pictureInPictureEnabled && this.video !== document.pictureInPictureElement) {
853
- return this.video.requestPictureInPicture();
854
- }
855
- }
856
- exitPiP() {
857
- if (document.pictureInPictureElement) {
858
- return document.exitPictureInPicture();
859
- }
860
- }
861
- togglePiP() {
862
- if (this._state.pip) {
863
- this.exitPiP();
864
- } else {
865
- this.enterPiP();
866
- }
867
- return this;
868
- }
869
- next() {
870
- if (this._playlist.length > 0 && this._currentIndex < this._playlist.length - 1) {
871
- this._currentIndex++;
872
- const item = this._playlist[this._currentIndex];
873
- this.load(item.src, item.poster);
874
- this.play();
875
- this._emit('trackchange', {
876
- index: this._currentIndex,
877
- item
878
- });
879
- }
880
- return this;
881
- }
882
- previous() {
883
- if (this._playlist.length > 0 && this._currentIndex > 0) {
884
- this._currentIndex--;
885
- const item = this._playlist[this._currentIndex];
886
- this.load(item.src, item.poster);
887
- this.play();
888
- this._emit('trackchange', {
889
- index: this._currentIndex,
890
- item
891
- });
892
- }
893
- return this;
894
- }
895
- playAt(index) {
896
- if (this._playlist.length > 0 && index >= 0 && index < this._playlist.length) {
897
- this._currentIndex = index;
898
- const item = this._playlist[this._currentIndex];
899
- this.load(item.src, item.poster);
900
- this.play();
901
- this._emit('trackchange', {
902
- index: this._currentIndex,
903
- item
904
- });
905
- }
906
- return this;
907
- }
908
-
909
- // Settings
910
- toggleSettings() {
911
- if (this.settingsPanel.classList.contains('plex-settings-open')) {
912
- this.closeSettings();
913
- } else {
914
- this.openSettings();
915
- }
916
- return this;
917
- }
918
- openSettings() {
919
- this.settingsPanel.classList.add('plex-settings-open');
920
- // Reset to main menu
921
- const content = this.settingsPanel.querySelector('.plex-settings-content');
922
- const speedMenu = this.settingsPanel.querySelector('.plex-settings-speed');
923
- if (content) content.style.display = 'block';
924
- if (speedMenu) speedMenu.style.display = 'none';
925
- return this;
926
- }
927
- closeSettings() {
928
- this.settingsPanel.classList.remove('plex-settings-open');
929
- return this;
930
- }
931
- _bindSettingsEvents() {
932
- if (!this.settingsPanel) return;
933
-
934
- // Close button
935
- const closeBtn = this.settingsPanel.querySelector('.plex-settings-close');
936
- closeBtn?.addEventListener('click', () => this.closeSettings());
937
-
938
- // Settings items
939
- const speedItem = this.settingsPanel.querySelector('[data-setting="speed"]');
940
- const speedMenu = this.settingsPanel.querySelector('.plex-settings-speed');
941
- const content = this.settingsPanel.querySelector('.plex-settings-content');
942
- const backBtn = this.settingsPanel.querySelector('.plex-settings-back');
943
- speedItem?.addEventListener('click', () => {
944
- if (content) content.style.display = 'none';
945
- if (speedMenu) speedMenu.style.display = 'block';
946
- });
947
- backBtn?.addEventListener('click', () => {
948
- if (content) content.style.display = 'block';
949
- if (speedMenu) speedMenu.style.display = 'none';
950
- });
951
-
952
- // Speed options
953
- const speedOptions = this.settingsPanel.querySelectorAll('.plex-speed-option');
954
- speedOptions.forEach(option => {
955
- option.addEventListener('click', () => {
956
- const speed = parseFloat(option.dataset.speed);
957
- this.setPlaybackRate(speed);
958
-
959
- // Update active state
960
- speedOptions.forEach(o => o.classList.remove('active'));
961
- option.classList.add('active');
962
-
963
- // Update display
964
- const speedValue = this.settingsPanel.querySelector('[data-setting="speed"] .plex-settings-value');
965
- if (speedValue) speedValue.textContent = `${speed}x`;
966
-
967
- // Go back to main menu
968
- if (content) content.style.display = 'block';
969
- if (speedMenu) speedMenu.style.display = 'none';
970
- });
971
- });
972
-
973
- // Close settings when clicking outside
974
- this.container.addEventListener('click', e => {
975
- if (!this.settingsPanel.contains(e.target) && !this.els.settingsBtn?.contains(e.target) && this.settingsPanel.classList.contains('plex-settings-open')) {
976
- this.closeSettings();
977
- }
978
- });
979
- }
980
- getState() {
981
- return {
982
- currentTime: this.video.currentTime,
983
- duration: this.video.duration || 0,
984
- volume: this.video.volume,
985
- muted: this.video.muted,
986
- isPlaying: !this.video.paused && !this.video.ended,
987
- isPaused: this.video.paused,
988
- isFullscreen: this._state.fullscreen,
989
- isPiP: this._state.pip,
990
- playbackRate: this.video.playbackRate,
991
- currentTrack: this._currentIndex,
992
- totalTracks: this._playlist.length
993
- };
994
- }
995
- getVideo() {
996
- return this.video;
997
- }
998
- destroy() {
999
- // Clear timeouts
1000
- clearTimeout(this._controlsTimeout);
1001
-
1002
- // Remove keyboard listener
1003
- if (this._keyHandler) {
1004
- document.removeEventListener('keydown', this._keyHandler);
1005
- }
1006
-
1007
- // Remove event listeners
1008
- this._eventListeners.clear();
1009
-
1010
- // Clear container
1011
- this.container.innerHTML = '';
1012
- this.container.classList.remove('plex-player');
1013
-
1014
- // Emit destroy event
1015
- this._emit('destroy');
1016
- }
1017
- }
1018
-
1019
- // Attach to window for UMD builds
1020
- if (typeof window !== 'undefined') {
1021
- window.PlexPlayer = PlexPlayer;
1022
- }
1023
-
1024
- exports.PlexPlayer = PlexPlayer;
1025
- exports.Utils = Utils;
1026
- exports.default = PlexPlayer;
1027
- //# sourceMappingURL=plex-player.cjs.js.map