Skip to content

Instantly share code, notes, and snippets.

@soup-bowl
Last active September 20, 2023 17:55
Show Gist options
  • Save soup-bowl/c98165af8a13e9e0956cadb71ea0354f to your computer and use it in GitHub Desktop.
Save soup-bowl/c98165af8a13e9e0956cadb71ea0354f to your computer and use it in GitHub Desktop.
UserScript to show films in Plex-preferred naming syntax
// ==UserScript==
// @name IMDb Name Output for Plex
// @version 0.2
// @description Prints out a pre-formatted name for my Plex library. Click on the name to copy to clipboard.
// @author soup-bowl
// @namespace https://gist.github.com/soup-bowl/c98165af8a13e9e0956cadb71ea0354f
// @match https://www.imdb.com/*
// @match https://imdb.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// IMDB seems to supply a nice JSON for us to hook into instead of DOM exploring.
const scriptElement = document.querySelector('script[type="application/ld+json"]');
const titleElement = document.querySelector('section.ipc-page-section h1 span')
if (scriptElement && titleElement) {
try {
const jsonData = JSON.parse(scriptElement.textContent);
const filmTitle = jsonData.name || '';
const year = jsonData.datePublished ? jsonData.datePublished.split('-')[0] : '';
const imdbID = window.location.href.split('/')[4];
const combinedString = `${filmTitle} (${year}) {imdb-${imdbID}}`;
if (filmTitle) {
const newLink = document.createElement('a');
newLink.href = '#';
newLink.textContent = combinedString;
newLink.style.color = 'white';
newLink.style.fontSize = '30px';
newLink.addEventListener('mouseenter', function() {
newLink.style.textDecoration = 'underline';
});
newLink.addEventListener('mouseleave', function() {
newLink.style.textDecoration = 'none';
});
newLink.addEventListener('click', function() {
navigator.clipboard.writeText(combinedString);
});
titleElement.parentNode.replaceChild(newLink, titleElement);
}
} catch (error) {
console.error('Error parsing JSON data:', error);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment