linkDoubanTrakt

在豆瓣和 trakt 之间增加跳转链接

  1. // ==UserScript==
  2. // @name linkDoubanTrakt
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description 在豆瓣和 trakt 之间增加跳转链接
  6. // @description:zh-CN 在豆瓣和 trakt 之间增加跳转链接
  7. // @description:en add trakt link on douban, and vice versa
  8. // @author Kjtsune
  9. // @match https://movie.douban.com/top250*
  10. // @match https://movie.douban.com/subject/*
  11. // @match https://trakt.tv/movies/*
  12. // @match https://trakt.tv/shows/*
  13. // @icon https://www.google.com/s2/favicons?sz=64&domain=douban.com
  14. // @grant GM.xmlHttpRequest
  15. // @connect api.douban.com
  16. // @connect movie.douban.com
  17. // @license MIT
  18. // ==/UserScript==
  19. "use strict";
  20.  
  21.  
  22. function getURL_GM(url) {
  23. return new Promise(resolve => GM.xmlHttpRequest({
  24. method: 'GET',
  25. url: url,
  26. onload: function (response) {
  27. if (response.status >= 200 && response.status < 400) {
  28. resolve(response.responseText);
  29. } else {
  30. console.error(`Error getting ${url}:`, response.status, response.statusText, response.responseText);
  31. resolve();
  32. }
  33. },
  34. onerror: function (response) {
  35. console.error(`Error during GM.xmlHttpRequest to ${url}:`, response.statusText);
  36. resolve();
  37. }
  38. }));
  39. }
  40.  
  41. async function getJSON_GM(url) {
  42. const data = await getURL_GM(url);
  43. if (data) {
  44. return JSON.parse(data);
  45. }
  46. }
  47.  
  48. async function getDoubanId(imdbId) {
  49. const search = await getJSON_GM(`https://movie.douban.com/j/subject_suggest?q=${imdbId}`);
  50. if (search && search.length > 0 && search[0].id) {
  51. return search[0].id
  52. }
  53. }
  54.  
  55. // Thanks JayXon
  56. function fixImdbLink() {
  57. let imdbA = document.querySelector('#info > a[href^=https\\:\\/\\/www\\.imdb');
  58. if (imdbA) return;
  59. const imdb_text = [...document.querySelectorAll('#info > span.pl')].find(s => s.innerText.trim() == 'IMDb:');
  60. if (!imdb_text) {
  61. console.log('IMDb id not available');
  62. return;
  63. }
  64. const text_node = imdb_text.nextSibling;
  65. const id = text_node.textContent.trim();
  66. let a = document.createElement('a');
  67. a.href = 'https://www.imdb.com/title/' + id;
  68. a.target = '_blank';
  69. a.appendChild(document.createTextNode(id));
  70. text_node.replaceWith(a);
  71. a.insertAdjacentText('beforebegin', ' ');
  72. }
  73.  
  74. function addTraktLink() {
  75. if (window.location.host != 'movie.douban.com') { return };
  76. // if (window.location.host.search(/douban/) == -1) { return };
  77. let traktA = document.querySelector('#traktLink');
  78. let imdbA = document.querySelector('#info > a[href^=https\\:\\/\\/www\\.imdb');
  79. if (!traktA && imdbA) {
  80. let imdbId = imdbA.textContent
  81. let traktHtml = `<a id="traktLink" href="https://trakt.tv/search/imdb?query=${imdbId}" target="_blank"> Trakt</a>`
  82. imdbA.insertAdjacentHTML("afterend", traktHtml);
  83. }
  84. }
  85.  
  86. async function addDoubanLink() {
  87. if (window.location.host != 'trakt.tv') { return };
  88. if (location.href.contains('seasons')) return;
  89. let doubanA = document.querySelector('#doubanLink');
  90. let imdbA = document.querySelector('#external-link-imdb');
  91. if (!doubanA && imdbA) {
  92. let imdbId = imdbA.href.split('/').at(-1);
  93. let doubanId = await getDoubanId(imdbId);
  94. let douhanHtml = `<a id="doubanLink" href="https://movie.douban.com/subject/${doubanId}/" target="_blank">Douban</a>`
  95. imdbA.insertAdjacentHTML("beforebegin", douhanHtml);
  96. }
  97.  
  98. }
  99.  
  100. function douban_delete_old(item) {
  101. let year = item.querySelector('p').textContent.split('\n')[2].match(/\d+/)[0]
  102. if (Number(year) < 2000 || Number(year) > 2010) {
  103. item.remove()
  104. }
  105. }
  106.  
  107. // clean top250
  108.  
  109. // let movieList = document.querySelectorAll('ol.grid_view > li')
  110. // movieList.forEach(douban_delete_old)
  111.  
  112. fixImdbLink()
  113. addTraktLink()
  114. addDoubanLink()