Reddit - Toggle Custom CSS

Persistently disable/re-enable subreddit-specific styles via a userscript command

当前为 2019-05-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit - Toggle Custom CSS
  3. // @description Persistently disable/re-enable subreddit-specific styles via a userscript command
  4. // @author chocolateboy
  5. // @copyright chocolateboy
  6. // @version 1.2.0
  7. // @namespace https://github.com/chocolateboy/userscripts
  8. // @license GPL: http://www.gnu.org/copyleft/gpl.html
  9. // @include http://reddit.com/r/*
  10. // @include https://reddit.com/r/*
  11. // @include http://*.reddit.com/r/*
  12. // @include https://*.reddit.com/r/*
  13. // @require https://code.jquery.com/jquery-3.4.1.min.js
  14. // @require https://cdn.jsdelivr.net/gh/eclecto/jQuery-onMutate@79bbb2b8caccabfc9b9ade046fe63f15f593fef6/src/jquery.onmutate.min.js
  15. // @grant GM_addStyle
  16. // @grant GM_deleteValue
  17. // @grant GM_getValue
  18. // @grant GM_setValue
  19. // @grant GM_registerMenuCommand
  20. // @run-at document-start
  21. // ==/UserScript==
  22.  
  23. // inspired by: http://userscripts-mirror.org/scripts/show/109818
  24.  
  25. const CUSTOM_CSS = 'link[ref^="applied_subreddit_"]'
  26. const DISABLE_CSS = false
  27. const SUBREDDIT = location.pathname.match(/\/r\/(\w+)/)[1]
  28.  
  29. function toggle () {
  30. const oldDisableCss = GM_getValue(SUBREDDIT, DISABLE_CSS)
  31. const disableCss = !oldDisableCss
  32.  
  33. $(CUSTOM_CSS).prop('disabled', disableCss)
  34.  
  35. if (disableCss) {
  36. GM_setValue(SUBREDDIT, true)
  37. } else {
  38. GM_deleteValue(SUBREDDIT)
  39. }
  40. }
  41.  
  42. // NOTE we need to disable the display rather than setting its visibility to
  43. // hidden as the latter doesn't hide the background (which leads to a flash of
  44. // styled content (FOSC) on subreddits with a custom background color and/or
  45. // image)
  46. //
  47. // XXX hide the html element rather than the body element as the latter still
  48. // results in a FOSC on some subreddits e.g. /r/firefox
  49. function hidePage () {
  50. GM_addStyle('html { display: none !important }')
  51. }
  52.  
  53. const disableCss = GM_getValue(SUBREDDIT, DISABLE_CSS)
  54.  
  55. if (disableCss) {
  56. $(document).onCreate('head', hidePage)
  57.  
  58. // https://wiki.greasespot.net/DOMContentLoaded#Workaround
  59. $(document).on('DOMContentLoaded', () => {
  60. $(CUSTOM_CSS).prop('disabled', true)
  61. GM_addStyle('html { display: initial !important }')
  62. })
  63. }
  64.  
  65. GM_registerMenuCommand('Toggle Custom CSS', toggle)