Show Metacritic.com ratings

Show metacritic metascore and user ratings on: Bandcamp, Apple Itunes (Music), Amazon (Music,Movies,TV Shows), IMDb (Movies), Google Play (Music, Movies), TV.com, Steam, Gamespot (PS4, XONE, PC), Rotten Tomatoes, Serienjunkies, BoxOfficeMojo, allmovie.com, movie.com, Wikipedia (en), themoviedb.org, letterboxd, TVmaze, TVGuide

当前为 2015-12-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Show Metacritic.com ratings
  3. // @description Show metacritic metascore and user ratings on: Bandcamp, Apple Itunes (Music), Amazon (Music,Movies,TV Shows), IMDb (Movies), Google Play (Music, Movies), TV.com, Steam, Gamespot (PS4, XONE, PC), Rotten Tomatoes, Serienjunkies, BoxOfficeMojo, allmovie.com, movie.com, Wikipedia (en), themoviedb.org, letterboxd, TVmaze, TVGuide
  4. // @namespace cuzi
  5. // @oujs:author cuzi
  6. // @grant GM_xmlhttpRequest
  7. // @grant GM_getResourceURL
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // @grant unsafeWindow
  11. // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
  12. // @resource global.min.css http://www.metacritic.com/css/global.min.1446760484.css
  13. // @resource base.min.css http://www.metacritic.com/css/search/base.min.1446760407.css
  14. // @license GNUGPL
  15. // @version 5
  16. // @include https://*.bandcamp.com/*
  17. // @include https://itunes.apple.com/*/album/*
  18. // @include https://play.google.com/store/music/album/*
  19. // @include https://play.google.com/store/movies/details/*
  20. // @include http://www.amazon.com/*
  21. // @include https://www.amazon.com/*
  22. // @include http://www.amazon.co.uk/*
  23. // @include https://www.amazon.co.uk/*
  24. // @include http://www.amazon.fr/*
  25. // @include https://www.amazon.fr/*
  26. // @include http://www.amazon.de/*
  27. // @include https://www.amazon.de/*
  28. // @include http://www.amazon.es/*
  29. // @include https://www.amazon.es/*
  30. // @include http://www.amazon.ca/*
  31. // @include https://www.amazon.ca/*
  32. // @include http://www.amazon.in/*
  33. // @include https://www.amazon.in/*
  34. // @include http://www.amazon.it/*
  35. // @include https://www.amazon.it/*
  36. // @include http://www.amazon.co.jp/*
  37. // @include https://www.amazon.co.jp/*
  38. // @include http://www.amazon.com.mx/*
  39. // @include https://www.amazon.com.mx/*
  40. // @include http://www.amazon.com.au/*
  41. // @include https://www.amazon.com.au/*
  42. // @include http://www.imdb.com/title/*
  43. // @include https://www.imdb.com/title/*
  44. // @include http://store.steampowered.com/app/*
  45. // @include https://store.steampowered.com/app/*
  46. // @include http://www.gamespot.com/*
  47. // @include https://www.gamespot.com/*
  48. // @include http://www.serienjunkies.de/*
  49. // @include https://www.serienjunkies.de/*
  50. // @include http://www.tv.com/shows/*
  51. // @include http://www.rottentomatoes.com/m/*
  52. // @include https://www.rottentomatoes.com/m/*
  53. // @include http://www.rottentomatoes.com/tv/*
  54. // @include https://www.rottentomatoes.com/tv/*
  55. // @include http://www.boxofficemojo.com/movies/*
  56. // @include http://www.allmovie.com/movie/*
  57. // @include https://en.wikipedia.org/*
  58. // @include http://www.movies.com/*/m*
  59. // @include https://www.themoviedb.org/movie/*
  60. // @include https://www.themoviedb.org/tv/*
  61. // @include http://letterboxd.com/film/*
  62. // @include https://letterboxd.com/film/*
  63. // @include http://www.tvmaze.com/shows/*
  64. // @include http://www.tvguide.com/tvshows/*
  65. // @include https://www.tvguide.com/tvshows/*
  66. // ==/UserScript==
  67.  
  68. var baseURL = "http://www.metacritic.com/";
  69.  
  70. var baseURL_music = "http://www.metacritic.com/music/";
  71. var baseURL_movie = "http://www.metacritic.com/movie/";
  72. var baseURL_pcgame = "http://www.metacritic.com/game/pc/";
  73. var baseURL_ps4 = "http://www.metacritic.com/game/playstation-4/";
  74. var baseURL_xone = "http://www.metacritic.com/game/xbox-one/";
  75. var baseURL_tv = "http://www.metacritic.com/tv/";
  76.  
  77. var baseURL_search = "http://www.metacritic.com/search/{type}/{query}/results";
  78. var baseURL_autosearch = "http://www.metacritic.com/autosearch";
  79.  
  80. var mybrowser = "other";
  81. if(~navigator.userAgent.indexOf("Chrome")) {
  82. mybrowser = "chrome";
  83. }
  84.  
  85.  
  86. // http://www.designcouch.com/home/why/2013/05/23/dead-simple-pure-css-loading-spinner/
  87. var CSS = "#mcdiv123 .grespinner{height:16px;width:16px;margin:0 auto;position:relative;-webkit-animation:rotation .6s infinite linear;-moz-animation:rotation .6s infinite linear;-o-animation:rotation .6s infinite linear;animation:rotation .6s infinite linear;border-left:6px solid rgba(0,174,239,.15);border-right:6px solid rgba(0,174,239,.15);border-bottom:6px solid rgba(0,174,239,.15);border-top:6px solid rgba(0,174,239,.8);border-radius:100%}@-webkit-keyframes rotation{from{-webkit-transform:rotate(0)}to{-webkit-transform:rotate(359deg)}}@-moz-keyframes rotation{from{-moz-transform:rotate(0)}to{-moz-transform:rotate(359deg)}}@-o-keyframes rotation{from{-o-transform:rotate(0)}to{-o-transform:rotate(359deg)}}@keyframes rotation{from{transform:rotate(0)}to{transform:rotate(359deg)}}#mcdiv123searchresults .result{font:12px arial,helvetica,serif;border-top-width:1px;border-top-color:#ccc;border-top-style:solid;padding:5px}#mcdiv123searchresults .result .result_type{display:inline}#mcdiv123searchresults .result .result_wrap{float:left;width:100%}#mcdiv123searchresults .result .has_score{padding-left:42px}#mcdiv123searchresults .result .basic_stats{height:1%;overflow:hidden}#mcdiv123searchresults .result h3{font-size:14px;font-weight:700}#mcdiv123searchresults .result a{color:#09f;font-weight:700;text-decoration:none}#mcdiv123searchresults .metascore_w.game.seventyfive,#mcdiv123searchresults .metascore_w.positive,#mcdiv123searchresults .metascore_w.score_favorable,#mcdiv123searchresults .metascore_w.score_outstanding,#mcdiv123searchresults .metascore_w.sixtyone{background-color:#6c3}#mcdiv123searchresults .metascore_w.forty,#mcdiv123searchresults .metascore_w.game.fifty,#mcdiv123searchresults .metascore_w.mixed,#mcdiv123searchresults .metascore_w.score_mixed{background-color:#fc3}#mcdiv123searchresults .metascore_w.negative,#mcdiv123searchresults .metascore_w.score_terrible,#mcdiv123searchresults .metascore_w.score_unfavorable{background-color:red}#mcdiv123searchresults a.metascore_w,#mcdiv123searchresults span.metascore_w{display:inline-block}#mcdiv123searchresults .result .metascore_w{color:#fff!important;font-family:Arial,Helvetica,sans-serif;font-size:17px;font-style:normal!important;font-weight:700!important;height:2em;line-height:2em;text-align:center;vertical-align:middle;width:2em;float:left;margin:0 0 0 -42px}#mcdiv123searchresults .result .more_stats{font-size:10px;color:#444}#mcdiv123searchresults .result .release_date .data{font-weight:700;color:#000}#mcdiv123searchresults ol,#mcdiv123searchresults ul{list-style:none}#mcdiv123searchresults .result li.stat{background:0 0;display:inline;float:left;margin:0;padding:0 6px 0 0;white-space:nowrap}#mcdiv123searchresults .result .deck{margin:3px 0 0}#mcdiv123searchresults .result .basic_stat{display:inline;float:right;overflow:hidden;width:100%}";
  88.  
  89. function name2metacritic(s) {
  90. return s.normalize('NFKD').replace(/\//g,"").replace(/[\u0300-\u036F]/g, '').replace(/&/g,"and").replace(/\W+/g, " ").toLowerCase().trim().replace(/\W+/g,"-");
  91. }
  92. function minutesSince(time) {
  93. var seconds = ((new Date()).getTime() - time.getTime()) / 1000;
  94. return seconds>60?parseInt(seconds/60)+" min ago":"now";
  95. }
  96. function randomStringId() {
  97. var id10 = () => Math.floor((1 + Math.random()) * 0x10000000000).toString(16).substring(1);
  98. return id10()+id10()+id10()+id10()+id10()+id10();
  99. }
  100. function fixMetacriticURLs(html) {
  101. return html.replace(/<a /g,'<a target="_blank" ').replace(/href="\//g,'href="'+baseURL).replace(/src="\//g,'src="'+baseURL);
  102. }
  103. function searchType2metacritic(type) {
  104. return ({
  105. 'movie' : 'movie',
  106. 'pcgame' : 'game',
  107. 'xonegame' : 'game',
  108. 'ps4game' : 'game',
  109. 'music' : 'album',
  110. 'tv' : 'tv'
  111. })[type];
  112. }
  113. function metacritic2searchType(type) {
  114. return ({
  115. "Album" : "music",
  116. "TV" : "tv",
  117. "Movie" : "movie",
  118. "PC Game" : "pcgame",
  119. "PS4 Game" : "ps4game",
  120. "XONE Game" : "onegame",
  121. "WIIU Game" : "xxxxx",
  122. "3DS Game" : "xxxx"
  123. })[type];
  124. }
  125.  
  126.  
  127. function metaScore(score, word) {
  128. var fg,bg,t;
  129. if(score == null) {
  130. fg = "black";
  131. bg = "#ccc";
  132. t = "tbd";
  133. } else if(score >= 75) {
  134. fg = "white";
  135. bg = "#6c3";
  136. t = parseInt(score);
  137. } else if(score < 40) {
  138. fg = "white";
  139. bg = "#f00";
  140. t = parseInt(score);
  141. } else {
  142. fg = "white";
  143. bg = "#fc3";
  144. t = parseInt(score);
  145. }
  146. return '<span title="'+(word?word:'')+'" style="display: inline-block; color: '+fg+';background:'+bg+';font-family: Arial,Helvetica,sans-serif;font-size: 17px;font-style: normal;font-weight: bold;height: 2em;width: 2em;line-height: 2em;text-align: center;vertical-align: middle;">'+t+'</span>';
  147. }
  148.  
  149. function filterUniversalUrl(url) {
  150. url = url.match(/http.+/)[0].replace(/https?:\/\/(www.)?/,"");
  151. if(url.startsWith("somehost")) {// TODO
  152. return url; // Do not remove parameters
  153. } else {
  154. return url.split("?")[0].split("&")[0]; // Remove parameters
  155. }
  156. }
  157.  
  158. function addToMap(url, metaurl) {
  159. var data = JSON.parse(GM_getValue("map","{}"));
  160. var url = filterUniversalUrl(url);
  161. var metaurl = metaurl.replace(/^http:\/\/(www.)?metacritic\.com\//,"");
  162.  
  163. data[url] = metaurl;
  164. GM_setValue("map", JSON.stringify(data));
  165. (new Image()).src = "http://123.net23.net/whitelist.php?docurl="+encodeURIComponent(url)+"&metaurl="+encodeURIComponent(metaurl)+"&ref="+encodeURIComponent(randomStringId());
  166. }
  167.  
  168. function addToBlacklist(url, metaurl) {
  169. var data = JSON.parse(GM_getValue("black","{}"));
  170. var url = filterUniversalUrl(url);
  171. var metaurl = metaurl.replace(/^http:\/\/(www.)?metacritic\.com\//,"");
  172.  
  173. data[url] = metaurl;
  174. GM_setValue("black", JSON.stringify(data));
  175. (new Image()).src = "http://123.net23.net/blacklist.php?docurl="+encodeURIComponent(url)+"&metaurl="+encodeURIComponent(metaurl)+"&ref="+encodeURIComponent(randomStringId());
  176. }
  177.  
  178. function isBlacklistedUrl(docurl, metaurl) {
  179. docurl = docurl.replace(/https?:\/\/(www.)?/,"");
  180. metaurl = metaurl.replace(/^http:\/\/(www.)?metacritic\.com\//,"");
  181. metaurl = metaurl.replace(/\/\//g,"/").replace(/\/\//g,"/");; // remove double slash
  182. var data = JSON.parse(GM_getValue("black","{}"));
  183. if(docurl in data) {
  184. if(data[docurl] == metaurl) {
  185. return true;
  186. }
  187. }
  188. return false;
  189. }
  190.  
  191. function isBlacklisted(metaurl) {
  192. return isBlacklistedUrl("" + document.location.host.replace(/^www\./,"") + document.location.pathname + document.location.search, metaurl);
  193. }
  194.  
  195.  
  196.  
  197. function listenForHotkeys(code, cb) {
  198. // Call cb() as soon as the code sequence was typed
  199. var i = 0;
  200. $(document).bind("keydown.listenForHotkeys",function(ev) {
  201. if(document.activeElement == document.body) {
  202. if(ev.key != code[i]) {
  203. i = 0;
  204. } else {
  205. i++;
  206. if(i == code.length) {
  207. ev.preventDefault();
  208. $(document).unbind("keydown.listenForHotkeys");
  209. cb();
  210. }
  211. }
  212. }
  213. });
  214. }
  215.  
  216.  
  217. function metacritic_hoverInfo(url, cb, errorcb) {
  218. // Get the metacritic hover info. Requests are cached.
  219. var handleresponse = function(response, cached) {
  220. if(response.status == 200 && cb) {
  221. if(~response.responseText.indexOf('"jsonRedirect"')) { // {"viewer":{},"mixpanelToken":"6e219fd....","mixpanelDistinctId":"255.255.255.255","omnitureDebug":0,"jsonRedirect":"\/movie\/national-lampoons-vacation"}
  222. var j = JSON.parse(response.responseText);
  223. current.url = baseURL + j["jsonRedirect"];
  224. metacritic_hoverInfo(baseURL + j["jsonRedirect"], cb, errorcb);
  225. } else {
  226. cb(response.responseText, new Date(response.time));
  227. }
  228. } else if(response.status != 200 && errorcb) {
  229. errorcb(response.responseText, new Date(response.time));
  230. if(!cached)
  231. console.log("Show metacritic ratings: Error:"+response.status+"\n"+url);
  232. }
  233. };
  234. var cache = JSON.parse(GM_getValue("hovercache","{}"));
  235. for(var prop in cache) {
  236. // Delete cached values, that are older than 2 hours
  237. if((new Date()).getTime() - (new Date(cache[prop].time)).getTime() > 2*60*60*1000) {
  238. delete cache[prop];
  239. }
  240. }
  241. if(url in cache) {
  242. handleresponse(cache[url], true);
  243. } else {
  244. GM_xmlhttpRequest({
  245. method: "POST",
  246. url: url,
  247. data: "hoverinfo=1",
  248. headers: {
  249. "Referer" : url,
  250. "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8",
  251. "Host" : "www.metacritic.com",
  252. "User-Agent" : "MetacriticUserscript "+navigator.userAgent,
  253. "X-Requested-With" : "XMLHttpRequest"
  254. },
  255. onload: function(response) {
  256. response.time = (new Date()).toJSON();
  257. cache[url] = response;
  258. GM_setValue("hovercache",JSON.stringify(cache));
  259. handleresponse(response, false);
  260. }
  261. });
  262. }
  263. }
  264. function metacritic_searchResults(url, cb, errorcb) {
  265. // Get metacritic search results. Requests are cached.
  266. var handleresponse = function(response, cached) {
  267. if(response.results.length && cb) {
  268. cb(response.results, new Date(response.time));
  269. } else if(response.results.length == 0 && errorcb) {
  270. errorcb(response.results, new Date(response.time));
  271. }
  272. };
  273. var cache = JSON.parse(GM_getValue("searchcache","{}"));
  274. for(var prop in cache) {
  275. // Delete cached values, that are older than 2 hours
  276. if((new Date()).getTime() - (new Date(cache[prop].time)).getTime() > 2*60*60*1000) {
  277. delete cache[prop];
  278. }
  279. }
  280. if(url in cache) {
  281. handleresponse(cache[url], true);
  282. } else {
  283. GM_xmlhttpRequest({
  284. method: "GET",
  285. url: url,
  286. headers: {
  287. "Referer" : url,
  288. "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8",
  289. "Host" : "www.metacritic.com",
  290. "User-Agent" : "MetacriticUserscript "+navigator.userAgent,
  291. },
  292. onload: function(response) {
  293. var results = [];
  294. if(!~response.responseText.indexOf("No search results found.")) {
  295. var d = $('<html>').html(response.responseText);
  296. d.find("ul.search_results.module .result").each(function() {
  297. results.push(this.innerHTML);
  298. });
  299. }
  300.  
  301. response = {
  302. time : (new Date()).toJSON(),
  303. results : results,
  304. };
  305. cache[url] = response;
  306. GM_setValue("searchcache",JSON.stringify(cache));
  307. handleresponse(response, false);
  308. },
  309. onerror: function(response) {
  310. alert(response.responseText);
  311. console.log("Show metacritic ratings: Search error: "+response.status+"\n"+url);
  312. handleresponse({
  313. time : (new Date()).toJSON(),
  314. results : [],
  315. }, false);
  316. }
  317. });
  318. }
  319. }
  320.  
  321. function metacritic_showHoverInfo(url) {
  322. if(!url) {
  323. return;
  324. }
  325. metacritic_hoverInfo(url,
  326. // On Success
  327. function(html, time) {
  328. $("#mcdiv123").remove();
  329. var div = $('<div id="mcdiv123"></div>').appendTo(document.body);
  330. div.css({
  331. position:"fixed",
  332. bottom :0,
  333. left: 0,
  334. minWidth: 300,
  335. backgroundColor: "#fff",
  336. border: "2px solid #bbb",
  337. borderRadius:" 6px",
  338. boxShadow: "0 0 3px 3px rgba(100, 100, 100, 0.2)",
  339. color: "#000",
  340. padding:" 3px",
  341. zIndex: "5010001",
  342. });
  343. // Functions for communication between page and iframe
  344. // Mozilla can access parent.document
  345. // Chrome can use postMessage()
  346. var functions = {
  347. "other" : {
  348. "parent": function() {},
  349. "frame" : function sizecorrection() {
  350. var f = parent.document.getElementById('mciframe123');
  351. for(var i =0; f.clientHeight < document.body.scrollHeight && i < 100; i++) {
  352. f.style.width = parseInt(f.style.width)+10+"px";
  353. }
  354. if(f.clientHeight < document.body.scrollHeight) {
  355. f.style.height = parseInt(f.style.height)+15+"px";
  356. f.style.width = "300px";
  357. sizecorrection();
  358. }
  359. }
  360. },
  361. "chrome" : {
  362. "parent" : function() {
  363. var f = parent.document.getElementById('mciframe123');
  364. window.addEventListener("message", function(e){
  365. if("mcimessage1" in e.data) {
  366. f.style.width = parseInt(f.style.width)+10+"px";
  367. } else if("mcimessage2" in e.data) {
  368. f.style.height = parseInt(f.style.height)+15+"px";
  369. f.style.width = "300px";
  370. } else {
  371. return;
  372. }
  373. f.contentWindow.postMessage({
  374. "mcimessage3" : true,
  375. "mciframe123_clientHeight" : f.clientHeight,
  376. "mciframe123_clientWidth" : f.clientWidth,
  377. },'*');
  378. });
  379. },
  380. "frame" : function() {
  381. var i = 0;
  382. window.addEventListener("message", function(e){
  383. if(!("mcimessage3" in e.data)) return;
  384. if(e.data.mciframe123_clientHeight < document.body.scrollHeight && i < 100) {
  385. parent.postMessage({"mcimessage1":1},'*');
  386. i++;
  387. }
  388. if(i >= 100) {
  389. parent.postMessage({"mcimessage2":1},'*')
  390. i = 0;
  391. }
  392. });
  393. parent.postMessage({"mcimessage1":1},'*');
  394. }
  395. }
  396. };
  397. var framesrc = 'data:text/html,';
  398. framesrc += encodeURIComponent('<!DOCTYPE html>\
  399. <html lang="en">\
  400. <head>\
  401. <meta charset="utf-8">\
  402. <title>Metacritic info</title>\
  403. <link rel="stylesheet" href="'+(mybrowser=="chrome"?"data:text/css;base64,":"")+GM_getResourceURL("base.min.css")+'" type="text/css">\
  404. <link rel="stylesheet" href="'+(mybrowser=="chrome"?"data:text/css;base64,":"")+GM_getResourceURL("global.min.css")+'" type="text/css">\
  405. <style>body { margin:0px; padding:0px; background:white; }</style>\
  406. <script>\
  407. function on_load() {\
  408. ('+functions[mybrowser].frame.toString()+')();\
  409. }\
  410. </script>\
  411. </head>\
  412. <body onload="on_load();">\
  413. <div style="border:0px solid; display:block; position:relative; border-radius:0px; padding:0px; margin:0px; box-shadow:none;" class="hover_div" id="hover_div">\
  414. <div class="hover_content">'+fixMetacriticURLs(html)+'</div>\
  415. </div>\
  416. </body>\
  417. </html>');
  418.  
  419. var frame = $("<iframe></iframe>").appendTo(div);
  420. frame.attr("id","mciframe123");
  421. frame.attr("src",framesrc);
  422. frame.attr("scrolling","auto");
  423. frame.css({
  424. width: 300,
  425. height: 170,
  426. border: "none"
  427. });
  428. functions[mybrowser].parent();
  429. var sub = $("<div></div>").appendTo(div);
  430. $('<time style="color:#b6b6b6; font-size: 11px;" datetime="'+time+'" title="'+time.toLocaleFormat()+'">'+minutesSince(time)+'</time>').appendTo(sub);
  431. $('<a style="color:#b6b6b6; font-size: 11px;" target="_blank" href="'+url+'" title="Open Metacritic">'+decodeURI(url.replace("http://www.","@"))+'</a>').appendTo(sub);
  432. $('<span title="Hide me" style="cursor:pointer; float:right; color:#b6b6b6; font-size: 11px;">&#128128;</span>').appendTo(sub).click(function() {
  433. document.body.removeChild(this.parentNode.parentNode);
  434. });
  435. $('<span title="This is the correct entry" style="cursor:pointer; float:right; color:green; font-size: 11px;">&check;</span>').data("url", url).appendTo(sub).click(function() {
  436. var docurl = document.location.href;
  437. var metaurl = $(this).data("url");
  438. addToMap(docurl,metaurl);
  439. alert("Saved to correct list!\n\n"+docurl+"\n"+metaurl);
  440. });
  441. $('<span title="This is NOT the correct entry" style="cursor:pointer; float:right; color:crimson; font-size: 11px;">&cross;</span>').data("url", url).appendTo(sub).click(function() {
  442. if(!confirm("This is NOT the correct entry!\n\nAdd to blacklist?")) return;
  443. var docurl = document.location.href;
  444. var metaurl = $(this).data("url");
  445. addToBlacklist(docurl,metaurl);
  446. alert("Saved to blacklist!\n\n"+docurl+"\n"+metaurl);
  447. // Open search
  448. metacritic_searchcontainer(null, current.searchTerm);
  449. metacritic_search(null, current.searchTerm);
  450. });
  451.  
  452. },
  453. // On error i.e. no result on metacritic.com
  454. function(html, time) {
  455. // Make search available
  456. metacritic_waitForHotkeys();
  457. var handleresponse = function(response) {
  458. var data;
  459. try {
  460. data = JSON.parse(response.responseText);
  461. } catch(e) {
  462. console.log("Error in JSON: search_term="+current.searchTerm);
  463. console.log(e);
  464. }
  465. if(data && data.autoComplete && data.autoComplete.length) {
  466. // Remove data with wrong type
  467. var newdata = [];
  468. data.autoComplete.forEach(function(result) {
  469. if(metacritic2searchType(result.refType) == current.type) {
  470. newdata.push(result);
  471. }
  472. });
  473. data.autoComplete = newdata;
  474. if(data.autoComplete.length == 0) {
  475. // No results
  476. console.log("No results (after filtering by type) for search_term="+current.searchTerm);
  477. } else if(data.autoComplete.length == 1) {
  478. // One result, let's show it
  479. if(!isBlacklisted(baseURL + data.autoComplete[0].url)) {
  480. metacritic_showHoverInfo(baseURL + data.autoComplete[0].url);
  481. return;
  482. }
  483. } else {
  484. // More than one result
  485. console.log("Multiple results for search_term="+current.searchTerm);
  486. var exactMatches = [];
  487. data.autoComplete.forEach(function(result,i) { // Try to find the correct result by matching the search term to exactly one movie title
  488. if(current.searchTerm == result.name) {
  489. exactMatches.push(result);
  490. }
  491. });
  492. if(exactMatches.length == 1) {
  493. // Only one exact match, let's show it
  494. if(!isBlacklisted(baseURL + exactMatches[0].url)) {
  495. metacritic_showHoverInfo(baseURL + exactMatches[0].url);
  496. return;
  497. }
  498. }
  499. }
  500. }
  501. // HERE: multiple results or no result. The user may type "meta" now
  502. };
  503. var cache = JSON.parse(GM_getValue("autosearchcache","{}"));
  504. for(var prop in cache) {
  505. // Delete cached values, that are older than 2 hours
  506. if((new Date()).getTime() - (new Date(cache[prop].time)).getTime() > 2*60*60*1000) {
  507. delete cache[prop];
  508. }
  509. }
  510. current.searchTerm = current.data.join(" ");
  511. if(current.searchTerm in cache) {
  512. handleresponse(cache[current.searchTerm], true);
  513. } else {
  514. GM_xmlhttpRequest({
  515. method: "POST",
  516. url: baseURL_autosearch,
  517. data: "search_term="+encodeURIComponent(current.searchTerm),
  518. headers: {
  519. "Referer" : url,
  520. "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8",
  521. "Host" : "www.metacritic.com",
  522. "User-Agent" : "MetacriticUserscript Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0",
  523. "X-Requested-With" : "XMLHttpRequest"
  524. },
  525. onload: function(response) {
  526. response = {
  527. time : (new Date()).toJSON(),
  528. responseText : response.responseText,
  529. };
  530. cache[current.searchTerm] = response;
  531. GM_setValue("autosearchcache",JSON.stringify(cache));
  532. handleresponse(response, false);
  533. }
  534. });
  535. }
  536. });
  537. }
  538.  
  539. function metacritic_waitForHotkeys() {
  540. listenForHotkeys("meta",metacritic_searchcontainer);
  541. }
  542.  
  543. function metacritic_searchcontainer(ev, query) {
  544. if(!query) {
  545. query = current.data.join(" ");
  546. }
  547. $("#mcdiv123").remove();
  548. var div = $('<div id="mcdiv123"></div>').appendTo(document.body);
  549. div.css({
  550. position:"fixed",
  551. bottom :0,
  552. left: 0,
  553. minWidth: 300,
  554. maxHeight: "80%",
  555. maxWidth: 640,
  556. overflow:"auto",
  557. backgroundColor: "#fff",
  558. border: "2px solid #bbb",
  559. borderRadius:" 6px",
  560. boxShadow: "0 0 3px 3px rgba(100, 100, 100, 0.2)",
  561. color: "#000",
  562. padding:" 3px",
  563. zIndex: "5010001",
  564. });
  565. var query = $('<input type="text" size="60" id="mcisearchquery">').appendTo(div).focus().val(query).on('keypress', function(e) {
  566. var code = e.keyCode || e.which;
  567. if(code == 13) { // Enter key
  568. metacritic_search.call(this,e);
  569. }
  570. });
  571. $('<button id="mcisearchbutton">').text("Search").appendTo(div).click(metacritic_search);
  572. }
  573.  
  574.  
  575. function metacritic_search(ev, query) {
  576. if(!query) { // Use values from search form
  577. query = $("#mcisearchquery").val();
  578. }
  579. var type = current.type;
  580.  
  581. var style = document.createElement('style');
  582. style.type = 'text/css';
  583. style.innerHTML = CSS;
  584. document.head.appendChild(style);
  585. var div = $("#mcdiv123");
  586. var loader = $('<div style="width:20px; height:20px;" class="grespinner"></div>').appendTo($("#mcisearchbutton"));
  587. var url = baseURL_search.replace("{type}",encodeURIComponent(type)).replace("{query}",encodeURIComponent(query));
  588. metacritic_searchResults(url,
  589. // On success
  590. function(results, time) {
  591. loader.remove();
  592. var accept = function(ev) {
  593. var a = $(this.parentNode).find("a[href*='metacritic.com']");
  594. var metaurl = a.attr("href");
  595. var docurl = document.location.href;
  596.  
  597. addToMap(docurl,metaurl);
  598. metacritic_showHoverInfo(metaurl);
  599. };
  600. var denyAll = function(ev) {
  601. var urls = [];
  602. var docurl = document.location.href;
  603. $("#mcdiv123searchresults").find("div.result a[href*='metacritic.com']").each(function() {
  604. addToBlacklist(docurl, this.href);
  605. });
  606. };
  607. var resultdiv = $("#mcdiv123searchresults").length?$("#mcdiv123searchresults").html(""):$('<div id="mcdiv123searchresults"></div>').css("max-width","95%").appendTo(div);
  608. results.forEach(function(html) {
  609. var singleresult = $('<div class="result"></div>').html(fixMetacriticURLs(html)+'<div style="clear:left"></div>').appendTo(resultdiv);
  610. $('<span title="This is the correct entry" style="cursor:pointer; color:green; font-size: 13px;">&check;</span>').prependTo(singleresult).click(accept);
  611. });
  612. var sub = $("<div></div>").appendTo(div);
  613. $('<time style="color:#b6b6b6; font-size: 11px;" datetime="'+time+'" title="'+time.toLocaleFormat()+'">'+minutesSince(time)+'</time>').appendTo(sub);
  614. $('<a style="color:#b6b6b6; font-size: 11px;" target="_blank" href="'+url+'" title="Open Metacritic">'+decodeURI(url.replace("http://www.","@"))+'</a>').appendTo(sub);
  615. $('<span title="Hide me" style="cursor:pointer; float:right; color:#b6b6b6; font-size: 11px;">&#128128;</span>').appendTo(sub).click(function() {
  616. document.body.removeChild(this.parentNode.parentNode);
  617. });
  618. $('<span title="None of the above is the correct item" style="cursor:pointer; float:right; color:crimson; font-size: 11px;">&cross;</span>').appendTo(sub).click(function() {if(confirm("None of the above is the correct item\nConfirm?")) denyAll()});
  619. },
  620. // On error i.e. no results
  621. function(results, time) {
  622. loader.remove();
  623. var resultdiv = $("#mcdiv123searchresults").length?$("#mcdiv123searchresults").html(""):$('<div id="mcdiv123searchresults"></div>').appendTo(div);
  624. resultdiv.html("No search results.");
  625. var sub = $("<div></div>").appendTo(div);
  626. $('<time style="color:#b6b6b6; font-size: 11px;" datetime="'+time+'" title="'+time.toLocaleFormat()+'">'+minutesSince(time)+'</time>').appendTo(sub);
  627. $('<a style="color:#b6b6b6; font-size: 11px;" target="_blank" href="'+url+'" title="Open Metacritic">'+decodeURI(url.replace("http://www.","@"))+'</a>').appendTo(sub);
  628. $('<span title="Hide me" style="cursor:pointer; float:right; color:#b6b6b6; font-size: 11px;">&#128128;</span>').appendTo(sub).click(function() {
  629. document.body.removeChild(this.parentNode.parentNode);
  630. });
  631. }
  632. );
  633. }
  634.  
  635. var current = {
  636. url : null,
  637. type : null,
  638. data : null, // Array of raw search keys
  639. searchTerm : null
  640. };
  641.  
  642.  
  643. function showURL(url) {
  644. if(!isBlacklisted(url)) {
  645. metacritic_showHoverInfo(url);
  646. } else {
  647. console.log(url +" is blacklisted!");
  648. }
  649. }
  650.  
  651.  
  652. var metacritic = {
  653. "mapped" : function metacritic_mapped(url, type) {
  654. // url was in the map/whitelist
  655. current.data = [url]
  656. current.url = url;
  657. current.type = type;
  658. current.searchTerm = url;
  659. showURL(url);
  660. },
  661. "music" : function metacritic_music(artistname, albumname) {
  662. current.data = [albumname.trim(),artistname.trim()]
  663. artistname = name2metacritic(artistname);
  664. albumname = name2metacritic(albumname);
  665. var url = baseURL_music + albumname + "/" + artistname;
  666. current.url = url;
  667. current.type = "music";
  668. current.searchTerm = albumname + "/" + artistname;
  669. showURL(url);
  670. },
  671. "movie" : function metacritic_movie(moviename) {
  672. current.data = [moviename.trim()]
  673. moviename = name2metacritic(moviename);
  674. var url = baseURL_movie + moviename;
  675. current.url = url;
  676. current.type = "movie";
  677. current.searchTerm = moviename;
  678. showURL(url);
  679. },
  680. "tv" : function metacritic_tv(seriesname) {
  681. current.data = [seriesname.trim()]
  682. seriesname = name2metacritic(seriesname);
  683. var url = baseURL_tv + seriesname;
  684. current.url = url;
  685. current.type = "tv";
  686. current.searchTerm = seriesname;
  687. showURL(url);
  688. },
  689. "pcgame" : function metacritic_pcgame(gamename) {
  690. current.data = [gamename.trim()]
  691. gamename = name2metacritic(gamename);
  692. var url = baseURL_pcgame + gamename;
  693. current.url = url;
  694. current.type = "pcgame";
  695. current.searchTerm = gamename;
  696. showURL(url);
  697. },
  698. "ps4game" : function metacritic_ps4game(gamename) {
  699. current.data = [gamename.trim()]
  700. gamename = name2metacritic(gamename);
  701. var url = baseURL_ps4 + gamename;
  702. current.url = url;
  703. current.type = "ps4game";
  704. current.searchTerm = gamename;
  705. showURL(url);
  706. },
  707. "xonegame" : function metacritic_xonegame(gamename) {
  708. current.data = [gamename.trim()]
  709. gamename = name2metacritic(gamename);
  710. var url = baseURL_xone + gamename;
  711. current.url = url;
  712. current.type = "xonegame";
  713. current.searchTerm = gamename;
  714. showURL(url);
  715. }
  716. };
  717.  
  718.  
  719. var Always = () => true;
  720. var sites = {
  721. 'bandcamp' : {
  722. host : ["bandcamp.com"],
  723. condition : function() {
  724. return unsafeWindow.TralbumData
  725. },
  726. products : [{
  727. condition : Always,
  728. type : "music",
  729. data : () => [unsafeWindow.TralbumData.artist, unsafeWindow.TralbumData.current.title]
  730. }]
  731. },
  732. 'itunes' : {
  733. host : ["itunes.apple.com"],
  734. condition : Always,
  735. products : [{
  736. condition : () => ~document.location.href.indexOf("/album/") ,
  737. type : "music",
  738. data : () => [document.querySelector("*[itemprop=byArtist]").textContent, document.querySelector("*[itemprop=name]").textContent]
  739. }]
  740. },
  741. 'googleplay' : {
  742. host : ["play.google.com"],
  743. condition : Always,
  744. products : [
  745. {
  746. condition : () => ~document.location.href.indexOf("/album/"),
  747. type : "music",
  748. data : () => [document.querySelector("*[itemprop=byArtist] a").textContent, document.querySelector("*[itemprop=name]").textContent]
  749. },
  750. {
  751. condition : () => ~document.location.href.indexOf("/movies/details/"),
  752. type : "movie",
  753. data : () => document.querySelector("*[itemprop=name]").textContent
  754. }
  755. ]
  756. },
  757. 'imdb' : {
  758. host : ["imdb.com"],
  759. condition : Always,
  760. products : [
  761. {
  762. condition : function() {
  763. var e = document.querySelector("meta[property='og:type']");
  764. if(e) {
  765. return e.content == "video.movie"
  766. }
  767. return false;
  768. },
  769. type : "movie",
  770. data : function() {
  771. if(document.querySelector(".title-extra[itemprop=name]")) {
  772. return [document.querySelector(".title-extra[itemprop=name]").firstChild.textContent.replace(/\"/g,"")];
  773. } else {
  774. return document.querySelector("*[itemprop=name]").textContent;
  775. }
  776. }
  777. },
  778. {
  779. condition : function() {
  780. var e = document.querySelector("meta[property='og:type']");
  781. if(e) {
  782. return e.content == "video.tv_show"
  783. }
  784. return false;
  785. },
  786. type : "tv",
  787. data : () => document.querySelector("*[itemprop=name]").textContent
  788. }
  789. ]
  790. },
  791. 'steam' : {
  792. host : ["store.steampowered.com"],
  793. condition : () => document.querySelector("*[itemprop=name]"),
  794. products : [{
  795. condition : Always,
  796. type : "pcgame",
  797. data : () => document.querySelector("*[itemprop=name]").textContent
  798. }]
  799. },
  800. 'tv.com' : {
  801. host : ["www.tv.com"],
  802. condition : () => document.querySelector("h1[itemprop=name]"),
  803. products : [{
  804. condition : Always,
  805. type : "tv",
  806. data : () => document.querySelector("h1[itemprop=name]").textContent
  807. }]
  808. },
  809. 'rottentomatoes' : {
  810. host : ["www.rottentomatoes.com"],
  811. condition : Always,
  812. products : [{
  813. condition : () => document.location.pathname.startsWith("/m/"),
  814. type : "movie",
  815. data : () => document.querySelector("h1[itemprop=name]").firstChild.textContent
  816. },
  817. {
  818. condition : () => document.location.pathname.startsWith("/tv/") ,
  819. type : "tv",
  820. data : () => document.querySelector("*[itemprop=partOfSeries] *[itemprop=name]").textContent
  821. }
  822. ]
  823. },
  824. 'serienjunkies' : {
  825. host : ["www.serienjunkies.de"],
  826. condition : Always,
  827. products : [{
  828. condition : () => Always,
  829. type : "tv",
  830. data : function() {
  831. if(document.querySelector("h1[itemprop=name]")) {
  832. return document.querySelector("h1[itemprop=name]").textContent;
  833. } else {
  834. var n = $("a:contains(Details zur)");
  835. if(n) {
  836. var name = n.text().match(/Details zur Produktion der Serie (.+)/)[1];
  837. return name;
  838. }
  839. }
  840. }
  841. }]
  842. },
  843. 'gamespot' : {
  844. host : ["gamespot.com"],
  845. condition : () => document.querySelector("[itemprop=device]"),
  846. products : [
  847. {
  848. condition : () => $("[itemprop=device]").text().contains("PC"),
  849. type : "pcgame",
  850. data : () => document.querySelector("h1[itemprop=name]").textContent
  851. },
  852. {
  853. condition : () => $("[itemprop=device]").text().contains("PS4"),
  854. type : "ps4game",
  855. data : () => document.querySelector("h1[itemprop=name]").textContent
  856. },
  857. {
  858. condition : () => $("[itemprop=device]").text().contains("XONE"),
  859. type : "xonegame",
  860. data : () => document.querySelector("h1[itemprop=name]").textContent
  861. }
  862. ]
  863. },
  864. 'amazon' : {
  865. host : ["amazon."],
  866. condition : Always,
  867. products : [
  868. {
  869. condition : function() {
  870. var music = ["Music","Musique","Musik","Música","Musica","音楽"];
  871. return music.some(function(s) {
  872. if(~document.title.indexOf(s)) {
  873. return true;
  874. } else {
  875. return false;
  876. }
  877. });
  878. },
  879. type : "music",
  880. data : function() {
  881. var artist = document.querySelector("#byline .author a").textContent;
  882. var title = document.getElementById("productTitle").textContent;
  883. title = title.replace(/\[([^\]]*)\]/g,""); // Remove [brackets] and their content
  884. return [artist, title];
  885. }
  886. },
  887. {
  888. condition : () => (document.getElementById("aiv-content-title") && document.getElementsByClassName("season-single-dark").length),
  889. type : "tv",
  890. data : () => document.getElementById("aiv-content-title").firstChild.data.trim()
  891. },
  892. {
  893. condition : () => document.getElementById("aiv-content-title"),
  894. type : "movie",
  895. data : () => document.getElementById("aiv-content-title").firstChild.data.trim()
  896. }
  897. ]
  898. },
  899. 'BoxOfficeMojo' : {
  900. host : ["boxofficemojo.com"],
  901. condition : () => ~document.location.search.indexOf("id="),
  902. products : [{
  903. condition : () => document.querySelector("#body table:nth-child(2) tr:first-child b"),
  904. type : "movie",
  905. data : () => document.querySelector("#body table:nth-child(2) tr:first-child b").firstChild.data
  906. }]
  907. },
  908. 'AllMovie' : {
  909. host : ["allmovie.com"],
  910. condition : () => document.querySelector("h2[itemprop=name].movie-title"),
  911. products : [{
  912. condition : () => document.querySelector("h2[itemprop=name].movie-title"),
  913. type : "movie",
  914. data : () => document.querySelector("h2[itemprop=name].movie-title").firstChild.data.trim()
  915. }]
  916. },
  917. 'en.wikipedia' : {
  918. host : ["en.wikipedia.org"],
  919. condition : Always,
  920. products : [{
  921. condition : function() {
  922. if(!document.querySelector(".infobox .summary")) {
  923. return false;
  924. }
  925. var r = /\d\d\d\d films/;
  926. return $("#catlinks a").filter((i,e) => e.firstChild.data.match(r)).length;
  927. },
  928. type : "movie",
  929. data : () => document.querySelector(".infobox .summary").firstChild.data
  930. },
  931. {
  932. condition : function() {
  933. if(!document.querySelector(".infobox .summary")) {
  934. return false;
  935. }
  936. var r = /television series/;
  937. return $("#catlinks a").filter((i,e) => e.firstChild.data.match(r)).length;
  938. },
  939. type : "tv",
  940. data : () => document.querySelector(".infobox .summary").firstChild.data
  941. }]
  942. },
  943. 'movies.com' : {
  944. host : ["movies.com"],
  945. condition : () => document.querySelector("meta[property='og:title']"),
  946. products : [{
  947. condition : Always,
  948. type : "movie",
  949. data : () => document.querySelector("meta[property='og:title']").content
  950. }]
  951. },
  952. 'themoviedb' : {
  953. host : ["themoviedb.org"],
  954. condition : () => document.querySelector("meta[property='og:type']"),
  955. products : [{
  956. condition : () => document.querySelector("meta[property='og:type']").content == "movie",
  957. type : "movie",
  958. data : () => document.querySelector("meta[property='og:title']").content
  959. },
  960. {
  961. condition : () => document.querySelector("meta[property='og:type']").content == "tv_series",
  962. type : "tv",
  963. data : () => document.querySelector("meta[property='og:title']").content
  964. }]
  965. },
  966. 'letterboxd' : {
  967. host : ["letterboxd.com"],
  968. condition : () => unsafeWindow.filmData && "name" in unsafeWindow.filmData,
  969. products : [{
  970. condition : Always,
  971. type : "movie",
  972. data : () => unsafeWindow.filmData.name
  973. }]
  974. },
  975. 'TVmaze' : {
  976. host : ["tvmaze.com"],
  977. condition : () => document.querySelector("h1"),
  978. products : [{
  979. condition : Always,
  980. type : "tv",
  981. data : () => document.querySelector("h1").firstChild.data
  982. }]
  983. },
  984. 'TVGuide' : {
  985. host : ["tvguide.com"],
  986. condition : Always,
  987. products : [{
  988. condition : () => document.location.pathname.startsWith("/tvshows/"),
  989. type : "tv",
  990. data : () => document.querySelector("meta[property='og:title']").content
  991. }]
  992. },
  993. };
  994.  
  995.  
  996. function main() {
  997.  
  998. var map = false;
  999.  
  1000. for(var name in sites) {
  1001. var site = sites[name];
  1002. if(site.host.some(function(e) {return ~this.indexOf(e)}, document.location.hostname) && site.condition()) {
  1003. for(var i = 0; i < site.products.length; i++) {
  1004. if(site.products[i].condition()) {
  1005. // Check map for a match
  1006. if(map === false) {
  1007. map = JSON.parse(GM_getValue("map","{}"));
  1008. }
  1009. var docurl = document.location.host.replace(/^www\./,"") + document.location.pathname + document.location.search;
  1010. if(docurl in map) {
  1011. // Found in map, show result
  1012. var metaurl = map[docurl];
  1013. metacritic["mapped"].apply(undefined, [baseURL + metaurl, site.products[i].type]);
  1014. break;
  1015. }
  1016. // Try to retrieve item name from page
  1017. var data;
  1018. try {
  1019. data = site.products[i].data();
  1020. } catch(e) {
  1021. data = false;
  1022. console.log(e);
  1023. }
  1024. if(data !== false) {
  1025. metacritic[site.products[i].type].apply(undefined, Array.isArray(data)?data:[data]);
  1026. }
  1027. break;
  1028. }
  1029. }
  1030. break;
  1031. }
  1032. }
  1033. }
  1034.  
  1035.  
  1036.  
  1037. main();
  1038. var lastLoc = document.location.href;
  1039. window.setInterval(function() {
  1040. if(document.location.href != lastLoc) {
  1041. lastLoc = document.location.href;
  1042. $("#mcdiv123").remove();
  1043. window.setTimeout(main,500);
  1044. }
  1045. },500);
  1046.