Replace IGDB with cs.rin.ru for Backloggd

Replaces the IGDB button on Backloggd with a cs.rin.ru downolad link

  1. // ==UserScript==
  2. // @name Replace IGDB with cs.rin.ru for Backloggd
  3. // @description Replaces the IGDB button on Backloggd with a cs.rin.ru downolad link
  4. // @author Soap
  5. // @namespace soap.systems
  6. // @homepageURL https://soap.systems
  7. // @match *://backloggd.com/*
  8. // @match *://*.backloggd.com/*
  9. // @match *://cs.rin.ru/forum/search.php*
  10. // @grant GM_registerMenuCommand
  11. // @version 1.8
  12. // @icon https://external-content.duckduckgo.com/ip3/www.backloggd.com.ico
  13. // @license GPLv3
  14. // ==/UserScript==
  15.  
  16.  
  17. // OPTIONS:
  18. // disable turbolinks-style navigation. this might make the site feel slower (though, not any slower than a normal website)
  19. // but it fixes an issue where links sometimes won't change automatically without a refresh
  20. // DEFAULT: true
  21. const disableTurbolinks = true
  22. // even with disableTurbolinks, results from searches will still open with turbolinks navigation
  23. // this adds a right click option on Tampermonkey and an extension menu option on Violentmonky
  24. // which will manually update the link
  25. // DEFAULT: true
  26. const enableContextMenu = true
  27. // note that when the script updates you will have to reconfigure any options. shouldn't update too often though.
  28.  
  29. /*
  30. TODO:
  31. - replace url at the bottom of search pages with "Search on cs.rin.ru".
  32. - url replacement logic works fine for this already, just need to fix the search query constructor
  33. - would be nice to replace the variable-based config with a key/value type deal with GM_setValue
  34. - add a ProtonDB button if the "released on" section doesn't include Linux
  35. - scrape the IGDB link to add a gog-games.to link if it exists
  36. - SteamRip link? Might be kinda redundant
  37. */
  38.  
  39. function removeTurbolinks() {
  40. if (location.host == 'backloggd.com') {
  41. document.querySelectorAll('a').forEach(link => {
  42. if (link.href.startsWith('https://backloggd.com')) {
  43. link.setAttribute('data-turbolinks', 'false');
  44. }
  45. });
  46. }
  47. }
  48.  
  49. // We have to get the game name and the right element from the IGDB link because Backloggd doesn't have any meaningful class names
  50. function changeIgdbUrl () {
  51. if (location.href.startsWith('https://backloggd.com/games')) {
  52. const link = document.querySelector('a[href^="https://www.igdb.com/games/"]')
  53. const linkElement = link.parentElement
  54.  
  55. if (linkElement) {
  56. let searchURL = getSearchUrl(link)
  57. linkElement.innerHTML = `Download on <a href="${searchURL}" target="_blank">cs.rin.ru</a>`
  58. }
  59. }
  60. }
  61.  
  62.  
  63. function getSearchUrl(link) {
  64. // examle IGDB link: https://backloggd.com/games/yakuza-6-the-song-of-life
  65. let searchQuery = link.href.split('/').pop()
  66. searchQuery = searchQuery.replaceAll('-', ' ')
  67. let searchURL = 'https://cs.rin.ru/forum/search.php?keywords=' + searchQuery + '&terms=all&author=&sc=1&sf=titleonly&sk=t&sd=d&sr=topics&st=0&ch=300&t=0&submit=Search&backloggdrinscript=true'
  68. return searchURL
  69. }
  70.  
  71. function clickFirstRinLink() {
  72. let params = new URLSearchParams(document.location.search)
  73. // There's a 'backloggdrinscript' parameter added to the end of the search URL so we can detect that you got to the search page from the script
  74. let scriptActive = params.get('backloggdrinscript');
  75. if (scriptActive) {
  76. const topics = document.querySelectorAll('a.topictitle');
  77.  
  78. // only redirect if there's one link in the search results
  79. if (topics.length === 1) {
  80. window.location.href = topics[0].href;
  81. }
  82. }
  83. }
  84.  
  85. if (disableTurbolinks === true) {
  86. removeTurbolinks()
  87. }
  88.  
  89. clickFirstRinLink()
  90.  
  91. if (enableContextMenu === true) {
  92. GM_registerMenuCommand('Replace URL', changeIgdbUrl)
  93. }
  94.  
  95. // Backloggd uses turbolinks, so we have to re-run each time the url changes
  96. // Will occasionally decide to not work. Refreshing fixes this but might be worth migrating to vm-url in case that works better
  97. // https://violentmonkey.github.io/api/matching/#matching-spa-sites-like-fb-github-twitter-youtube
  98. onUrlChange()
  99.  
  100. if (self.navigation && disableTurbolinks === false) {
  101. navigation.addEventListener('navigatesuccess', onUrlChange);
  102. } else {
  103. let u = location.href;
  104. new MutationObserver(() => u !== (u = location.href) && onUrlChange())
  105. .observe(document, {subtree: true, childList: true});
  106. }
  107.  
  108. function onUrlChange() {
  109. changeIgdbUrl()
  110. }