Spaces:
Running
Running
File size: 8,962 Bytes
9164723 ef0774a 9164723 ef0774a 9164723 ef0774a 9164723 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | document.addEventListener('DOMContentLoaded', async function() {
const player = document.getElementById('player');
const playerOverlay = document.getElementById('player-overlay');
const channelList = document.getElementById('channel-list');
const currentChannel = document.getElementById('current-channel');
const currentProgram = document.getElementById('current-program');
// Default M3U URL
const m3uUrl = 'https://iptv-org.github.io/iptv/languages/mal.m3u';
playerOverlay.classList.remove('hidden');
// Load channels from M3U file
async function loadChannels() {
try {
const response = await fetch(m3uUrl);
const text = await response.text();
const channels = parseM3U(text);
channelList.innerHTML = '';
channels.forEach(channel => {
const channelItem = document.createElement('div');
channelItem.className = 'channel-item bg-gray-700 hover:bg-gray-600 rounded-lg p-3 cursor-pointer flex items-center transition-all';
channelItem.innerHTML = `
<div class="w-10 h-10 rounded-full bg-purple-500 flex items-center justify-center mr-3">
<i data-feather="tv" class="w-4 h-4"></i>
</div>
<div class="flex-1">
<h3 class="font-medium truncate">${channel.name}</h3>
<p class="text-gray-400 text-xs truncate">${channel.group || 'General'}</p>
</div>
`;
channelItem.addEventListener('click', () => {
playChannel(channel);
});
channelList.appendChild(channelItem);
});
feather.replace();
} catch (error) {
console.error('Error loading channels:', error);
channelList.innerHTML = `
<div class="text-center py-8 text-gray-400">
<i data-feather="alert-circle" class="w-12 h-12 mx-auto mb-2"></i>
<p>Failed to load channels. Please try again later.</p>
</div>
`;
feather.replace();
}
}
// Parse M3U file content
function parseM3U(content) {
const lines = content.split('\n');
const channels = [];
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith('#EXTINF:')) {
const infoLine = lines[i];
const urlLine = lines[i + 1];
if (urlLine && !urlLine.startsWith('#')) {
const channel = {
name: extractName(infoLine),
group: extractGroup(infoLine),
url: urlLine.trim()
};
channels.push(channel);
i++;
}
}
}
return channels;
}
// Extract channel name from EXTINF line
function extractName(extinfLine) {
const match = extinfLine.match(/tvg-name="([^"]*)"/i);
if (match && match[1]) {
return match[1];
}
// Fallback to last part after comma
const parts = extinfLine.split(',');
return parts[parts.length - 1].trim();
}
// Extract channel group from EXTINF line
function extractGroup(extinfLine) {
const match = extinfLine.match(/group-title="([^"]*)"/i);
return match && match[1] ? match[1] : null;
}
// Play selected channel
function playChannel(channel) {
playerOverlay.classList.add('hidden');
currentChannel.textContent = channel.name;
currentProgram.textContent = channel.group ? `Category: ${channel.group}` : 'Live Stream';
player.pause();
// Check if HLS.js is needed
if (channel.url.endsWith('.m3u8')) {
if (typeof Hls === 'undefined') {
loadHlsJs().then(() => {
setupHlsPlayer(channel.url);
});
} else {
setupHlsPlayer(channel.url);
}
} else {
// Direct video source
player.src = channel.url;
player.play();
}
// Highlight selected channel
const channelItems = document.querySelectorAll('.channel-item');
channelItems.forEach(item => {
item.classList.remove('bg-rose-500', 'text-white');
item.classList.add('bg-gray-700', 'hover:bg-gray-600');
});
// Find and highlight the clicked channel
const selectedChannel = [...channelItems].find(item =>
item.querySelector('h3').textContent === channel.name
);
if (selectedChannel) {
selectedChannel.classList.remove('bg-gray-700', 'hover:bg-gray-600');
selectedChannel.classList.add('bg-rose-500', 'text-white');
}
}
// Load HLS.js dynamically
function loadHlsJs() {
return new Promise((resolve, reject) => {
if (typeof Hls !== 'undefined') {
resolve();
return;
}
const script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/hls.js@latest';
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
// Setup HLS player
function setupHlsPlayer(url) {
if (Hls.isSupported()) {
const hls = new Hls();
hls.loadSource(url);
hls.attachMedia(player);
hls.on(Hls.Events.MANIFEST_PARSED, function() {
player.play();
});
} else if (player.canPlayType('application/vnd.apple.mpegurl')) {
// For Safari
player.src = url;
player.play();
} else {
alert('Error: Your browser does not support HLS streaming.');
}
}
// Initialize the app
loadChannels();
// Handle volume change from player controls
document.addEventListener('volumechange', (e) => {
player.volume = e.detail.volume;
});
// Handle play/pause from player controls
document.addEventListener('playpause', () => {
if (player.paused) {
player.play();
} else {
player.pause();
}
});
// Handle fullscreen from player controls
document.addEventListener('fullscreen', () => {
if (player.requestFullscreen) {
player.requestFullscreen();
} else if (player.webkitRequestFullscreen) {
player.webkitRequestFullscreen();
} else if (player.msRequestFullscreen) {
player.msRequestFullscreen();
}
});
// Channel navigation
let currentChannelIndex = -1;
let channels = [];
document.addEventListener('prevchannel', () => {
if (channels.length === 0) return;
currentChannelIndex = (currentChannelIndex - 1 + channels.length) % channels.length;
playChannel(channels[currentChannelIndex]);
});
document.addEventListener('nextchannel', () => {
if (channels.length === 0) return;
currentChannelIndex = (currentChannelIndex + 1) % channels.length;
playChannel(channels[currentChannelIndex]);
});
// Modified loadChannels to store channels globally
async function loadChannels() {
try {
const response = await fetch(m3uUrl);
const text = await response.text();
channels = parseM3U(text);
channelList.innerHTML = '';
channels.forEach((channel, index) => {
const channelItem = document.createElement('div');
channelItem.className = 'channel-item bg-gray-700 hover:bg-gray-600 rounded-lg p-3 cursor-pointer flex items-center transition-all';
channelItem.innerHTML = `
<div class="w-10 h-10 rounded-full bg-purple-500 flex items-center justify-center mr-3">
<i data-feather="tv" class="w-4 h-4"></i>
</div>
<div class="flex-1">
<h3 class="font-medium truncate">${channel.name}</h3>
<p class="text-gray-400 text-xs truncate">${channel.group || 'General'}</p>
</div>
`;
channelItem.addEventListener('click', () => {
currentChannelIndex = index;
playChannel(channel);
});
channelList.appendChild(channelItem);
});
}); |