IMDb Search

Search on imdb.com

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

  1. // ==UserScript==
  2. // @name IMDb Search
  3. // @version 1
  4. // @description Search on imdb.com
  5. // @author FuSiOn
  6. // @match https://*/*
  7. // @match http://*/*
  8. // @require http://code.jquery.com/jquery-latest.js
  9. // @grant GM_xmlhttpRequest
  10. // @namespace https://greasyfork.org/users/10999
  11. // ==/UserScript==
  12.  
  13. var IMDb = function(title,year,section,callback,item,retry) {
  14. section = section || "movies"
  15. retry = retry || false;
  16. var GetInfo = function(result, item) {
  17. GM_xmlhttpRequest({
  18. method: "GET",
  19. url: result,
  20. onload: function(response) {
  21. if (response.status == 200) {
  22. if(/\/combined$/.test(response.finalUrl)){
  23. console.log('Please disable the setting "Always display full cast and crew credits" in your account.');
  24. return;
  25. }
  26. var responseData = response.responseText.match(/<div id="title-overview-widget">[\w\W]+?<\/tbody>[\w\W]+?<\/table>[\w\W]+?<\/div>/i)[0]
  27. .replace(/(<img[\w\W]+?src)="http:\/\/ia\.media-imdb\.com([^"]+")/g, '$1New="$2'),
  28. Info = {
  29. "Title": $('.header [itemprop="name"]',responseData).text(),
  30. "Year": $('.header .nobr',responseData).text().replace(/\(|\)/g,""),
  31. "ID": result.match(/tt\d+/)[0],
  32. "Rating": $('span[itemprop="ratingValue"]', responseData).text(),
  33. "Count": $('span[itemprop="ratingCount"]', responseData).text(),
  34. "Genre": "",
  35. "URL": result,
  36. "Poster": "http://ia.media-imdb.com" + $('img[alt*="Poster"]',responseData).attr('srcNew'),
  37. "Trailer": typeof($("[itemprop=trailer]",responseData).attr("href")) == 'undefined' ? '' : "http://imdb.com" + $("[itemprop=trailer]",responseData).attr("href"),
  38. "InWatchList": null,
  39. "InLists": null,
  40. "Description": $('[itemprop="description"]', responseData).text(),
  41. "Stars": $('[itemprop="actors"] a', responseData)
  42. },
  43. logged = /nblogout/.test(response.responseText);
  44. $('span[itemprop="genre"]', responseData).each(function() {
  45. if (Info.Genre !== "") {
  46. Info.Genre += " | ";
  47. }
  48. Info.Genre += $(this).text();
  49. });
  50. if(logged){
  51. GM_xmlhttpRequest({
  52. method: "POST",
  53. url: "http://www.imdb.com/list/_ajax/watchlist_has",
  54. data: "consts%5B%5D=" + Info.ID + "&tracking_tag=wlb-lite",
  55. headers: {
  56. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
  57. },
  58. onload: function(response) {
  59. if(response.status == 200){
  60. var obj = JSON.parse(response.responseText);
  61. if(obj.status === 200){
  62. if(typeof(obj.has[Info.ID]) !== "undefined"){
  63. Info.InWatchList = true;
  64. }else Info.InWatchList = false;
  65. GM_xmlhttpRequest({
  66. method: "POST",
  67. url: "http://www.imdb.com/list/_ajax/wlb_dropdown",
  68. data: "tconst=" + Info.ID,
  69. headers: {
  70. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
  71. },
  72. onload: function(response) {
  73. if(response.status == 200){
  74. var obj = JSON.parse(response.responseText);
  75. if(obj.status === 200){
  76. obj.items.forEach(function(item){
  77. if(item.data_list_item_ids != null){
  78. if(Info.InLists == null) Info.InLists = [];
  79. Info.InLists.push(item.data_list_id)
  80. }
  81. })
  82. setIMDB(Info, item);
  83. }
  84. }
  85. }
  86. });
  87. }
  88. }
  89. }
  90. });
  91. }else setIMDB(Info, item);
  92. }
  93. }
  94. });
  95. };
  96. var setIMDB = function(Info, item) {
  97. if(typeof(callback) != 'function'){
  98. console.log(Info)
  99. return;
  100. }
  101. callback(Info,item);
  102. };
  103. if(!/^tt\d+$/.test(title)){
  104. (function() {
  105. GM_xmlhttpRequest({
  106. method: "GET",
  107. url: "http://akas.imdb.com/find?ref_=nv_sr_fn&s=all&q=" + encodeURIComponent(title) + ((section === "tv")? "&ttype=tv":"&ttype=ft"),
  108. onload: function(response) {
  109. if (response.status == 200) {
  110. if (!/<a name="tt"><\/a>[\w\W]+?<\/table>/i.test(response.responseText)) {
  111. if (/No results found for/.test(response.responseText)) {
  112. console.log("IMDB_Search: No result found for:", title, year);
  113. } else {
  114. console.log("IMDB_Search: A unknown error has occured:", title, year);
  115. }
  116. return;
  117. }
  118. var responseData = response.responseText.match(/<a name="tt"><\/a>[\w\W]+?<\/table>/i)[0]
  119. .replace(/(<img[\w\W]+?src=)"[^"]+"/g, '$1""'),
  120. selector = {
  121. "movies" :'.findResult:containsX("' + title + '"):not(:containsI("(video game)"),:containsI("(tv episode)"),:containsI("(tv series)"),:containsI("(tv mini-series)"),:containsI("(short)"))',
  122. "tv":'.findResult:containsX("' + title + '"):containsI("(TV Series)"),.findResult:containsX("' + title + '"):containsI("(tv series)"),.findResult:containsX("' + title + '"):containsI("(tv mini-series)")',
  123. "console": '.findResult:containsX("' + title + '"):containsI("(Video Game)")'
  124. },
  125. result,
  126. results = $(selector[section], responseData);
  127. if (results.length > 0) {
  128. if (results.length > 1) {
  129. if (year) {
  130. if (results.find(":contains('" + year + "')").length === 0) {
  131. if (results.find(":contains('" + (parseInt(year) - 1) + "')").length === 0) {
  132. results = results.find(":contains('" + (parseInt(year) + 1) + "')");
  133. }else{
  134. results = results.find(":contains('" + (parseInt(year) - 1).toString() + "')");
  135. }
  136. } else {
  137. results = results.find(":contains('" + year + "')");
  138. }
  139. }
  140. if (results.length > 0) {
  141. $('small',results[0]).remove();
  142. result = $(results[0]).find("a").attr("href").replace(/(.+)?\?.+$/, "http://imdb.com$1");
  143. GetInfo(result, item);
  144. } else {
  145. console.log("IMDB_Search: ", "Found no match with the given query and year:", title, year);
  146. }
  147. } else {
  148. $('small',results[0]).remove();
  149. result = $(results[0]).find("a").attr("href").replace(/(.+)?\?.+$/, "http://imdb.com$1");
  150. GetInfo(result, item);
  151. }
  152. } else {
  153. if ($('.findResult', responseData).length === 1) {
  154. results = $('.findResult', responseData);
  155. $('small',results[0]).remove();
  156. result = $(results[0]).find("a").attr("href").replace(/(.+)?\?.+$/, "http://imdb.com$1");
  157. GetInfo(result, item);
  158. } else {
  159. if(section === 'movies' && retry === false && /[\[(][^\])]+[\])]/.test(title)){
  160. IMDB(title.replace(/[\[(][^\])]+[\])]/,""), year, item,true);
  161. }else{
  162. console.log("IMDB_Search: ", "Found no match with the given query:", title, year);
  163. }
  164. }
  165. }
  166. } else {
  167. console.log(response.status + " " + response.statusText);
  168. }
  169. }
  170. });
  171. })();
  172. }else{
  173. GetInfo("http://www.imdb.com/title/" + title,item);
  174. }
  175. };
  176. unsafeWindow.IMDb = IMDb;
  177.  
  178. $.expr[":"].containsI = $.expr.createPseudo(function(arg) {
  179. return function( elem ) {
  180. return $(elem).text().trim().toUpperCase()
  181. .indexOf(arg.trim().toUpperCase()) >= 0;
  182. };
  183. });
  184. $.expr[":"].containsX = $.expr.createPseudo(function(arg) {
  185. return function( elem ) {
  186. return $(elem).text().trim()
  187. .replace(/\s\([IVX]+\)\s/," ")
  188. .replace(/\sII(?:\s|$)/g,"2")
  189. .replace(/\sIII(?:\s|$)/g,"3")
  190. .replace(/\sIV(?:\s|$)/g,"4")
  191. .replace(/\sV(?:\s|$)/g,"5")
  192. .replace(/\sVI(?:\s|$)/g,"6")
  193. .replace(/\sVII(?:\s|$)/g,"7")
  194. .replace(/\sIIX(?:\s|$)/g,"8")
  195. .replace(/\sIX(?:\s|$)/g,"9")
  196. .replace(/\sX(?:\s|$)/g,"10")
  197. .replace(/the\b|part(?=\s?\d)/ig,"")
  198. .replace(/and/ig,"&")
  199. .replace(/äàâ/ig,"A")
  200. .replace(/ç/ig,"C")
  201. .replace(/éèëê/ig,"E")
  202. .replace(/\W/g,"")
  203. .toUpperCase()
  204. .indexOf(arg.trim().replace(/\sII(?:\s|$)/g,"2")
  205. .replace(/\sIII(?:\s|$)/g,"3")
  206. .replace(/\sIV(?:\s|$)/g,"4")
  207. .replace(/\sV(?:\s|$)/g,"5")
  208. .replace(/\sVI(?:\s|$)/g,"6")
  209. .replace(/\sVII(?:\s|$)/g,"7")
  210. .replace(/\sIIX(?:\s|$)/g,"8")
  211. .replace(/\sIX(?:\s|$)/g,"9")
  212. .replace(/\sX(?:\s|$)/g,"10")
  213. .replace(/the\b|part(?=\s?\d)|p(?=\d)|3d/ig,"")
  214. .replace(/and/ig,"&")
  215. .replace(/äàâ/ig,"A")
  216. .replace(/ç/ig,"C")
  217. .replace(/éèëê/ig,"E")
  218. .replace(/\W/g,"")
  219. .toUpperCase()) >= 0;
  220. };
  221. });