From cdb213380962de25ee2912f7bebd3ff0b2e342ba Mon Sep 17 00:00:00 2001 From: Pulkit Pareek <84959590+pulkitpareek18@users.noreply.github.com> Date: Fri, 10 Feb 2023 19:21:46 +0530 Subject: [PATCH] added title in url --- script.js | 12 +++++++++--- script.min.js | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/script.js b/script.js index 597b758..4fdf642 100644 --- a/script.js +++ b/script.js @@ -32,7 +32,7 @@ function fetchAndShow() { let imageAndInfo = ""; if (result.qid === "movie" && result.i) { - imageAndInfo = ` + imageAndInfo = `

${result.l}

@@ -41,7 +41,7 @@ function fetchAndShow() {
`; } else if (result.qid === "tvSeries" && result.i) { - imageAndInfo = ` + imageAndInfo = `

${result.l}

@@ -117,10 +117,16 @@ function fillSearchInput() { searchInput.value = search; fetchAndShow(); } + + // It will set the contents according to url data + else if (imdb && type && !search && !episode && !season) { fetchTitle(imdb) .then(title => setAll(imdb, title,season,episode,type)) .catch(error => console.error(error)); + + + }else if (imdb && !search && episode && season) { fetchTitle(imdb) .then(title => setAll(imdb, title, season, episode,type)) @@ -226,7 +232,7 @@ function setVideo(element) { for (const episode of episodesDataJSON.episodes) { const episodeNumber = episode.episode_number; let formatedEpisodeNumber = (episodeNumber).toLocaleString('en-US', { minimumIntegerDigits: 2, useGrouping: false }); - episodesData += `
E${formatedEpisodeNumber}. ${episode.name}`; + episodesData += `E${formatedEpisodeNumber}. ${episode.name}`; } episodeContainer.innerHTML = episodesData; diff --git a/script.min.js b/script.min.js index cb0e2e2..cd62a11 100644 --- a/script.min.js +++ b/script.min.js @@ -1 +1 @@ -function scrollToTop(){const c=document.documentElement.scrollTop||document.body.scrollTop;c>0&&(window.requestAnimationFrame(scrollToTop),window.scrollTo(0,c-c/8))}const searchInput=document.getElementById("search-input"),searchButton=document.getElementById("search-button"),resultsContainer=document.getElementById("results");function fetchAndShow(){const query=encodeURIComponent(searchInput.value),url=`https://cors-anywhere.azm.workers.dev/https://v3.sg.media-imdb.com/suggestion/x/${query}.json`;Pace.restart(),fetch(url).then(response=>response.json()).then(data=>{const results=data.d;resultsContainer.innerHTML="",results.forEach(result=>{if(result.i&&("movie"===result.qid||"tvSeries"===result.qid)){const resultElem=document.createElement("div");resultElem.classList.add("result");let imageAndInfo="";"movie"===result.qid&&result.i?imageAndInfo=`\n \n
\n

${result.l}

\n

${result.s}

\n
\n
`:"tvSeries"===result.qid&&result.i&&(imageAndInfo=`\n \n
\n

${result.l}

\n

${result.s}

\n
\n
`),resultElem.innerHTML=imageAndInfo,resultsContainer.appendChild(resultElem)}})})}function setAll(imdb,title,season,episode,type){if(imdb&&title&&!season&&!episode&&type){let a=document.createElement("a");a.setAttribute("onClick","setUrl(this); return setVideo(this);"),a.setAttribute("url",`imdb=${imdb}&&type=movie`),a.setAttribute("isWebSeries","false"),a.setAttribute("title",title),a.setAttribute("class","links"),a.setAttribute("IMDB",imdb),a.setAttribute("href","https://www.2embed.to/embed/imdb/movie?id="+imdb),a.setAttribute("target","_blank"),a.click()}else if(imdb&&title&&episode&&!type){let a=document.createElement("a");a.setAttribute("onClick","setUrl(this); return setVideo(this);"),a.setAttribute("url",`imdb=${imdb}&season=${season}&episode=${episode}`),a.setAttribute("isWebSeries","true"),a.setAttribute("title",title),a.setAttribute("class","links"),a.setAttribute("IMDB",imdb),a.setAttribute("href",`https://www.2embed.to/embed/imdb/tv?id=${imdb}&s=${season}&e=${episode}`),a.setAttribute("target","_blank"),a.click()}}const fetchTitle=async imdbID=>{const url=`https://cors-anywhere.azm.workers.dev/https://v3.sg.media-imdb.com/suggestion/x/${imdbID}.json`;try{const response=await fetch(url),data=await response.json(),title=data.d[0].l;return title}catch(error){console.error(error)}};function setUrl(element){let search=element.getAttribute("url");window.history.replaceState({},"",`?${search.replace(/%20/g,"+")}`)}function fillSearchInput(){let searchParams=new URLSearchParams(window.location.search),search=searchParams.get("search"),season=searchParams.get("season"),episode=searchParams.get("episode"),imdb=searchParams.get("imdb"),type=searchParams.get("type");if(!search||season||episode||imdb)!imdb||!type||search||episode||season?imdb&&!search&&episode&&season&&fetchTitle(imdb).then(title=>setAll(imdb,title,season,episode,type)).catch(error=>console.error(error)):fetchTitle(imdb).then(title=>setAll(imdb,title,season,episode,type)).catch(error=>console.error(error));else{search=search.replace(/\+/g,"%20");const searchInput=document.querySelector("#search-input");searchInput.value=search,fetchAndShow()}}function updateURL(input){let search=input.value;search?window.history.replaceState({},"",`?search=${encodeURIComponent(search).replace(/%20/g,"+")}`):window.history.replaceState({},"",window.location.pathname)}function episodeHighlight(cssidentification="s1e1"){document.querySelectorAll(".episodes").forEach((function(episode){episode.className="episodes"})),document.querySelector(`.episodes[cssidentification='${cssidentification}']`).className="episodes selected"}function setVideo(element){const iframe=document.getElementById("iframe"),video=document.getElementById("video");iframe.src=element.getAttribute("href"),video.style.display="block";const webSeriesData=document.getElementById("webSeriesData"),tmdbApiKey="b6b677eb7d4ec17f700e3d4dfc31d005",imdbID=element.getAttribute("IMDB");if(Pace.restart(),scrollToTop(),"false"==element.getAttribute("isWebSeries")&&"links"==element.className&&(webSeriesData.innerHTML=""),""!==element.getAttribute("title")&&(document.title=element.getAttribute("title")),element.className.includes("episode")&&(episodeHighlight(element.getAttribute("cssidentification")),console.log("clicked")),"true"==element.getAttribute("isWebSeries")){async function printEpisodes(){const response=await fetch(`https://api.themoviedb.org/3/find/${imdbID}?api_key=${tmdbApiKey}&language=en-US&external_source=imdb_id`),data=await response.json(),showId=data.tv_results[0].id,seasonsData=await fetch(`https://api.themoviedb.org/3/tv/${showId}?api_key=${tmdbApiKey}&language=en-US`),seasonsDataJSON=await seasonsData.json(),numberOfSeasons=seasonsDataJSON.number_of_seasons;webSeriesData.innerHTML+="

Seasons:

";for(let seasonNumber=1;seasonNumber<=numberOfSeasons;seasonNumber++){webSeriesData.innerHTML+=`

Season ${seasonNumber}:


`;let episodeContainer=document.createElement("div");episodeContainer.classList.add("episode-container");let episodesData="";const episodesDataResponse=await fetch(`https://api.themoviedb.org/3/tv/${showId}/season/${seasonNumber}?api_key=${tmdbApiKey}&language=en-US`),episodesDataJSON=await episodesDataResponse.json();for(const episode of episodesDataJSON.episodes){const episodeNumber=episode.episode_number;let formatedEpisodeNumber=episodeNumber.toLocaleString("en-US",{minimumIntegerDigits:2,useGrouping:!1});episodesData+=`E${formatedEpisodeNumber}. ${episode.name}`}episodeContainer.innerHTML=episodesData,webSeriesData.appendChild(episodeContainer),episodeHighlight(),document.querySelector('a[cssIdentification="s1e1"]').click()}}webSeriesData.innerHTML="",printEpisodes()}function gtag(){dataLayer.push(arguments)}return window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","G-THTQ9GZQ0J"),console.log("gtag updated location"),!1}fillSearchInput(),searchInput.addEventListener("keyup",(function(){updateURL(this),fetchAndShow(),$("html,body").animate({scrollTop:$("#results").offset().top},"slow")})),searchButton.addEventListener("click",(function(){fetchAndShow()})); \ No newline at end of file +function scrollToTop(){const c=document.documentElement.scrollTop||document.body.scrollTop;c>0&&(window.requestAnimationFrame(scrollToTop),window.scrollTo(0,c-c/8))}const searchInput=document.getElementById("search-input"),searchButton=document.getElementById("search-button"),resultsContainer=document.getElementById("results");function fetchAndShow(){const query=encodeURIComponent(searchInput.value),url=`https://cors-anywhere.azm.workers.dev/https://v3.sg.media-imdb.com/suggestion/x/${query}.json`;Pace.restart(),fetch(url).then(response=>response.json()).then(data=>{const results=data.d;resultsContainer.innerHTML="",results.forEach(result=>{if(result.i&&("movie"===result.qid||"tvSeries"===result.qid)){const resultElem=document.createElement("div");resultElem.classList.add("result");let imageAndInfo="";"movie"===result.qid&&result.i?imageAndInfo=`\n \n
\n

${result.l}

\n

${result.s}

\n
\n
`:"tvSeries"===result.qid&&result.i&&(imageAndInfo=`\n \n
\n

${result.l}

\n

${result.s}

\n
\n
`),resultElem.innerHTML=imageAndInfo,resultsContainer.appendChild(resultElem)}})})}function setAll(imdb,title,season,episode,type){if(imdb&&title&&!season&&!episode&&type){let a=document.createElement("a");a.setAttribute("onClick","setUrl(this); return setVideo(this);"),a.setAttribute("url",`imdb=${imdb}&&type=movie`),a.setAttribute("isWebSeries","false"),a.setAttribute("title",title),a.setAttribute("class","links"),a.setAttribute("IMDB",imdb),a.setAttribute("href","https://www.2embed.to/embed/imdb/movie?id="+imdb),a.setAttribute("target","_blank"),a.click()}else if(imdb&&title&&episode&&!type){let a=document.createElement("a");a.setAttribute("onClick","setUrl(this); return setVideo(this);"),a.setAttribute("url",`imdb=${imdb}&season=${season}&episode=${episode}`),a.setAttribute("isWebSeries","true"),a.setAttribute("title",title),a.setAttribute("class","links"),a.setAttribute("IMDB",imdb),a.setAttribute("href",`https://www.2embed.to/embed/imdb/tv?id=${imdb}&s=${season}&e=${episode}`),a.setAttribute("target","_blank"),a.click()}}const fetchTitle=async imdbID=>{const url=`https://cors-anywhere.azm.workers.dev/https://v3.sg.media-imdb.com/suggestion/x/${imdbID}.json`;try{const response=await fetch(url),data=await response.json(),title=data.d[0].l;return title}catch(error){console.error(error)}};function setUrl(element){let search=element.getAttribute("url");window.history.replaceState({},"",`?${search.replace(/%20/g,"+")}`)}function fillSearchInput(){let searchParams=new URLSearchParams(window.location.search),search=searchParams.get("search"),season=searchParams.get("season"),episode=searchParams.get("episode"),imdb=searchParams.get("imdb"),type=searchParams.get("type");if(!search||season||episode||imdb)!imdb||!type||search||episode||season?imdb&&!search&&episode&&season&&fetchTitle(imdb).then(title=>setAll(imdb,title,season,episode,type)).catch(error=>console.error(error)):fetchTitle(imdb).then(title=>setAll(imdb,title,season,episode,type)).catch(error=>console.error(error));else{search=search.replace(/\+/g,"%20");const searchInput=document.querySelector("#search-input");searchInput.value=search,fetchAndShow()}}function updateURL(input){let search=input.value;search?window.history.replaceState({},"",`?search=${encodeURIComponent(search).replace(/%20/g,"+")}`):window.history.replaceState({},"",window.location.pathname)}function episodeHighlight(cssidentification="s1e1"){document.querySelectorAll(".episodes").forEach((function(episode){episode.className="episodes"})),document.querySelector(`.episodes[cssidentification='${cssidentification}']`).className="episodes selected"}function setVideo(element){const iframe=document.getElementById("iframe"),video=document.getElementById("video");iframe.src=element.getAttribute("href"),video.style.display="block";const webSeriesData=document.getElementById("webSeriesData"),tmdbApiKey="b6b677eb7d4ec17f700e3d4dfc31d005",imdbID=element.getAttribute("IMDB");if(Pace.restart(),scrollToTop(),"false"==element.getAttribute("isWebSeries")&&"links"==element.className&&(webSeriesData.innerHTML=""),""!==element.getAttribute("title")&&(document.title=element.getAttribute("title")),element.className.includes("episode")&&(episodeHighlight(element.getAttribute("cssidentification")),console.log("clicked")),"true"==element.getAttribute("isWebSeries")){async function printEpisodes(){const response=await fetch(`https://api.themoviedb.org/3/find/${imdbID}?api_key=${tmdbApiKey}&language=en-US&external_source=imdb_id`),data=await response.json(),showId=data.tv_results[0].id,seasonsData=await fetch(`https://api.themoviedb.org/3/tv/${showId}?api_key=${tmdbApiKey}&language=en-US`),seasonsDataJSON=await seasonsData.json(),numberOfSeasons=seasonsDataJSON.number_of_seasons;webSeriesData.innerHTML+="

Seasons:

";for(let seasonNumber=1;seasonNumber<=numberOfSeasons;seasonNumber++){webSeriesData.innerHTML+=`

Season ${seasonNumber}:


`;let episodeContainer=document.createElement("div");episodeContainer.classList.add("episode-container");let episodesData="";const episodesDataResponse=await fetch(`https://api.themoviedb.org/3/tv/${showId}/season/${seasonNumber}?api_key=${tmdbApiKey}&language=en-US`),episodesDataJSON=await episodesDataResponse.json();for(const episode of episodesDataJSON.episodes){const episodeNumber=episode.episode_number;let formatedEpisodeNumber=episodeNumber.toLocaleString("en-US",{minimumIntegerDigits:2,useGrouping:!1});episodesData+=`E${formatedEpisodeNumber}. ${episode.name}`}episodeContainer.innerHTML=episodesData,webSeriesData.appendChild(episodeContainer),episodeHighlight(),document.querySelector('a[cssIdentification="s1e1"]').click()}}webSeriesData.innerHTML="",printEpisodes()}function gtag(){dataLayer.push(arguments)}return window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","G-THTQ9GZQ0J"),console.log("gtag updated location"),!1}fillSearchInput(),searchInput.addEventListener("keyup",(function(){updateURL(this),fetchAndShow(),$("html,body").animate({scrollTop:$("#results").offset().top},"slow")})),searchButton.addEventListener("click",(function(){fetchAndShow()})); \ No newline at end of file