IMDb details page links

Adds some links to IMDb details page

当前为 2015-09-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name IMDb details page links
  3. // @namespace IMDb.com
  4. // @description Adds some links to IMDb details page
  5. // @include *imdb.com/title/*
  6. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js
  7. // @require https://cdnjs.cloudflare.com/ajax/libs/jqModal/1.3.0/jqModal.min.js
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @grant GM_registerMenuCommand
  11. // @grant GM_addStyle
  12. // @version 2.0
  13. // ==/UserScript==
  14.  
  15. // CHANGELOG
  16. // 2.0 - Make links modifiable
  17. // 1.1 - Update URLs
  18.  
  19. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/Trim
  20. if(!String.prototype.trim) {
  21. String.prototype.trim = function () {
  22. return this.replace(/^\s+|\s+$/g,'');
  23. };
  24. }
  25.  
  26. function defaultSites() {
  27. return [{title:"Google", url:"http://www.google.com/search?q={title} {year}"},
  28. {title:"Subtitles", url:"http://www.google.com/search?q={title} {year} subtitles"},
  29. {title:"YouTube", url:"http://www.youtube.com/results?search_query={title} {year}"},
  30. {title:"iCheckMovies", url:"http://www.icheckmovies.com/search/movies/?query={imdbid}"},
  31. {title:"RottenTomatoes", url:"http://www.rottentomatoes.com/search/?search={title}"},
  32. {title:"HDBits", url:"http://hdbits.org/browse2.php#film/dir=null&searchtype=film&actorfilm=film&search={imdbid}"},
  33. {title:"AsianDVDClub", url:"http://asiandvdclub.org/browse.php?search={imdbid}&descr=1"},
  34. {title:"PassThePopCorn", url:"http://passthepopcorn.me/torrents.php?searchstr={imdbid}"},
  35. {title:"KaraGarga", url:"http://karagarga.in/browse.php?incldead=&d=&sort=&search={imdbidn}&search_type=imdb"},
  36. {title:"Cinemageddon", url:"http://cinemageddon.net/browse.php?search={imdbid}&proj=0"},
  37. {title:"AsiaTorrents", url:"https://avistaz.to/torrents?in=1&search={title}&tags=&type=0&language=0&subtitle=0&discount=0&rip_type=0&video_quality=0&tv_type=0&uploader="},
  38. {title:"TehConnection", url:"http://tehconnection.eu/torrents.php?searchstr={title}"},
  39. {title:"WhatTheMovie", url:"http://whatthemovie.com/search?t=movie&q={imdbid}"},
  40. {title:"Mubi", url:"http://www.google.com/search?q=site:mubi.com {title} {year}"},
  41. {title:"ListAL", url:"http://www.google.com/search?q=site:listal.com {title} {year}"},
  42. {title:"HanCinema", url:"http://www.hancinema.net/googlesearch.php?cx=partner-pub-1612871806153672%3A2t41l1-gajp&cof=FORID%3A10&ie=ISO-8859-1&hl=en&q={title}"},
  43. {title:"Criticker", url:"http://www.criticker.com/?st=movies&h={imdbid}&g=Go"},
  44. {title:"iCM Highest Rated",url:"http://themagician.host56.com/highestrated/search/#search={imdbid}"}];
  45. }
  46.  
  47. /**
  48. * @brief Find header element
  49. * @return Header element or null if not found
  50. */
  51. function findHeader() {
  52. $header = null;
  53. if(location.href.indexOf("reference") === -1 &&
  54. location.href.indexOf("combined") === -1) {
  55. $overview = $("#overview-top");
  56. if($overview !== null) {
  57. $header = $overview.find(".header:first");
  58. }
  59. }
  60. else {
  61. $header = $("#tn15title");
  62. }
  63. return $header;
  64. }
  65.  
  66. function parseConstants() {
  67. $header = findHeader();
  68. if($header.length === 0) {
  69. return null;
  70. }
  71. var matches = $header.text().replace(/(\r\n|\n|\r)/gm,"").match(/(.*)(?:[\s]+)(?:.*)\(.*([0-9]{4})/);
  72. var imdb_id = window.location.href.match(/(tt[0-9]+)/)[0];
  73. return {title: matches[1].trim(),
  74. year: matches[2].trim(),
  75. imdbid: imdb_id,
  76. imdbid_n: imdb_id.replace("tt", "")};
  77. }
  78.  
  79. var App = {
  80. links: [],
  81. init: function() {
  82. App.buildLinks();
  83. // Add modal
  84. GM_addStyle('.jqmWindow {display: none; position: absolute; font-family: verdana, arial, sans-serif; ' +
  85. 'background-color:#fff; color:#000; padding: 12px 30px; overflow-y: scroll; font-size: 14px} .jqmOverlay { background-color:#000 }');
  86. var cfgMainHtml = '<div id="dialog" class="jqmWindow"></div>';
  87. $(cfgMainHtml).css({
  88. top: '17%', left: '50%', marginLeft: '-400px', width: '800px', height: '450px'
  89. }).appendTo('body');
  90. $('#dialog').jqm();
  91. },
  92. buildLinks: function() {
  93. var sites = GM_getValue("sites");
  94. if(sites !== null) {
  95. sites = eval(sites);
  96. }
  97. else {
  98. GM_setValue("sites", uneval(defaultSites()));
  99. sites = defaultSites();
  100. }
  101. $root = $("#pagecontent");
  102. $root.css("position", "relative");
  103. $("#linkbar").remove();
  104. $c = $('<div></div>');
  105. $c.attr("id", "linkbar");
  106. $c.css({width: "150px", padding: "15px",
  107. textAlign: "right", position: "absolute", top: "8px",
  108. left: "-170px", backgroundColor: "#fff"});
  109. $root.append($c);
  110. // Render sites
  111. for(var i = 0; i < sites.length; ++i) {
  112. App.addSite(sites[i]);
  113. }
  114. },
  115. addSite: function(site) {
  116. var c = parseConstants();
  117. if(c === null) {
  118. return;
  119. }
  120. var url = site.url.replace("{title}", c.title)
  121. .replace("{year}", c.year)
  122. .replace("{imdbid}", c.imdbid)
  123. .replace("{imdbidn}", c.imdbid_n);
  124. $root = $("#linkbar");
  125. $link = $("<a></a>");
  126. $link.attr("href", url);
  127. $link.text(site.title);
  128. $link.css({display: "block", marginBottom: "2px"});
  129. $root.append($link);
  130. },
  131. displayEditor: function() {
  132. var sites = GM_getValue("sites");
  133. if(sites !== null) {
  134. sites = eval(sites);
  135. }
  136. var c = parseConstants();
  137. var out = '<div><p>{title} = ' + c.title + '</p>'
  138. + '<p>{year} = ' + c.year
  139. + ' {imdbid} = ' + c.imdbid
  140. + ' {imdbidn} = ' + c.imdbid_n + '</p><table>';
  141. for(var i = 0; i < sites.length; ++i) {
  142. out += '<tr class="site-' + i + '"><td><a href="#" class="delete">Delete</a></td><td><input type="text" size="30" value="'
  143. + sites[i].title + '"></td><td><input type="text" size="80" value="'
  144. + sites[i].url + '"></td></tr>';
  145. }
  146. out += '</table><p><button id="add">Add new</button> <button id="restore">Restore defaults</button></p>';
  147. $("#dialog").html(out);
  148. $("#dialog").jqmShow();
  149. $("#dialog").on("change input paste", "tr[class^=site] input", App.saveEditor);
  150. $("#dialog").on("click", "a.delete", function(e){
  151. e.preventDefault();
  152. var response = window.confirm("Delete?");
  153. if(!response) {
  154. return;
  155. }
  156. $(this).parent().parent().remove();
  157. App.saveEditor();
  158. });
  159. $("#add", "#dialog").on("click", function() {
  160. var $table = $("table", "#dialog");
  161. $table.append('<tr class="site-' + $table.find("tr").length + '"><td><a href="#" class="delete">Delete</a></td>'
  162. + '<td><input type="text" size="30" value=""></td>'
  163. + '<td><input type="text" size="80" value=""></td></tr>');
  164. });
  165. $("#restore", "#dialog").on("click", function() {
  166. var response = window.confirm("Restore defaults? (This will remove all links!)");
  167. if(!response) {
  168. return;
  169. }
  170. GM_setValue("sites", uneval(defaultSites()));
  171. App.buildLinks();
  172. $("#dialog").jqmHide();
  173. });
  174. },
  175. saveEditor: function() {
  176. // Save result
  177. var $rows = $("tr", "#dialog");
  178. var mapped = $rows.map(function(index, elem){
  179. var $fields = $(elem).find("input");
  180. return {title: $fields.eq(0).val(), url: $fields.eq(1).val()};
  181. });
  182. GM_setValue("sites", uneval(mapped.get()));
  183. App.buildLinks();
  184. }
  185. };
  186.  
  187. $(window).ready(App.init);
  188.  
  189. GM_registerMenuCommand("Edit sites", App.displayEditor);