Skip to content

Instantly share code, notes, and snippets.

@theit8514
Last active April 15, 2019 04:34
Show Gist options
  • Save theit8514/5ae6aec82c40bd4f42867c289f5f6300 to your computer and use it in GitHub Desktop.
Save theit8514/5ae6aec82c40bd4f42867c289f5f6300 to your computer and use it in GitHub Desktop.
Plex Season Title Editor - UserScript
// ==UserScript==
// @name Plex Season Title Editor
// @namespace https://gist.github.com/theit8514
// @version 0.2
// @description Allows editing of Season titles by adding the Title input to the Season edit popup
// @author theit8514
// @grant none
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js
// @include https://app.plex.tv/desktop*
// @include https://*.plex.direct:*/*
// ==/UserScript==
/*
Tested on Plex 1.14.1.5488
For users that use a custom URL for their Plex server, you can use the "User includes" section of the
script editor to set your own includes or add a line to the include section above. E.g: */
// @include https://plex.example.com:*/*
/*
The MIT License (MIT)
Copyright (c) 2017 theit8514
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
function log(m) {
var args = Array.prototype.slice.call(arguments);
args.unshift('[Plex Season Title Editor]');
console.log.apply(this, args);
}
if ($ === undefined) {
log('jQuery not loaded, attempting to load manually...');
var script = document.createElement('script');
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js';
script.type = 'text/javascript';
script.onload = function() {
setTimeout(function() {
log('jQuery loaded');
console.log($);
var jQuery_310 = $.noConflict(true);
if ($ === undefined) {
log('$ is undefined, jQuery_310 calls: ' + jQuery_310.fn.jquery);
} else {
log('$ calls: ' + $.fn.jquery + ', jQuery_310 calls: ' + jQuery_310.fn.jquery);
}
// initialize any code that uses jQuery_310 here
init_SeasonTitleEditor(jQuery_310);
}, 1000);
};
document.getElementsByTagName('head')[0].appendChild(script);
} else {
$( document ).ready(function() {
log('jQuery loaded by user script manager, initializing...');
init_SeasonTitleEditor($);
});
}
function init_SeasonTitleEditor($) {
var plexapp = $('#plex.application');
if (plexapp.length === 0) {
// If there is not a div with id plex with class application, then this is not a plex page.
return;
}
$(plexapp).on('click', '[data-qa-id=metadataPosterEditButton]', onClickPosterEdit);
$(plexapp).on('click', '[aria-label=Edit]', onClickToolbarEdit);
var clickedSeason = '';
function showInModal(wireup) {
if (wireup === undefined) {
wireup = true;
}
var modal = $('.edit-metadata-modal');
if (wireup) {
modal.on('click', '.change-pane-btn[data-pane=general]', onChangePane);
modal.on('click', '[data-dismiss=modal]', onCloseModal);
modal.on('click', 'button.save-btn', onCloseModal);
}
var body = $('.modal-body-pane', modal);
// Modal could be loading at this point, we should check to see if we have a summary field and retimeout if not found.
var form = $('form.edit-metadata-form', body);
if (form.length === 0) {
setTimeout(showInModal, 100, wireup);
return;
}
var summaryField = $('textarea[name=summary]', body);
if (summaryField.length === 0) {
setTimeout(showInModal, 100, wireup);
return;
}
log('Creating field for title on the modal window');
var row = summaryField.closest('.row');
var newDiv = $('<div class="col-md-12"><div class="form-group selectize-group" data-attr="title"><label for="title">Title</label><div class="input-group"><a href="#" data-field="summary" class="input-group-addon edit-lock-addon" tabindex="-1"><i class="glyphicon lock"></i></a></div></div></div>');
var inputGroup = $('.input-group', newDiv);
row.prepend(newDiv);
var newField = $('<input type="text" id="title" class="form-control selectized" name="title" placeholder="Default Title" tabindex="-1">');
newField.val(clickedSeason);
inputGroup.append(newField);
}
function onCloseModal(event) {
// When closing a modal that we modified, clear the current season.
clickedSeason = '';
}
function onChangePane(event) {
// When changing back to the General page, we need to re-add the field.
log('Change pane!');
if (clickedSeason === '') {
return;
}
showInModal(false);
}
function onClickPosterEdit(event) {
log('Clicked poster!');
clickedSeason = '';
// Get parent card, find the title for it
var siblings = $(event.currentTarget).closest('[data-qa-id^=metadataPosterCard-]').siblings();
var isSeasonView = siblings.find('span').text().indexOf('episode') >= 0;
if (!isSeasonView) {
return;
}
clickedSeason = siblings.find('a').text();
setTimeout(showInModal, 100);
}
function onClickToolbarEdit(event) {
log('Clicked toolbar!');
clickedSeason = '';
// Toolbar sucks, since the same button controls edit for multiple types of data.
// Check to see if the current page shows "x EPISODES" on the screen.
var episodeLine = $('[class^=HubCellTitle-hubCellTitle-]', plexapp);
var isSeasonView = episodeLine.text().toLowerCase().indexOf('episode') >= 0;
if (!isSeasonView) {
return;
}
// Get the title of this season from the page.
clickedSeason = $.trim($('div[class^=PrePlayLeftTitle-leftTitle-]').text());
setTimeout(showInModal, 100);
}
}
@GRubio
Copy link

GRubio commented Dec 16, 2017

Hi. It seems I can't find help at flex.tv nor Google... How can I run this script to use it in Plex?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment