// ==UserScript==
// @name IMDb List Helper
// @namespace imdb
// @description Makes creating IMDb lists more efficient and convenient
// @version 1.6.1.2
// @include http://*imdb.com/list/edit*
// @downloadURL none
// ==/UserScript==
//
// CHANGELOG
//
// 1.6.1.2
// added: input text suggestion as a placeholder
//
// 1.6.1.1
// fixed: some entries are skipped when adding imdb ids/urls
//
//
// milliseconds between each request
//
var REQUEST_DELAY = 1000;
//
// do not modify anything below this line !!
//
var jQuery = unsafeWindow.jQuery;
var $ = jQuery;
var textEdit = '
'
+ ' '
+ ' '
+ ' '
+ ' '
+ 'Remaining: 0
'
+ 'Current: '
+ 'Regexp: '
+ '
';
$("div#main > div.list_edit > div.add").after(textEdit);
// globals
var films = [];
var regexObj = null;
function resetState()
{
films = [];
regexObj = null;
$("#filmList").removeAttr("disabled");
$("#filmRegexp").removeAttr("disabled"); // leave regex
$("#doList").removeAttr("disabled");
$("#filmCurrent").val("");
$("input[name=add]", "div.add").val("");
$("div.results", "div.add").html("");
}
function searchFilm(filmTitle)
{
// remove unnecessary whitespace
filmTitle = $.trim(filmTitle);
// set current text to what we're searching
$("#filmCurrent", "#imdbListTextEdit").val(filmTitle);
// remove the first title from the text box and set the remaining number
$filmList = $("#filmList", "#imdbListTextEdit");
var newList = $filmList.val().split("\n");
$("#filmsRemaining", "#imdbListTextEdit").text(newList.length-1);
$filmList.val( newList.slice(1).join("\n") );
// run regex
if( regexObj != null )
{
filmTitle = regexObj.exec(filmTitle)[1];
}
// set imdb search input field to film title
$("input[name=add]", "div.add").val(filmTitle);
// trigger search button click
$("button.search").triggerHandler("click");
}
// when start button is clicked
$("#imdbListTextEdit").delegate("#doList", "click", function(e)
{
$filmList = $("#filmList", "#imdbListTextEdit");
// regex
$regexBox = $("#filmRegexp", "#imdbListTextEdit");
if( $regexBox.val() )
{
regexObj = RegExp( $regexBox.val() );
}
// disable the text area and the button and the regexp box
$filmList.attr("disabled", "disabled");
$regexBox.attr("disabled", "disabled");
$("#doList").attr("disabled", "disabled");
films = $filmList.val().split("\n");
// search first item
searchFilm(films.shift());
});
// when skip button is clicked
$("#imdbListTextEdit").delegate("#skipFilm", "click", function(e)
{
// skip to next film
if(films.length > 0)
{
searchFilm(films.shift());
}
else if(films.length == 0) // if no more films...
{
resetState();
}
});
// Sometimes the request fails forcing the user to skip an entry to continue
jQuery("#imdbListTextEdit").delegate("#retryPost", "click", function(e)
{
$("button.search").triggerHandler("click");
});
// when a search result item is clicked
$("div.results", "div.add").delegate("li", "click", function(e)
{
// if there's more items, search next...
if(films.length > 0)
{
searchFilm(films.shift());
}
else
{
// if last film, reset stuff
resetState();
}
});
// monitors for changes to the search result box
// if it recognizes an imdb url/id, it's clicked automatically
// since there's only one result
$("div.results").bind("DOMNodeInserted", function(e)
{
if($("#filmCurrent").val().match(/(tt[0-9]{7})/)[1] && $("div.results").find("li").length)
{
setTimeout(function()
{
$("li", "div.results").trigger("click");
}, REQUEST_DELAY);
}
});