document.addEventListener('DOMContentLoaded', function () { if (!window.location.pathname.includes('bohemia-afterdark-vault-videos')) return; let playlist = JSON.parse(localStorage.getItem('bvmtv_playlist') || '[]'); function savePlaylist() { localStorage.setItem('bvmtv_playlist', JSON.stringify(playlist)); } function updateButton() { const btn = document.getElementById('bvmtv-playlist-btn'); if (btn) btn.innerHTML = '🎵 My Playlist (' + playlist.length + ')'; savePlaylist(); } function createFloatingButton() { if (document.getElementById('bvmtv-playlist-btn')) return; const btn = document.createElement('div'); btn.id = 'bvmtv-playlist-btn'; btn.style.position = 'fixed'; btn.style.bottom = '20px'; btn.style.right = '20px'; btn.style.background = '#ff0055'; btn.style.color = '#fff'; btn.style.padding = '12px 18px'; btn.style.cursor = 'pointer'; btn.style.borderRadius = '6px'; btn.style.zIndex = '999999'; btn.style.fontWeight = '600'; btn.style.boxShadow = '0 5px 15px rgba(0,0,0,.5)'; btn.onclick = openPlaylistPanel; document.body.appendChild(btn); updateButton(); } function openPlaylistPanel() { if (document.getElementById('bvmtv-playlist-panel')) return; const panel = document.createElement('div'); panel.id = 'bvmtv-playlist-panel'; panel.style.position = 'fixed'; panel.style.top = '0'; panel.style.right = '0'; panel.style.width = '420px'; panel.style.height = '100%'; panel.style.background = '#000'; panel.style.color = '#fff'; panel.style.zIndex = '1000000'; panel.style.padding = '20px'; panel.style.overflowY = 'auto'; panel.innerHTML = '

My Playlist

' + '' + '
' + '' + ''; document.body.appendChild(panel); document.getElementById('bvmtv-close').onclick = function () { panel.remove(); }; document.getElementById('bvmtv-clear').onclick = function () { playlist = []; savePlaylist(); renderPlaylist(); updateButton(); }; renderPlaylist(); } function renderPlaylist() { const container = document.getElementById('bvmtv-playlist-items'); if (!container) return; container.innerHTML = ''; playlist.forEach(function (item, index) { const div = document.createElement('div'); div.style.marginBottom = '10px'; div.style.borderBottom = '1px solid #222'; div.style.paddingBottom = '8px'; div.innerHTML = '
▶ ' + item.title + '
' + ''; div.querySelector('div').onclick = function () { playVideo(index); }; div.querySelector('button').onclick = function () { playlist.splice(index, 1); savePlaylist(); renderPlaylist(); updateButton(); }; container.appendChild(div); }); } function playVideo(index) { const player = document.getElementById('bvmtv-player'); if (!player) return; player.pause(); player.src = playlist[index].url; player.load(); player.play(); player.onended = function () { if (playlist[index + 1]) playVideo(index + 1); }; } function findMainGrid() { return document.querySelector('ul.products') || document.querySelector('.woocommerce ul.products') || document.querySelector('.products'); } function attachAddButtons() { const mainGrid = findMainGrid(); if (!mainGrid) return; // Only direct children (no :scope, no >) const kids = Array.prototype.slice.call(mainGrid.children); const cards = kids.filter(function (el) { return el && el.matches && (el.matches('li.product') || el.matches('.product')); }); cards.forEach(function (card) { if (card.querySelector('.bvmtv-add')) return; const link = card.querySelector('a'); const titleEl = card.querySelector('h2, .woocommerce-loop-product__title'); if (!link || !titleEl) return; const btn = document.createElement('button'); btn.className = 'bvmtv-add'; btn.textContent = 'ADD TO PLAYLIST'; btn.style.width = '100%'; btn.style.marginTop = '8px'; btn.style.padding = '8px'; btn.style.background = '#ff0055'; btn.style.color = '#fff'; btn.style.border = 'none'; btn.style.cursor = 'pointer'; btn.style.fontWeight = '600'; btn.onclick = function () { fetch(link.href) .then(function (response) { return response.text(); }) .then(function (html) { const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html'); const video = doc.querySelector('video source') || doc.querySelector('video'); if (video) { playlist.push({ title: titleEl.textContent.trim(), url: video.src || video.getAttribute('src') }); savePlaylist(); updateButton(); btn.textContent = 'Added ✓'; } }); }; card.appendChild(btn); }); } createFloatingButton(); attachAddButtons(); });