Libreddit Quirk Fixer

Fix some quirks of libreddit instances (disabled HLS, disabled NSFW, etc)

当前为 2024-01-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Libreddit Quirk Fixer
  3. // @namespace happyviking
  4. // @version 1.4.0
  5. // @grant none
  6. // @run-at document-end
  7. // @license MIT
  8. // @description Fix some quirks of libreddit instances (disabled HLS, disabled NSFW, etc)
  9. // @icon https://gitlab.com/uploads/-/system/project/avatar/32545239/libreddit.png
  10. // @author HappyViking
  11.  
  12. // <<INSTANCES START HERE>>
  13. // @match https://l.opnxng.com/*
  14. // @match https://libreddit.bus-hit.me/*
  15. // @match https://libreddit.privacydev.net/*
  16. // @match https://libreddit.projectsegfau.lt/*
  17. // @match https://reddit.idevicehacked.com/*
  18. // @match https://reddit.invak.id/*
  19. // @match https://redlib.catsarch.com/*
  20. // @match https://redlib.freedit.eu/*
  21. // @match https://redlib.matthew.science/*
  22. // @match https://redlib.perennialte.ch/*
  23. // @match https://redlib.tux.pizza/*
  24. // @match https://redlib.vimmer.dev/*
  25. // @match https://libreddit.freedit.eu/*
  26. // @match https://libreddit.hu/*
  27. // @match https://libreddit.kylrth.com/*
  28. // @match https://libreddit.lunar.icu/*
  29. // @match https://libreddit.mha.fi/*
  30. // @match https://libreddit.northboot.xyz/*
  31. // @match https://libreddit.oxymagnesium.com/*
  32. // @match https://libreddit.pussthecat.org/*
  33. // @match https://libreddit.spike.codes/*
  34. // @match https://libreddit.strongthany.cc/*
  35. // @match https://libreddit.tiekoetter.com/*
  36. // @match https://lr.4201337.xyz/*
  37. // @match https://lr.aeong.one/*
  38. // @match https://lr.artemislena.eu/*
  39. // @match https://lr.slipfox.xyz/*
  40. // @match https://r.walkx.fyi/*
  41. // @match https://reddit.rtrace.io/*
  42. // @match https://reddit.simo.sh/*
  43. // @match https://reddit.smnz.de/*
  44. // @match https://reddit.utsav2.dev/*
  45. // @match https://safereddit.com/*
  46. // @match https://snoo.habedieeh.re/*
  47. // @match https://libreddit.kutay.dev/*
  48. // @match https://libreddit.tux.pizza/*
  49. // @match https://lr.vern.cc/*
  50. // @match https://r.darklab.sh/*
  51. // @match https://reddit.leptons.xyz/*
  52. // @match https://discuss.whatever.social/*
  53. // @match https://libreddit.kavin.rocks/*
  54. // @match https://libreddit.cachyos.org/*
  55. // @match https://libreddit.domain.glass/*
  56. // @match https://libreddit.privacy.com.de/*
  57. // @match https://reddit.baby/*
  58. // <<INSTANCES END HERE>>
  59.  
  60. // @match https://geoblock.ste.company/restricted/*
  61.  
  62. // ==/UserScript==
  63.  
  64. let shouldReloadWithNewPreferences = false
  65. let preferencesString = ""
  66.  
  67. function setPreference(name, val) {
  68. preferencesString += `&${name}=${val}`
  69. shouldReloadWithNewPreferences = true
  70. }
  71.  
  72. function tryNewInstance(suffix){
  73. location.replace('https://farside.link/libreddit/' + suffix ?? (window.location.pathname + window.location.search));
  74. }
  75.  
  76. function setCookie(name, val) {
  77. const expiry = new Date()
  78. expiry.setMonth(expiry.getMonth() + 1)
  79. const domainAssociation = "domain=" + window.location.hostname;
  80. document.cookie = `${name}=${val};${domainAssociation};expires=${expiry.toUTCString()}`;
  81. }
  82. function getCookie(name) {
  83. const nameInfo = name + "=";
  84. const cookieList = document.cookie.split(';');
  85. return cookieList.find(c => c.trim().startsWith(nameInfo))
  86. }
  87.  
  88. function fixNSFWGate() {
  89. const nsfwElement = document.getElementById("nsfw_landing")
  90. if (!nsfwElement) return;
  91. const nsfwInfo = nsfwElement.querySelector("p")?.innerHTML
  92. if (!nsfwInfo) return
  93. if (nsfwInfo.includes("SFW-only")){
  94. const addedMessage = document.createElement("p")
  95. addedMessage.textContent = "Redirecting you to new instance..."
  96. nsfwElement.appendChild(addedMessage)
  97. tryNewInstance()
  98. }else{
  99. setPreference("show_nsfw", "on")
  100. }
  101. }
  102.  
  103. // In case the server doesn't actually serve a proper page, for any reason.
  104. // Since some might just have something like captcha pages (which are fine), we'll
  105. // only do this for some known problematic instances
  106. function fixInvalidPage(){
  107. if (["reddit.invak.id", "reddit.simo.sh"].includes(window.location.hostname)){
  108. const description = document.querySelector('meta[name="description"]')?.content
  109. if (!description || typeof description != "string" || !description.toLowerCase().includes("libreddit")){
  110. tryNewInstance()
  111. }
  112. }
  113. }
  114.  
  115.  
  116. function fixDefaultCommentOrder(){
  117. if (["lr.artemislena.eu"].includes(window.location.hostname)){
  118. const COOKIE_NAME = window.location.hostname + "FIXED_COMMENT_ORDER"
  119. if (!getCookie(COOKIE_NAME)){
  120. setCookie(COOKIE_NAME, "yes")
  121. setPreference("comment_sort", "confidence")
  122. }
  123. }
  124. }
  125.  
  126. function fixNoHls() {
  127. const notifications = document.getElementsByClassName("post_notification")
  128. for (const notification of notifications){
  129. const notifMessage = notification.querySelector("a")?.textContent
  130. if (notifMessage.trim() === "Enable HLS"){
  131. setPreference("use_hls", "on")
  132. break
  133. }
  134. }
  135. }
  136.  
  137. function fixGeoFencing() {
  138. if (window.location.hostname == "geoblock.ste.company" && window.location.search.includes("reddit")) {
  139. const redirect = new URL(location.href).searchParams.get('path')
  140. tryNewInstance(redirect)
  141. }
  142. }
  143.  
  144. fixInvalidPage()
  145. fixGeoFencing()
  146. fixDefaultCommentOrder()
  147. fixNSFWGate()
  148. fixNoHls()
  149.  
  150. if (shouldReloadWithNewPreferences){
  151. // We might as well turn on HLS before we realize that it's not enabled and we
  152. // have to reload a second time...
  153. setPreference("use_hls", "on")
  154. location.replace(`https://${window.location.hostname}/settings/update?${preferencesString}&redirect=${encodeURI(window.location.pathname.slice(1) + window.location.search)}`)
  155. }