`;
document.body.appendChild(container);
// Snackbar Element
const snackbar = document.createElement('div');
snackbar.id = 'wn-snackbar';
document.body.appendChild(snackbar);
/*************** Utility Functions ***************/
// Function to show snackbar messages
function showSnackbar(message) {
snackbar.textContent = message;
snackbar.classList.add('show');
setTimeout(() => {
snackbar.classList.remove('show');
}, 3000);
}
// Function to toggle visibility of sections
function toggleSections(show) {
const redirectOptions = document.getElementById('wn-redirectOptions');
const seasonEpisodeOptions = document.getElementById('wn-seasonEpisodeOptions');
if (show) {
redirectOptions.classList.add('active');
seasonEpisodeOptions.classList.add('active');
} else {
redirectOptions.classList.remove('active');
seasonEpisodeOptions.classList.remove('active');
}
}
// Function to extract IMDb ID using a robust regex
function getImdbId(url) {
const regex = /\/title\/(tt\d{7,8})/; // IMDb IDs typically have 7 or 8 digits
const match = url.match(regex);
return match ? match[1] : null;
}
// Function to determine the title type on IMDb by inspecting specific list items
function determineImdbTitleType() {
const listItems = document.querySelectorAll('ul.ipc-inline-list.ipc-inline-list--show-dividers li.ipc-inline-list__item');
for (const item of listItems) {
const text = item.textContent.trim().toLowerCase();
if (text.includes('tv series') || text.includes('tv mini-series')) {
return 'tv';
}
// You can add more conditions here for other types if needed
}
return 'movie'; // Default to 'movie' if no TV indicators are found
}
// Function to extract TMDB ID and type from the URL
function getTmdbInfo(url) {
const regex = /https:\/\/www\.themoviedb\.org\/(movie|tv)\/(\d+)/;
const match = url.match(regex);
if (match) {
return { type: match[1], id: match[2] };
}
return null;
}
// Function to build the redirect URL with optional season and episode for TV
function buildRedirectUrl(titleType, id, season, episode) {
let url = `https://vidbinge.dev/embed/${titleType}/${id}`;
if (titleType === 'tv' && season && episode) {
url += `/${season}/${episode}`;
}
return url;
}
/*************** Settings Management ***************/
// Load existing settings
let redirectEnabled = GM_getValue('redirectEnabled', false);
let redirectTarget = GM_getValue('redirectTarget', 'same');
let season = GM_getValue('season', 1);
let episode = GM_getValue('episode', 1);
// Initialize UI with settings
const redirectCheckbox = document.getElementById('wn-redirectCheckbox');
const redirectSame = document.getElementById('wn-redirectSame');
const redirectNew = document.getElementById('wn-redirectNew');
const seasonInput = document.getElementById('wn-seasonInput');
const episodeInput = document.getElementById('wn-episodeInput');
redirectCheckbox.checked = redirectEnabled;
toggleSections(redirectEnabled);
if (redirectTarget === 'same') {
redirectSame.checked = true;
} else {
redirectNew.checked = true;
}
seasonInput.value = season;
episodeInput.value = episode;
/*************** Event Listeners ***************/
// Event listener for the redirect checkbox
redirectCheckbox.addEventListener('change', function() {
const isEnabled = redirectCheckbox.checked;
GM_setValue('redirectEnabled', isEnabled);
toggleSections(isEnabled);
});
// Event listeners for radio buttons
redirectSame.addEventListener('change', function() {
if (this.checked) {
GM_setValue('redirectTarget', 'same');
}
});
redirectNew.addEventListener('change', function() {
if (this.checked) {
GM_setValue('redirectTarget', 'new');
}
});
// Event listeners for Season and Episode inputs
seasonInput.addEventListener('input', function() {
let val = parseInt(seasonInput.value, 10);
if (isNaN(val) || val < 1) {
val = 1;
seasonInput.value = val;
}
GM_setValue('season', val);
});
episodeInput.addEventListener('input', function() {
let val = parseInt(episodeInput.value, 10);
if (isNaN(val) || val < 1) {
val = 1;
episodeInput.value = val;
}
GM_setValue('episode', val);
});
// Event listener for the redirect button
document.getElementById('wn-redirectButton').addEventListener('click', function() {
const url = window.location.href;
// Check if URL is IMDb
const imdbId = getImdbId(url);
if (imdbId) {
const titleType = determineImdbTitleType();
if (titleType === 'tv') {
const currentSeason = GM_getValue('season', 1);
const currentEpisode = GM_getValue('episode', 1);
const redirectUrl = buildRedirectUrl(titleType, imdbId, currentSeason, currentEpisode);
performRedirect(redirectUrl);
} else {
// For movies, no season and episode
const redirectUrl = buildRedirectUrl(titleType, imdbId);
performRedirect(redirectUrl);
}
return;
}
// Check if URL is TMDB
const tmdbInfo = getTmdbInfo(url);
if (tmdbInfo) {
const { type, id } = tmdbInfo;
if (type === 'tv') {
const currentSeason = GM_getValue('season', 1);
const currentEpisode = GM_getValue('episode', 1);
const redirectUrl = buildRedirectUrl(type, id, currentSeason, currentEpisode);
performRedirect(redirectUrl);
} else {
// For movies, no season and episode
const redirectUrl = buildRedirectUrl(type, id);
performRedirect(redirectUrl);
}
return;
}
showSnackbar('This is not a supported title page.');
});
/*************** Redirection Function ***************/
function performRedirect(url) {
if (!GM_getValue('redirectEnabled', false)) {
showSnackbar('Redirect is disabled in settings.');
return;
}
const target = GM_getValue('redirectTarget', 'same');
if (target === 'same') {
window.location.href = url;
showSnackbar('Redirecting...');
} else if (target === 'new') {
window.open(url, '_blank');
showSnackbar('Redirecting to new tab...');
} else {
showSnackbar('Invalid redirect target.');
}
}
/*************** Optional: Auto Redirect ***************/
// Uncomment the following block if you want to auto redirect when enabled
/*
if (redirectEnabled) {
const url = window.location.href;
// Check if URL is IMDb
const imdbId = getImdbId(url);
if (imdbId) {
const titleType = determineImdbTitleType();
if (titleType === 'tv') {
const currentSeason = GM_getValue('season', 1);
const currentEpisode = GM_getValue('episode', 1);
const redirectUrl = buildRedirectUrl(titleType, imdbId, currentSeason, currentEpisode);
performRedirect(redirectUrl);
} else {
// For movies, no season and episode
const redirectUrl = buildRedirectUrl(titleType, imdbId);
performRedirect(redirectUrl);
}
return;
}
// Check if URL is TMDB
const tmdbInfo = getTmdbInfo(url);
if (tmdbInfo) {
const { type, id } = tmdbInfo;
if (type === 'tv') {
const currentSeason = GM_getValue('season', 1);
const currentEpisode = GM_getValue('episode', 1);
const redirectUrl = buildRedirectUrl(type, id, currentSeason, currentEpisode);
performRedirect(redirectUrl);
} else {
// For movies, no season and episode
const redirectUrl = buildRedirectUrl(type, id);
performRedirect(redirectUrl);
}
return;
}
console.error('This is not a valid IMDb or TMDB title page.');
}
*/
})();