Skip to content

Instantly share code, notes, and snippets.

@bitmvr
Last active April 3, 2024 19:25
Show Gist options
  • Save bitmvr/d9fd52a2df19087b7e158c9d22d878fa to your computer and use it in GitHub Desktop.
Save bitmvr/d9fd52a2df19087b7e158c9d22d878fa to your computer and use it in GitHub Desktop.
Bookmarklet for Generating Plex Supported Filename for Movies from TMDB or IMDB
/*
The following code is used to generate a string that adheres to a Plex filename
convention for movie titles. This code can strip page titles from either
TMDB or IMDB and put them in dot notation of:
Movie.Title.Year.{source-sourceId}
An extension is not provided as I do not know the extension of the target file.
*/
javascript: (() => {
var sites = {
'www.themoviedb.org': {
'tag': 'tmdb',
'titleSuffix': ' — The Movie Database (TMDB)'
},
'www.imdb.com': {
'tag': 'imdb',
'titleSuffix': ' - IMDb'
}
};
var pageTitleString = document.title;
var id = window.location.pathname.split('/')[2].split('-')[0];
var hostname = window.location.hostname;
var process_title = function(data) {
return data.split(' ')
.map(e => e.replace(/[^a-z0-9]/gi, ''))
.join('.');
};
if (sites[hostname]) {
movieTitleAndYear = pageTitleString.replace(sites[hostname].titleSuffix, '');
sourceTag = sites[hostname].tag;
} else {
throw new Error("This is not a supported site.");
};
movieTitleAndYear = process_title(movieTitleAndYear);
filename = `${movieTitleAndYear}.{${sourceTag}-${id}}`;
var copyListener = function(event) {
document.removeEventListener("copy", copyListener, true);
event.preventDefault();
let clipboardData = event.clipboardData;
clipboardData.clearData();
clipboardData.setData("text/plain", filename);
};
document.addEventListener("copy", copyListener, true);
document.execCommand("copy");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment