@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,1033 +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
- (function (global, factory) {
9
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
10
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
11
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.PlexPlayer = {}));
12
- })(this, (function (exports) { 'use strict';
13
-
14
- /**
15
- * @frameset/plex-player - Core Entry Point
16
- * Professional Video Player by FRAMESET Studio
17
- * https://frameset.dev
18
- */
19
-
20
- /**
21
- * Utility Functions
22
- */
23
- const Utils = {
24
- formatTime(seconds) {
25
- if (isNaN(seconds) || !isFinite(seconds)) return '0:00';
26
- seconds = Math.max(0, Math.floor(seconds));
27
- const hours = Math.floor(seconds / 3600);
28
- const minutes = Math.floor(seconds % 3600 / 60);
29
- const secs = seconds % 60;
30
- if (hours > 0) {
31
- return `${hours}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
32
- }
33
- return `${minutes}:${secs.toString().padStart(2, '0')}`;
34
- },
35
- clamp(num, min, max) {
36
- return Math.min(Math.max(num, min), max);
37
- },
38
- throttle(fn, delay) {
39
- let lastCall = 0;
40
- return function (...args) {
41
- const now = Date.now();
42
- if (now - lastCall >= delay) {
43
- lastCall = now;
44
- return fn.apply(this, args);
45
- }
46
- };
47
- },
48
- debounce(fn, delay) {
49
- let timeoutId;
50
- return function (...args) {
51
- clearTimeout(timeoutId);
52
- timeoutId = setTimeout(() => fn.apply(this, args), delay);
53
- };
54
- }
55
- };
56
-
57
- /**
58
- * PlexPlayer - Main Player Class
59
- * A complete, standalone video player
60
- */
61
- class PlexPlayer {
62
- constructor(options = {}) {
63
- // Validate container
64
- if (!options.container) {
65
- throw new Error('PlexPlayer: container option is required');
66
- }
67
-
68
- // Get container element
69
- this.container = typeof options.container === 'string' ? document.querySelector(options.container) : options.container;
70
- if (!this.container) {
71
- throw new Error('PlexPlayer: container element not found');
72
- }
73
-
74
- // Store options with defaults
75
- this.options = {
76
- autoplay: false,
77
- muted: false,
78
- loop: false,
79
- volume: 1,
80
- poster: '',
81
- preload: 'metadata',
82
- keyboard: true,
83
- touch: true,
84
- pip: true,
85
- cast: true,
86
- fullscreen: true,
87
- controlsHideDelay: 3000,
88
- theme: {},
89
- subtitles: {},
90
- ads: {
91
- enabled: false
92
- },
93
- i18n: {},
94
- ...options
95
- };
96
-
97
- // Internationalization with defaults
98
- this.i18n = {
99
- play: 'Play',
100
- pause: 'Pause',
101
- mute: 'Mute',
102
- unmute: 'Unmute',
103
- fullscreen: 'Fullscreen',
104
- exitFullscreen: 'Exit Fullscreen',
105
- pip: 'Picture-in-Picture',
106
- exitPip: 'Exit Picture-in-Picture',
107
- settings: 'Settings',
108
- speed: 'Speed',
109
- quality: 'Quality',
110
- subtitles: 'Subtitles',
111
- off: 'Off',
112
- normal: 'Normal',
113
- captions: 'Captions',
114
- audio: 'Audio',
115
- volume: 'Volume',
116
- seekForward: 'Seek Forward',
117
- seekBackward: 'Seek Backward',
118
- ...this.options.i18n
119
- };
120
-
121
- // Internal state
122
- this._eventListeners = new Map();
123
- this._state = {
124
- playing: false,
125
- paused: true,
126
- muted: this.options.muted,
127
- volume: this.options.volume,
128
- currentTime: 0,
129
- duration: 0,
130
- buffered: 0,
131
- fullscreen: false,
132
- pip: false,
133
- playbackRate: 1
134
- };
135
-
136
- // Playlist state
137
- this._playlist = [];
138
- this._currentIndex = 0;
139
-
140
- // Controls timeout
141
- this._controlsTimeout = null;
142
-
143
- // Initialize player
144
- this._init();
145
- }
146
- _init() {
147
- // Add player class
148
- this.container.classList.add('plex-player');
149
- // Show controls initially
150
- this.container.classList.add('plex-controls-visible');
151
-
152
- // Apply theme
153
- this._applyTheme();
154
-
155
- // Create DOM structure
156
- this._createDOM();
157
-
158
- // Bind events
159
- this._bindEvents();
160
-
161
- // Initialize features
162
- if (this.options.keyboard) this._initKeyboard();
163
- if (this.options.touch) this._initTouch();
164
- if (this.options.pip) this._initPiP();
165
- if (this.options.cast) this._initCast();
166
-
167
- // Emit ready event
168
- setTimeout(() => this._emit('ready'), 0);
169
- }
170
- _applyTheme() {
171
- const {
172
- theme
173
- } = this.options;
174
- if (theme.primary) this.container.style.setProperty('--plex-primary', theme.primary);
175
- if (theme.background) this.container.style.setProperty('--plex-bg', theme.background);
176
- if (theme.text) this.container.style.setProperty('--plex-text', theme.text);
177
- if (theme.borderRadius) this.container.style.setProperty('--plex-border-radius', theme.borderRadius);
178
- }
179
- _createDOM() {
180
- // Video element
181
- this.video = document.createElement('video');
182
- this.video.className = 'plex-video';
183
- this.video.setAttribute('playsinline', '');
184
- this.video.setAttribute('webkit-playsinline', '');
185
- this.video.preload = this.options.preload;
186
- this.video.muted = this.options.muted;
187
- this.video.volume = this.options.volume;
188
- this.video.loop = this.options.loop;
189
- if (this.options.poster) this.video.poster = this.options.poster;
190
- this.container.appendChild(this.video);
191
-
192
- // Controls overlay
193
- this.controls = document.createElement('div');
194
- this.controls.className = 'plex-controls';
195
- this.controls.innerHTML = this._getControlsHTML();
196
- this.container.appendChild(this.controls);
197
-
198
- // Cache control elements
199
- this._cacheElements();
200
-
201
- // Big play button
202
- this.bigPlayBtn = document.createElement('div');
203
- this.bigPlayBtn.className = 'plex-big-play';
204
- this.bigPlayBtn.innerHTML = `<svg viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>`;
205
- this.container.appendChild(this.bigPlayBtn);
206
-
207
- // Loading spinner
208
- this.loader = document.createElement('div');
209
- this.loader.className = 'plex-loader';
210
- this.loader.innerHTML = '<div class="plex-spinner"></div>';
211
- this.container.appendChild(this.loader);
212
-
213
- // Settings panel
214
- this.settingsPanel = document.createElement('div');
215
- this.settingsPanel.className = 'plex-settings-panel';
216
- this.settingsPanel.innerHTML = this._getSettingsHTML();
217
- this.container.appendChild(this.settingsPanel);
218
- }
219
- _getSettingsHTML() {
220
- const i18n = this.i18n;
221
- return `
222
- <div class="plex-settings-header">
223
- <span class="plex-settings-title">${i18n.settings || 'Settings'}</span>
224
- <button class="plex-settings-close">
225
- <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>
226
- </button>
227
- </div>
228
- <div class="plex-settings-content">
229
- <div class="plex-settings-item" data-setting="speed">
230
- <span class="plex-settings-label">${i18n.speed || 'Speed'}</span>
231
- <span class="plex-settings-value">1x</span>
232
- </div>
233
- <div class="plex-settings-item" data-setting="quality">
234
- <span class="plex-settings-label">${i18n.quality || 'Quality'}</span>
235
- <span class="plex-settings-value">Auto</span>
236
- </div>
237
- <div class="plex-settings-item" data-setting="subtitles">
238
- <span class="plex-settings-label">${i18n.subtitles || 'Subtitles'}</span>
239
- <span class="plex-settings-value">${i18n.off || 'Off'}</span>
240
- </div>
241
- </div>
242
- <div class="plex-settings-submenu plex-settings-speed" style="display: none;">
243
- <div class="plex-settings-back">
244
- <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>
245
- <span>${i18n.speed || 'Speed'}</span>
246
- </div>
247
- <div class="plex-speed-options">
248
- <button class="plex-speed-option" data-speed="0.25">0.25x</button>
249
- <button class="plex-speed-option" data-speed="0.5">0.5x</button>
250
- <button class="plex-speed-option" data-speed="0.75">0.75x</button>
251
- <button class="plex-speed-option active" data-speed="1">1x</button>
252
- <button class="plex-speed-option" data-speed="1.25">1.25x</button>
253
- <button class="plex-speed-option" data-speed="1.5">1.5x</button>
254
- <button class="plex-speed-option" data-speed="1.75">1.75x</button>
255
- <button class="plex-speed-option" data-speed="2">2x</button>
256
- </div>
257
- </div>
258
- `;
259
- }
260
- _getControlsHTML() {
261
- const i18n = this.i18n;
262
- return `
263
- <div class="plex-progress-container">
264
- <div class="plex-progress-bar">
265
- <div class="plex-progress-buffered"></div>
266
- <div class="plex-progress-played"></div>
267
- <div class="plex-progress-handle"></div>
268
- </div>
269
- <div class="plex-progress-preview">
270
- <div class="plex-preview-time">0:00</div>
271
- </div>
272
- </div>
273
- <div class="plex-controls-bar">
274
- <div class="plex-controls-left">
275
- <button class="plex-btn plex-play-btn" title="${i18n.play || 'Play'}">
276
- <svg class="plex-icon-play" viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>
277
- <svg class="plex-icon-pause" viewBox="0 0 24 24"><path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/></svg>
278
- </button>
279
- <button class="plex-btn plex-prev-btn" title="${i18n.previous || 'Previous'}">
280
- <svg viewBox="0 0 24 24"><path d="M6 6h2v12H6zm3.5 6l8.5 6V6z"/></svg>
281
- </button>
282
- <button class="plex-btn plex-next-btn" title="${i18n.next || 'Next'}">
283
- <svg viewBox="0 0 24 24"><path d="M6 18l8.5-6L6 6v12zM16 6v12h2V6h-2z"/></svg>
284
- </button>
285
- <div class="plex-volume-container">
286
- <button class="plex-btn plex-volume-btn" title="${i18n.mute || 'Mute'}">
287
- <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>
288
- <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>
289
- <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>
290
- </button>
291
- <div class="plex-volume-slider">
292
- <div class="plex-volume-track">
293
- <div class="plex-volume-level"></div>
294
- <div class="plex-volume-handle"></div>
295
- </div>
296
- </div>
297
- </div>
298
- <div class="plex-time">
299
- <span class="plex-time-current">0:00</span>
300
- <span class="plex-time-separator">/</span>
301
- <span class="plex-time-duration">0:00</span>
302
- </div>
303
- </div>
304
- <div class="plex-controls-right">
305
- <button class="plex-btn plex-settings-btn" title="${i18n.settings || 'Settings'}">
306
- <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>
307
- </button>
308
- ${this.options.pip ? `
309
- <button class="plex-btn plex-pip-btn" title="${i18n.pip || 'Picture-in-Picture'}">
310
- <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>
311
- </button>
312
- ` : ''}
313
- ${this.options.cast ? `
314
- <button class="plex-btn plex-cast-btn" title="${i18n.cast || 'Cast'}">
315
- <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>
316
- </button>
317
- ` : ''}
318
- ${this.options.fullscreen ? `
319
- <button class="plex-btn plex-fullscreen-btn" title="${i18n.fullscreen || 'Fullscreen'}">
320
- <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>
321
- <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>
322
- </button>
323
- ` : ''}
324
- </div>
325
- </div>
326
- `;
327
- }
328
- _cacheElements() {
329
- const $ = sel => this.controls.querySelector(sel);
330
- this.els = {
331
- progressContainer: $('.plex-progress-container'),
332
- progressBar: $('.plex-progress-bar'),
333
- progressBuffered: $('.plex-progress-buffered'),
334
- progressPlayed: $('.plex-progress-played'),
335
- progressHandle: $('.plex-progress-handle'),
336
- progressPreview: $('.plex-progress-preview'),
337
- previewTime: $('.plex-preview-time'),
338
- playBtn: $('.plex-play-btn'),
339
- prevBtn: $('.plex-prev-btn'),
340
- nextBtn: $('.plex-next-btn'),
341
- volumeBtn: $('.plex-volume-btn'),
342
- volumeSlider: $('.plex-volume-slider'),
343
- volumeTrack: $('.plex-volume-track'),
344
- volumeLevel: $('.plex-volume-level'),
345
- volumeHandle: $('.plex-volume-handle'),
346
- timeCurrent: $('.plex-time-current'),
347
- timeDuration: $('.plex-time-duration'),
348
- settingsBtn: $('.plex-settings-btn'),
349
- pipBtn: $('.plex-pip-btn'),
350
- castBtn: $('.plex-cast-btn'),
351
- fullscreenBtn: $('.plex-fullscreen-btn')
352
- };
353
- }
354
- _bindEvents() {
355
- // Video events
356
- this.video.addEventListener('play', () => this._onPlay());
357
- this.video.addEventListener('pause', () => this._onPause());
358
- this.video.addEventListener('ended', () => this._onEnded());
359
- this.video.addEventListener('timeupdate', () => this._onTimeUpdate());
360
- this.video.addEventListener('progress', () => this._onProgress());
361
- this.video.addEventListener('loadedmetadata', () => this._onLoadedMetadata());
362
- this.video.addEventListener('volumechange', () => this._onVolumeChange());
363
- this.video.addEventListener('waiting', () => this._onWaiting());
364
- this.video.addEventListener('canplay', () => this._onCanPlay());
365
- this.video.addEventListener('error', e => this._onError(e));
366
-
367
- // Control events
368
- this.bigPlayBtn.addEventListener('click', () => this.togglePlay());
369
- this.video.addEventListener('click', () => this.togglePlay());
370
- this.els.playBtn?.addEventListener('click', () => this.togglePlay());
371
- this.els.prevBtn?.addEventListener('click', () => this.previous());
372
- this.els.nextBtn?.addEventListener('click', () => this.next());
373
- this.els.volumeBtn?.addEventListener('click', () => this.toggleMute());
374
- this.els.pipBtn?.addEventListener('click', () => this.togglePiP());
375
- this.els.fullscreenBtn?.addEventListener('click', () => this.toggleFullscreen());
376
- this.els.settingsBtn?.addEventListener('click', () => this.toggleSettings());
377
- this.els.castBtn?.addEventListener('click', () => this.cast());
378
-
379
- // Settings panel events
380
- this._bindSettingsEvents();
381
-
382
- // Progress bar
383
- this._bindProgressEvents();
384
-
385
- // Volume slider
386
- this._bindVolumeEvents();
387
-
388
- // Controls visibility
389
- this._bindControlsVisibility();
390
-
391
- // Fullscreen change
392
- document.addEventListener('fullscreenchange', () => this._onFullscreenChange());
393
- document.addEventListener('webkitfullscreenchange', () => this._onFullscreenChange());
394
- }
395
- _bindProgressEvents() {
396
- const progress = this.els.progressContainer;
397
- if (!progress) return;
398
- let isDragging = false;
399
- const seek = e => {
400
- const rect = this.els.progressBar.getBoundingClientRect();
401
- const percent = Utils.clamp((e.clientX - rect.left) / rect.width, 0, 1);
402
- this.seekPercent(percent * 100);
403
- };
404
- const updatePreview = e => {
405
- const rect = this.els.progressBar.getBoundingClientRect();
406
- const percent = Utils.clamp((e.clientX - rect.left) / rect.width, 0, 1);
407
- const time = percent * this.video.duration;
408
- if (this.els.previewTime) {
409
- this.els.previewTime.textContent = Utils.formatTime(time);
410
- }
411
- if (this.els.progressPreview) {
412
- const left = Utils.clamp(e.clientX - rect.left, 30, rect.width - 30);
413
- this.els.progressPreview.style.left = `${left}px`;
414
- }
415
- };
416
- progress.addEventListener('mousedown', e => {
417
- isDragging = true;
418
- seek(e);
419
- });
420
- progress.addEventListener('mousemove', e => {
421
- updatePreview(e);
422
- if (isDragging) seek(e);
423
- });
424
- document.addEventListener('mouseup', () => {
425
- isDragging = false;
426
- });
427
- document.addEventListener('mousemove', e => {
428
- if (isDragging) seek(e);
429
- });
430
- }
431
- _bindVolumeEvents() {
432
- const volumeTrack = this.els.volumeTrack;
433
- if (!volumeTrack) return;
434
- let isDragging = false;
435
- const setVolume = e => {
436
- const rect = volumeTrack.getBoundingClientRect();
437
- const percent = Utils.clamp((e.clientX - rect.left) / rect.width, 0, 1);
438
- this.setVolume(percent);
439
- };
440
- volumeTrack.addEventListener('mousedown', e => {
441
- isDragging = true;
442
- setVolume(e);
443
- });
444
- document.addEventListener('mousemove', e => {
445
- if (isDragging) setVolume(e);
446
- });
447
- document.addEventListener('mouseup', () => {
448
- isDragging = false;
449
- });
450
- }
451
- _bindControlsVisibility() {
452
- const showControls = () => {
453
- this.container.classList.add('plex-controls-visible');
454
- clearTimeout(this._controlsTimeout);
455
- if (this._state.playing) {
456
- this._controlsTimeout = setTimeout(() => {
457
- this.container.classList.remove('plex-controls-visible');
458
- }, this.options.controlsHideDelay);
459
- }
460
- };
461
- this.container.addEventListener('mousemove', showControls);
462
- this.container.addEventListener('mouseenter', showControls);
463
- this.container.addEventListener('mouseleave', () => {
464
- if (this._state.playing) {
465
- this.container.classList.remove('plex-controls-visible');
466
- }
467
- });
468
-
469
- // Always show when paused
470
- this.video.addEventListener('pause', showControls);
471
- }
472
-
473
- // Event handlers
474
- _onPlay() {
475
- this._state.playing = true;
476
- this._state.paused = false;
477
- this.container.classList.add('plex-playing');
478
- this.container.classList.remove('plex-paused');
479
- this.bigPlayBtn.style.display = 'none';
480
- this._emit('play');
481
- }
482
- _onPause() {
483
- this._state.playing = false;
484
- this._state.paused = true;
485
- this.container.classList.remove('plex-playing');
486
- this.container.classList.add('plex-paused');
487
- this.bigPlayBtn.style.display = '';
488
- this._emit('pause');
489
- }
490
- _onEnded() {
491
- this._state.playing = false;
492
- this.container.classList.remove('plex-playing');
493
-
494
- // Auto-play next if playlist
495
- if (this._playlist.length > 0 && this._currentIndex < this._playlist.length - 1) {
496
- this.next();
497
- } else {
498
- this._emit('ended');
499
- }
500
- }
501
- _onTimeUpdate() {
502
- this._state.currentTime = this.video.currentTime;
503
- const percent = this.video.currentTime / this.video.duration * 100;
504
- if (this.els.progressPlayed) {
505
- this.els.progressPlayed.style.width = `${percent}%`;
506
- }
507
- if (this.els.progressHandle) {
508
- this.els.progressHandle.style.left = `${percent}%`;
509
- }
510
- if (this.els.timeCurrent) {
511
- this.els.timeCurrent.textContent = Utils.formatTime(this.video.currentTime);
512
- }
513
- this._emit('timeupdate', {
514
- currentTime: this.video.currentTime,
515
- duration: this.video.duration
516
- });
517
- }
518
- _onProgress() {
519
- const buffered = this.video.buffered;
520
- if (buffered.length > 0) {
521
- const percent = buffered.end(buffered.length - 1) / this.video.duration * 100;
522
- this._state.buffered = percent;
523
- if (this.els.progressBuffered) {
524
- this.els.progressBuffered.style.width = `${percent}%`;
525
- }
526
- this._emit('progress', {
527
- buffered: percent
528
- });
529
- }
530
- }
531
- _onLoadedMetadata() {
532
- this._state.duration = this.video.duration;
533
- if (this.els.timeDuration) {
534
- this.els.timeDuration.textContent = Utils.formatTime(this.video.duration);
535
- }
536
- this._emit('loadedmetadata', {
537
- duration: this.video.duration
538
- });
539
- }
540
- _onVolumeChange() {
541
- this._state.volume = this.video.volume;
542
- this._state.muted = this.video.muted;
543
- const volume = this.video.muted ? 0 : this.video.volume;
544
-
545
- // Update volume UI
546
- if (this.els.volumeLevel) {
547
- this.els.volumeLevel.style.width = `${volume * 100}%`;
548
- }
549
- if (this.els.volumeHandle) {
550
- this.els.volumeHandle.style.left = `${volume * 100}%`;
551
- }
552
-
553
- // Update icon
554
- this.container.classList.remove('plex-muted', 'plex-volume-low');
555
- if (this.video.muted || volume === 0) {
556
- this.container.classList.add('plex-muted');
557
- } else if (volume < 0.5) {
558
- this.container.classList.add('plex-volume-low');
559
- }
560
- this._emit('volumechange', {
561
- volume: this.video.volume,
562
- muted: this.video.muted
563
- });
564
- }
565
- _onWaiting() {
566
- this.container.classList.add('plex-loading');
567
- this._emit('waiting');
568
- }
569
- _onCanPlay() {
570
- this.container.classList.remove('plex-loading');
571
- this._emit('canplay');
572
- }
573
- _onError(e) {
574
- this._emit('error', {
575
- code: this.video.error?.code,
576
- message: this.video.error?.message
577
- });
578
- }
579
- _onFullscreenChange() {
580
- const isFullscreen = !!(document.fullscreenElement || document.webkitFullscreenElement);
581
- this._state.fullscreen = isFullscreen;
582
- if (isFullscreen) {
583
- this.container.classList.add('plex-fullscreen');
584
- } else {
585
- this.container.classList.remove('plex-fullscreen');
586
- }
587
- this._emit('fullscreenchange', {
588
- isFullscreen
589
- });
590
- }
591
-
592
- // Keyboard support
593
- _initKeyboard() {
594
- this._keyHandler = e => {
595
- if (!this.container.contains(document.activeElement) && document.activeElement !== document.body) return;
596
- switch (e.key) {
597
- case ' ':
598
- case 'k':
599
- e.preventDefault();
600
- this.togglePlay();
601
- break;
602
- case 'ArrowLeft':
603
- e.preventDefault();
604
- this.seek(this.video.currentTime - 10);
605
- break;
606
- case 'ArrowRight':
607
- e.preventDefault();
608
- this.seek(this.video.currentTime + 10);
609
- break;
610
- case 'ArrowUp':
611
- e.preventDefault();
612
- this.setVolume(Math.min(1, this.video.volume + 0.1));
613
- break;
614
- case 'ArrowDown':
615
- e.preventDefault();
616
- this.setVolume(Math.max(0, this.video.volume - 0.1));
617
- break;
618
- case 'm':
619
- this.toggleMute();
620
- break;
621
- case 'f':
622
- this.toggleFullscreen();
623
- break;
624
- case 'p':
625
- this.togglePiP();
626
- break;
627
- case 'n':
628
- if (e.shiftKey) {
629
- this.previous();
630
- } else {
631
- this.next();
632
- }
633
- break;
634
- }
635
- };
636
- document.addEventListener('keydown', this._keyHandler);
637
- }
638
-
639
- // Touch support
640
- _initTouch() {
641
- let touchStartX = 0;
642
- let touchStartY = 0;
643
- let touchStartTime = 0;
644
- this.video.addEventListener('touchstart', e => {
645
- touchStartX = e.touches[0].clientX;
646
- touchStartY = e.touches[0].clientY;
647
- touchStartTime = Date.now();
648
- });
649
- this.video.addEventListener('touchend', e => {
650
- const touchEndX = e.changedTouches[0].clientX;
651
- const touchEndY = e.changedTouches[0].clientY;
652
- const touchDuration = Date.now() - touchStartTime;
653
- const deltaX = touchEndX - touchStartX;
654
- const deltaY = touchEndY - touchStartY;
655
-
656
- // Tap to toggle play (if quick tap)
657
- if (Math.abs(deltaX) < 30 && Math.abs(deltaY) < 30 && touchDuration < 300) {
658
- this.togglePlay();
659
- }
660
- // Swipe to seek
661
- else if (Math.abs(deltaX) > 50 && Math.abs(deltaY) < 30) {
662
- if (deltaX > 0) {
663
- this.seek(this.video.currentTime + 10);
664
- } else {
665
- this.seek(this.video.currentTime - 10);
666
- }
667
- }
668
- });
669
- }
670
-
671
- // PiP support
672
- _initPiP() {
673
- if (!document.pictureInPictureEnabled) {
674
- if (this.els.pipBtn) {
675
- this.els.pipBtn.style.display = 'none';
676
- }
677
- return;
678
- }
679
- this.video.addEventListener('enterpictureinpicture', () => {
680
- this._state.pip = true;
681
- this.container.classList.add('plex-pip');
682
- this._emit('enterpip');
683
- });
684
- this.video.addEventListener('leavepictureinpicture', () => {
685
- this._state.pip = false;
686
- this.container.classList.remove('plex-pip');
687
- this._emit('leavepip');
688
- });
689
- }
690
-
691
- // Cast support
692
- _initCast() {
693
- // Chromecast requires HTTPS and specific browser
694
- const isChrome = /Chrome/.test(navigator.userAgent) && !/Edge/.test(navigator.userAgent);
695
- const isHTTPS = location.protocol === 'https:' || location.hostname === 'localhost';
696
- if (!isChrome || !isHTTPS) {
697
- if (this.els.castBtn) {
698
- this.els.castBtn.style.display = 'none';
699
- }
700
- return;
701
- }
702
-
703
- // Load Cast SDK if available
704
- if (window.chrome && window.chrome.cast) {
705
- this._initCastApi();
706
- } else {
707
- window['__onGCastApiAvailable'] = isAvailable => {
708
- if (isAvailable) this._initCastApi();
709
- };
710
- }
711
- }
712
- _initCastApi() {
713
- if (this.els.castBtn) {
714
- this.els.castBtn.addEventListener('click', () => {
715
- if (window.cast && window.cast.framework) {
716
- const context = cast.framework.CastContext.getInstance();
717
- context.requestSession();
718
- }
719
- });
720
- }
721
- }
722
-
723
- // Event system
724
- on(event, callback) {
725
- if (!this._eventListeners.has(event)) {
726
- this._eventListeners.set(event, new Set());
727
- }
728
- this._eventListeners.get(event).add(callback);
729
- return this;
730
- }
731
- off(event, callback) {
732
- if (this._eventListeners.has(event)) {
733
- this._eventListeners.get(event).delete(callback);
734
- }
735
- return this;
736
- }
737
- _emit(event, data = {}) {
738
- if (this._eventListeners.has(event)) {
739
- this._eventListeners.get(event).forEach(callback => {
740
- try {
741
- callback(data);
742
- } catch (e) {
743
- console.error(`PlexPlayer: Error in ${event} handler:`, e);
744
- }
745
- });
746
- }
747
- }
748
-
749
- // Public API
750
- load(src, poster) {
751
- this.video.src = src;
752
- if (poster) this.video.poster = poster;
753
- this.video.load();
754
- if (this.options.autoplay) {
755
- this.play();
756
- }
757
- return this;
758
- }
759
- loadPlaylist(items) {
760
- this._playlist = items.map((item, index) => ({
761
- ...item,
762
- index
763
- }));
764
- this._currentIndex = 0;
765
- if (this._playlist.length > 0) {
766
- const first = this._playlist[0];
767
- this.load(first.src, first.poster);
768
- this._emit('playlistload', {
769
- playlist: this._playlist
770
- });
771
- }
772
- return this;
773
- }
774
- play() {
775
- return this.video.play();
776
- }
777
- pause() {
778
- this.video.pause();
779
- return this;
780
- }
781
- togglePlay() {
782
- if (this.video.paused) {
783
- this.play();
784
- } else {
785
- this.pause();
786
- }
787
- return this;
788
- }
789
- seek(time) {
790
- this.video.currentTime = Utils.clamp(time, 0, this.video.duration || 0);
791
- return this;
792
- }
793
- seekPercent(percent) {
794
- const time = percent / 100 * this.video.duration;
795
- return this.seek(time);
796
- }
797
- setVolume(level) {
798
- this.video.volume = Utils.clamp(level, 0, 1);
799
- if (this.video.muted && level > 0) {
800
- this.video.muted = false;
801
- }
802
- return this;
803
- }
804
- getVolume() {
805
- return this.video.volume;
806
- }
807
- mute() {
808
- this.video.muted = true;
809
- return this;
810
- }
811
- unmute() {
812
- this.video.muted = false;
813
- return this;
814
- }
815
- toggleMute() {
816
- this.video.muted = !this.video.muted;
817
- return this;
818
- }
819
- setPlaybackRate(rate) {
820
- this.video.playbackRate = rate;
821
- this._state.playbackRate = rate;
822
- this._emit('ratechange', {
823
- rate
824
- });
825
- return this;
826
- }
827
- getPlaybackRate() {
828
- return this.video.playbackRate;
829
- }
830
- enterFullscreen() {
831
- const el = this.container;
832
- if (el.requestFullscreen) {
833
- return el.requestFullscreen();
834
- } else if (el.webkitRequestFullscreen) {
835
- return el.webkitRequestFullscreen();
836
- }
837
- }
838
- exitFullscreen() {
839
- if (document.exitFullscreen) {
840
- return document.exitFullscreen();
841
- } else if (document.webkitExitFullscreen) {
842
- return document.webkitExitFullscreen();
843
- }
844
- }
845
- toggleFullscreen() {
846
- if (this._state.fullscreen) {
847
- this.exitFullscreen();
848
- } else {
849
- this.enterFullscreen();
850
- }
851
- return this;
852
- }
853
- enterPiP() {
854
- if (document.pictureInPictureEnabled && this.video !== document.pictureInPictureElement) {
855
- return this.video.requestPictureInPicture();
856
- }
857
- }
858
- exitPiP() {
859
- if (document.pictureInPictureElement) {
860
- return document.exitPictureInPicture();
861
- }
862
- }
863
- togglePiP() {
864
- if (this._state.pip) {
865
- this.exitPiP();
866
- } else {
867
- this.enterPiP();
868
- }
869
- return this;
870
- }
871
- next() {
872
- if (this._playlist.length > 0 && this._currentIndex < this._playlist.length - 1) {
873
- this._currentIndex++;
874
- const item = this._playlist[this._currentIndex];
875
- this.load(item.src, item.poster);
876
- this.play();
877
- this._emit('trackchange', {
878
- index: this._currentIndex,
879
- item
880
- });
881
- }
882
- return this;
883
- }
884
- previous() {
885
- if (this._playlist.length > 0 && this._currentIndex > 0) {
886
- this._currentIndex--;
887
- const item = this._playlist[this._currentIndex];
888
- this.load(item.src, item.poster);
889
- this.play();
890
- this._emit('trackchange', {
891
- index: this._currentIndex,
892
- item
893
- });
894
- }
895
- return this;
896
- }
897
- playAt(index) {
898
- if (this._playlist.length > 0 && index >= 0 && index < this._playlist.length) {
899
- this._currentIndex = index;
900
- const item = this._playlist[this._currentIndex];
901
- this.load(item.src, item.poster);
902
- this.play();
903
- this._emit('trackchange', {
904
- index: this._currentIndex,
905
- item
906
- });
907
- }
908
- return this;
909
- }
910
-
911
- // Settings
912
- toggleSettings() {
913
- if (this.settingsPanel.classList.contains('plex-settings-open')) {
914
- this.closeSettings();
915
- } else {
916
- this.openSettings();
917
- }
918
- return this;
919
- }
920
- openSettings() {
921
- this.settingsPanel.classList.add('plex-settings-open');
922
- // Reset to main menu
923
- const content = this.settingsPanel.querySelector('.plex-settings-content');
924
- const speedMenu = this.settingsPanel.querySelector('.plex-settings-speed');
925
- if (content) content.style.display = 'block';
926
- if (speedMenu) speedMenu.style.display = 'none';
927
- return this;
928
- }
929
- closeSettings() {
930
- this.settingsPanel.classList.remove('plex-settings-open');
931
- return this;
932
- }
933
- _bindSettingsEvents() {
934
- if (!this.settingsPanel) return;
935
-
936
- // Close button
937
- const closeBtn = this.settingsPanel.querySelector('.plex-settings-close');
938
- closeBtn?.addEventListener('click', () => this.closeSettings());
939
-
940
- // Settings items
941
- const speedItem = this.settingsPanel.querySelector('[data-setting="speed"]');
942
- const speedMenu = this.settingsPanel.querySelector('.plex-settings-speed');
943
- const content = this.settingsPanel.querySelector('.plex-settings-content');
944
- const backBtn = this.settingsPanel.querySelector('.plex-settings-back');
945
- speedItem?.addEventListener('click', () => {
946
- if (content) content.style.display = 'none';
947
- if (speedMenu) speedMenu.style.display = 'block';
948
- });
949
- backBtn?.addEventListener('click', () => {
950
- if (content) content.style.display = 'block';
951
- if (speedMenu) speedMenu.style.display = 'none';
952
- });
953
-
954
- // Speed options
955
- const speedOptions = this.settingsPanel.querySelectorAll('.plex-speed-option');
956
- speedOptions.forEach(option => {
957
- option.addEventListener('click', () => {
958
- const speed = parseFloat(option.dataset.speed);
959
- this.setPlaybackRate(speed);
960
-
961
- // Update active state
962
- speedOptions.forEach(o => o.classList.remove('active'));
963
- option.classList.add('active');
964
-
965
- // Update display
966
- const speedValue = this.settingsPanel.querySelector('[data-setting="speed"] .plex-settings-value');
967
- if (speedValue) speedValue.textContent = `${speed}x`;
968
-
969
- // Go back to main menu
970
- if (content) content.style.display = 'block';
971
- if (speedMenu) speedMenu.style.display = 'none';
972
- });
973
- });
974
-
975
- // Close settings when clicking outside
976
- this.container.addEventListener('click', e => {
977
- if (!this.settingsPanel.contains(e.target) && !this.els.settingsBtn?.contains(e.target) && this.settingsPanel.classList.contains('plex-settings-open')) {
978
- this.closeSettings();
979
- }
980
- });
981
- }
982
- getState() {
983
- return {
984
- currentTime: this.video.currentTime,
985
- duration: this.video.duration || 0,
986
- volume: this.video.volume,
987
- muted: this.video.muted,
988
- isPlaying: !this.video.paused && !this.video.ended,
989
- isPaused: this.video.paused,
990
- isFullscreen: this._state.fullscreen,
991
- isPiP: this._state.pip,
992
- playbackRate: this.video.playbackRate,
993
- currentTrack: this._currentIndex,
994
- totalTracks: this._playlist.length
995
- };
996
- }
997
- getVideo() {
998
- return this.video;
999
- }
1000
- destroy() {
1001
- // Clear timeouts
1002
- clearTimeout(this._controlsTimeout);
1003
-
1004
- // Remove keyboard listener
1005
- if (this._keyHandler) {
1006
- document.removeEventListener('keydown', this._keyHandler);
1007
- }
1008
-
1009
- // Remove event listeners
1010
- this._eventListeners.clear();
1011
-
1012
- // Clear container
1013
- this.container.innerHTML = '';
1014
- this.container.classList.remove('plex-player');
1015
-
1016
- // Emit destroy event
1017
- this._emit('destroy');
1018
- }
1019
- }
1020
-
1021
- // Attach to window for UMD builds
1022
- if (typeof window !== 'undefined') {
1023
- window.PlexPlayer = PlexPlayer;
1024
- }
1025
-
1026
- exports.PlexPlayer = PlexPlayer;
1027
- exports.Utils = Utils;
1028
- exports.default = PlexPlayer;
1029
-
1030
- Object.defineProperty(exports, '__esModule', { value: true });
1031
-
1032
- }));
1033
- //# sourceMappingURL=plex-player.js.map