IMDb List Helper

Makes creating IMDb lists more efficient and convenient

当前为 2015-08-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name IMDb List Helper
  3. // @namespace imdb
  4. // @description Makes creating IMDb lists more efficient and convenient
  5. // @version 1.6.1.2
  6. // @include http://*imdb.com/list/edit*
  7. // ==/UserScript==
  8.  
  9. //
  10. // CHANGELOG
  11. //
  12. // 1.6.1.2
  13. // added: input text suggestion as a placeholder
  14. //
  15. // 1.6.1.1
  16. // fixed: some entries are skipped when adding imdb ids/urls
  17. //
  18.  
  19. //
  20. // milliseconds between each request
  21. //
  22. var REQUEST_DELAY = 1000;
  23.  
  24. //
  25. // do not modify anything below this line !!
  26. //
  27. var jQuery = unsafeWindow.jQuery;
  28. var $ = jQuery;
  29.  
  30. var textEdit = '<div id="imdbListTextEdit" style="'
  31. + 'padding: 10px; border: 1px solid #e8e8e8">'
  32. + '<textarea id="filmList" rows="7" cols="60" placeholder="Input titles, IMDb URLs, and IDs here and click Start"></textarea><br />'
  33. + '<input type="button" id="doList" value="Start" /> '
  34. + '<input type="button" id="skipFilm" value="Skip" /> '
  35. + '<input type="button" id="retryPost" value="Retry" /> '
  36. + '<span style="font-weight: bold">Remaining: <span id="filmsRemaining">0</span></span><br /><br />'
  37. + '<span style="font-weight: bold;">Current: <input type="text" id="filmCurrent" size="65" style="font-family: monospace" /></span><br />'
  38. + '<span style="font-weight: bold;">Regexp: <input type="text" value="^(.*)$" id="filmRegexp" size="65" style="font-family: monospace; margin-top: 4px; margin-left: 1px" /></span><br />'
  39. + '</div>';
  40.  
  41. $("div#main > div.list_edit > div.add").after(textEdit);
  42.  
  43. // globals
  44. var films = [];
  45. var regexObj = null;
  46.  
  47. function resetState()
  48. {
  49. films = [];
  50. regexObj = null;
  51. $("#filmList").removeAttr("disabled");
  52. $("#filmRegexp").removeAttr("disabled"); // leave regex
  53. $("#doList").removeAttr("disabled");
  54. $("#filmCurrent").val("");
  55. $("input[name=add]", "div.add").val("");
  56. $("div.results", "div.add").html("");
  57. }
  58.  
  59.  
  60.  
  61. function searchFilm(filmTitle)
  62. {
  63. // remove unnecessary whitespace
  64. filmTitle = $.trim(filmTitle);
  65. // set current text to what we're searching
  66. $("#filmCurrent", "#imdbListTextEdit").val(filmTitle);
  67. // remove the first title from the text box and set the remaining number
  68. $filmList = $("#filmList", "#imdbListTextEdit");
  69. var newList = $filmList.val().split("\n");
  70. $("#filmsRemaining", "#imdbListTextEdit").text(newList.length-1);
  71. $filmList.val( newList.slice(1).join("\n") );
  72. // run regex
  73. if( regexObj != null )
  74. {
  75. filmTitle = regexObj.exec(filmTitle)[1];
  76. }
  77. // set imdb search input field to film title
  78. $("input[name=add]", "div.add").val(filmTitle);
  79. // trigger search button click
  80. $("button.search").triggerHandler("click");
  81. }
  82.  
  83. // when start button is clicked
  84. $("#imdbListTextEdit").delegate("#doList", "click", function(e)
  85. {
  86. $filmList = $("#filmList", "#imdbListTextEdit");
  87. // regex
  88. $regexBox = $("#filmRegexp", "#imdbListTextEdit");
  89. if( $regexBox.val() )
  90. {
  91. regexObj = RegExp( $regexBox.val() );
  92. }
  93. // disable the text area and the button and the regexp box
  94. $filmList.attr("disabled", "disabled");
  95. $regexBox.attr("disabled", "disabled");
  96. $("#doList").attr("disabled", "disabled");
  97. films = $filmList.val().split("\n");
  98. // search first item
  99. searchFilm(films.shift());
  100. });
  101.  
  102. // when skip button is clicked
  103. $("#imdbListTextEdit").delegate("#skipFilm", "click", function(e)
  104. {
  105. // skip to next film
  106. if(films.length > 0)
  107. {
  108. searchFilm(films.shift());
  109. }
  110. else if(films.length == 0) // if no more films...
  111. {
  112. resetState();
  113. }
  114. });
  115.  
  116. // Sometimes the request fails forcing the user to skip an entry to continue
  117. jQuery("#imdbListTextEdit").delegate("#retryPost", "click", function(e)
  118. {
  119. $("button.search").triggerHandler("click");
  120. });
  121.  
  122. // when a search result item is clicked
  123. $("div.results", "div.add").delegate("li", "click", function(e)
  124. {
  125. // if there's more items, search next...
  126. if(films.length > 0)
  127. {
  128. searchFilm(films.shift());
  129. }
  130. else
  131. {
  132. // if last film, reset stuff
  133. resetState();
  134. }
  135. });
  136.  
  137. // monitors for changes to the search result box
  138. // if it recognizes an imdb url/id, it's clicked automatically
  139. // since there's only one result
  140. $("div.results").bind("DOMNodeInserted", function(e)
  141. {
  142. if($("#filmCurrent").val().match(/(tt[0-9]{7})/)[1] && $("div.results").find("li").length)
  143. {
  144. setTimeout(function()
  145. {
  146. $("li", "div.results").trigger("click");
  147. }, REQUEST_DELAY);
  148. }
  149. });