Last active
February 24, 2024 01:43
-
-
Save carmona/09cd03fddeb7db25ace42a21cf3d7e33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this is meant to extract a movie's full name, year and ID from an IMDB page | |
// in a format that is easily read by Plex | |
// eg: "The Haunted Mansion [2003][tt0338094]" | |
(() => { | |
let window_code = findCode(window.location.pathname); | |
if (window_code && window_code.includes('tt')) { | |
let movie_name = formatMovieTitle(getOriginalName() || getSimpleName()); | |
let year = getYear(document.title); | |
copyToClipboard(`${movie_name} [${year}][${window_code}]`); | |
} | |
function getOriginalName () { | |
let original_name = document.querySelector(".sc-d8941411-1.fTeJrK"); | |
if (!original_name) return null; | |
original_name = original_name.innerText.replace('Original title: ', ''); | |
return original_name; | |
} | |
function getSimpleName () { | |
let input = document.title; | |
return input.split("(")[0].trim(); | |
} | |
function getYear (input) { | |
let yearMatch = input.match(/\((.*?)\)/); | |
let year; | |
if (yearMatch) { | |
let yearText = yearMatch[1]; | |
year = yearText.match(/\d{4}/); | |
} | |
return year; | |
} | |
function formatMovieTitle(input) { | |
let title = input.replace('&', 'and'); | |
title = title.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); | |
title = title.replace(/'/g, ''); | |
title = title.replace(/[^\w\s]/g, ''); | |
title = title.replace(/[$".]/g, ''); | |
return title.trim(); | |
} | |
function findCode(url) { | |
let parts = url.split('/'); | |
let imdb_code; | |
parts.forEach(function(part) { | |
if (part.startsWith('tt')) { | |
imdb_code = part; | |
} | |
}); | |
return imdb_code; | |
} | |
function copyToClipboard(text) { | |
var textarea = document.createElement("textarea"); | |
textarea.value = text; | |
document.body.appendChild(textarea); | |
textarea.select(); | |
document.execCommand("copy"); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment