IMDB works#

Shows number of credited works after names on IMDB site

当前为 2017-01-23 提交的版本,查看 最新版本

  1. // ==UserScript==//
  2. // @name IMDB works#
  3. // @description Shows number of credited works after names on IMDB site
  4. // @match *://*.imdb.com/*
  5. // @version 1.0.4
  6. // @author wOxxOm
  7. // @namespace wOxxOm.scripts
  8. // @license MIT License
  9. // @grant GM_addStyle
  10. // @grant GM_xmlhttpRequest
  11. // @run-at document-start
  12. // @require https://gf.qytechs.cn/scripts/12228/code/setMutationHandler.js
  13. // ==/UserScript==
  14.  
  15. var SELECTOR = 'a[href^="/name/nm"]';
  16. var CACHE_DURATION = 30 * 24 * 3600 * 1000; // 1 month
  17.  
  18. GM_addStyle(
  19. '.number-of-works, .number-of-works span { opacity: 0.5; transition: opacity .25s ease-in-out .25s; }' +
  20. '.number-of-works span:before { content: "/"; }' +
  21. '.number-of-works:hover { opacity: 1.0; }' +
  22. '.number-of-works:hover span { opacity: 1.0; }' +
  23. '.number-of-works:before { content: " ["; opacity: 0.5; }' +
  24. '.number-of-works:after { content: "]"; opacity: 0.5; }'
  25. );
  26.  
  27. process(document.querySelectorAll(SELECTOR));
  28. setMutationHandler(document, SELECTOR, process);
  29.  
  30. function process(links) {
  31. var now = Date.now();
  32. for (var link, i = 0; (link = links[i++]); ) {
  33. if (link.querySelector('img') ||
  34. !link.textContent.trim() ||
  35. link.nextElementSibling && link.nextElementSibling.matches('.number-of-works'))
  36. continue;
  37. var id = (link.href.match(/\/name\/nm(\d+)\/?(?:\?.*)?$/) || [])[1];
  38. if (!id)
  39. continue;
  40. var cacheKey = 'works#' + id;
  41. var cache = (localStorage[cacheKey] || '').split('\t');
  42. if (cache.length == 2 && +cache[0] && cache[1]) {
  43. showWorksNum(link, cache[1]);
  44. var isFresh = +cache[0] > now;
  45. if (isFresh)
  46. continue;
  47. }
  48. doXHR({
  49. url: link.pathname,
  50. link: link,
  51. cacheKey: cacheKey,
  52. onload: parseNamePage,
  53. });
  54. }
  55. }
  56.  
  57. function showWorksNum(link, num, needsUpdate) {
  58. if (link.nextElementSibling && link.nextElementSibling.matches('.number-of-works')) {
  59. link.nextElementSibling.textContent = num;
  60. delete link.nextElementSibling.dataset.updating;
  61. } else {
  62. link.insertAdjacentHTML('afterend',
  63. '<span class="number-of-works"' +
  64. (needsUpdate ? ' data-updating' : '') + '>' +
  65. num.toString().replace(/\/(\d+)/, '<span>$1</span>') +
  66. '</span>');
  67. }
  68. }
  69.  
  70. function parseNamePage(doc, options) {
  71. var credits = [].map.call(doc.querySelectorAll('#filmography .head'), function(e) {
  72. return +(e.textContent.match(/\((\d+) credits?\)/i) || [])[1];
  73. });
  74. if (!credits.length)
  75. return;
  76. var creditsSum = credits.reduce(function(sum, e) { return sum + e; }, 0);
  77. var worksNum = credits[0] + (credits.length > 1 ? '/' + creditsSum : '');
  78. showWorksNum(options.link, worksNum);
  79. localStorage[options.cacheKey] = '' + (Date.now() + CACHE_DURATION) + '\t' + worksNum;
  80. }
  81.  
  82. function doXHR(options) {
  83. if (document.readyState == 'complete') {
  84. sendRequest(options);
  85. return;
  86. }
  87. if (!doXHR.queue) {
  88. doXHR.queue = [];
  89. window.addEventListener('load', function docLoaded() {
  90. window.removeEventListener('load', docLoaded);
  91. while (doXHR.queue.length) {
  92. var req = doXHR.queue.shift();
  93. if (req.url) {
  94. sendRequest(req);
  95. } else {
  96. // this request's url was processed in the queue earlier so we'll reuse the result
  97. showWorksNum(req.link, localStorage[req.cacheKey]);
  98. }
  99. }
  100. });
  101. }
  102. doXHR.queue.some(function(e) {
  103. if (e.url == options.url) {
  104. // this request's link element will use the will-be-cached data from the earlier queued request
  105. options.url = '';
  106. return true;
  107. }
  108. });
  109. doXHR.queue.push(options);
  110.  
  111. function sendRequest(options) {
  112. var xhr = new XMLHttpRequest();
  113. xhr.open('GET', options.url);
  114. xhr.responseType = 'document';
  115. xhr.onload = function(e) {
  116. options.onload(xhr.response, options);
  117. };
  118. xhr.send();
  119. }
  120. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址