tak / viewer.js
mbasaranoglu's picture
Upload 6 files
8fef298 verified
Raw
History Blame Contribute Delete
7.34 kB
// URL'den link kodunu al
function getLinkCodeFromUrl() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('code');
}
// URL'den link verisini decode et
function getLinkFromUrl() {
const urlParams = new URLSearchParams(window.location.search);
const encodedData = urlParams.get('data');
if (!encodedData) return null;
try {
return JSON.parse(atob(encodedData));
} catch (e) {
return null;
}
}
// Link kodunu doğrula (localStorage'dan)
function validateLink(code) {
const links = localStorage.getItem('videoLinks');
if (!links) return null;
try {
const linkArray = JSON.parse(links);
return linkArray.find(link => link.code === code) || null;
} catch (e) {
return null;
}
}
// Linkin süresi doldu mu kontrol et
function isLinkExpired(link) {
if (link.limitType === 'time') {
const now = new Date();
const expiresAt = new Date(link.expiresAt);
return now > expiresAt;
} else {
return link.remainingViews <= 0;
}
}
// İzleme sayısını azalt ve localStorage'a kaydet
function decrementViewCount(link) {
const links = localStorage.getItem('videoLinks');
if (!links) return link.remainingViews - 1;
try {
const linkArray = JSON.parse(links);
const linkIndex = linkArray.findIndex(l => l.code === link.code);
if (linkIndex !== -1) {
linkArray[linkIndex].remainingViews = linkArray[linkIndex].remainingViews - 1;
localStorage.setItem('videoLinks', JSON.stringify(linkArray));
return linkArray[linkIndex].remainingViews;
}
} catch (e) {
console.error('Error decrementing view count:', e);
}
return link.remainingViews - 1;
}
// Bildirim göster
function showNotification(message, type = 'success') {
const notification = document.getElementById('notification');
notification.textContent = message;
notification.className = 'notification';
if (type === 'error') {
notification.style.background = '#333';
notification.style.color = '#fff';
} else {
notification.style.background = '#fff';
notification.style.color = '#000';
}
notification.style.display = 'block';
setTimeout(() => {
notification.style.display = 'none';
}, 3000);
}
// Videoyu göster
function showVideo(link) {
document.getElementById('loading').style.display = 'none';
document.getElementById('error').style.display = 'none';
document.getElementById('videoSection').style.display = 'block';
document.getElementById('linkCode').textContent = link.code;
// Video player'ı ayarla ve videoyu dinamik yükle
const videoPlayer = document.getElementById('videoPlayer');
videoPlayer.src = 'Proje_Taslak_v7.mp4';
if (link.limitType === 'count') {
// Kez bazlı limit
const currentRemaining = link.remainingViews;
if (currentRemaining <= 0) {
// Hak doldu
showNotification('İzleme hakkınız doldu!', 'error');
videoPlayer.pause();
videoPlayer.src = '';
setTimeout(() => {
window.location.href = 'viewer.html?code=' + link.code;
}, 2000);
return;
}
// İzleme sayısını azalt
const newRemaining = decrementViewCount(link);
link.remainingViews = newRemaining;
document.getElementById('remainingViews').textContent = newRemaining;
document.getElementById('viewCountDisplay').textContent = newRemaining;
} else {
// Süre bazlı limit
const now = new Date();
const expiresAt = new Date(link.expiresAt);
const totalDuration = expiresAt - now;
const remainingMinutes = Math.max(0, Math.ceil(totalDuration / 60000));
// Video bilgilerindeki kalan hakkı güncelle
document.getElementById('remainingViews').textContent = remainingMinutes + ' dakika';
// Timer'ı göster, view-counter'ı gizle
document.getElementById('timerContainer').style.display = 'block';
const viewCounter = document.getElementById('viewCounter');
if (viewCounter) {
viewCounter.style.display = 'none';
}
// Süre formatlama fonksiyonu
function formatTime(ms) {
const totalSeconds = Math.floor(ms / 1000);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
}
// Timer değişkenlerini window'a taşı
window.linkExpiresAt = expiresAt;
window.linkTotalDuration = totalDuration;
window.linkVideoPlayer = videoPlayer;
window.linkCode = link.code;
// Timer güncelleme fonksiyonu
window.updateTimer = () => {
const currentTime = new Date();
const remainingMs = Math.max(0, window.linkExpiresAt - currentTime);
const progressPercent = window.linkTotalDuration > 0 ? (remainingMs / window.linkTotalDuration) * 100 : 0;
document.getElementById('timerDisplay').textContent = formatTime(remainingMs);
document.getElementById('timerProgress').style.width = progressPercent + '%';
document.getElementById('remainingViews').textContent = Math.ceil(remainingMs / 60000) + ' dakika';
if (remainingMs <= 0) {
showNotification('Süreniz doldu!', 'error');
window.linkVideoPlayer.pause();
window.linkVideoPlayer.src = '';
clearInterval(window.expirationInterval);
setTimeout(() => {
window.location.href = 'viewer.html?code=' + window.linkCode;
}, 2000);
}
};
// Başlangıç değerini göster ve timer'ı başlat
window.updateTimer();
window.expirationInterval = setInterval(window.updateTimer, 1000);
}
}
// Hata göster
function showError() {
document.getElementById('loading').style.display = 'none';
document.getElementById('videoSection').style.display = 'none';
document.getElementById('error').style.display = 'block';
}
// Sayfa yüklendiğinde
document.addEventListener('DOMContentLoaded', function() {
const code = getLinkCodeFromUrl();
if (!code) {
showError();
return;
}
const link = validateLink(code);
if (!link) {
showError();
return;
}
if (isLinkExpired(link)) {
if (link.limitType === 'count') {
document.querySelector('.error-message').textContent = 'İzleme Hakkı Doldu';
document.querySelector('.error-description').textContent = 'Bu link için izleme hakkınız tükenmiş.';
} else {
document.querySelector('.error-message').textContent = 'Süre Doldu';
document.querySelector('.error-description').textContent = 'Bu linkin süresi dolmuş.';
}
showError();
return;
}
showVideo(link);
});