Reddit - Toggle Custom CSS

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

当前为 2017-05-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit - Toggle Custom CSS
  3. // @description Persistently disable/re-enable subreddit-specific CSS via a userscript command
  4. // @author chocolateboy
  5. // @namespace https://github.com/chocolateboy/userscripts
  6. // @include http://reddit.com/r/*
  7. // @include https://reddit.com/r/*
  8. // @include http://*.reddit.com/r/*
  9. // @include https://*.reddit.com/r/*
  10. // @require https://code.jquery.com/jquery-3.2.1.min.js
  11. // @version 1.0.0
  12. // @run-at document-start
  13. // @grant GM_addStyle
  14. // @grant GM_deleteValue
  15. // @grant GM_getValue
  16. // @grant GM_setValue
  17. // @grant GM_registerMenuCommand
  18. // ==/UserScript==
  19.  
  20. // inspired by: http://userscripts-mirror.org/scripts/show/109818
  21.  
  22. const SUBREDDIT = location.pathname.match(/\/r\/(\w+)/)[1]
  23. const CUSTOM_CSS = 'link[ref^="applied_subreddit_"]'
  24.  
  25. function toggle () {
  26. const oldDisableCss = GM_getValue(SUBREDDIT, false)
  27. const disableCss = !oldDisableCss
  28.  
  29. $(CUSTOM_CSS).prop('disabled', disableCss)
  30.  
  31. if (disableCss) {
  32. GM_setValue(SUBREDDIT, true)
  33. } else {
  34. GM_deleteValue(SUBREDDIT)
  35. }
  36. }
  37.  
  38. const disableCss = GM_getValue(SUBREDDIT, false)
  39.  
  40. if (disableCss) {
  41. // https://wiki.greasespot.net/DOMContentLoaded#Workaround
  42.  
  43. // NOTE we need to disable the display rather than setting its
  44. // visibility to hidden as the latter doesn't hide the background
  45. // (which leads to a flash of styled content on subreddits with a custom
  46. // background color and/or image)
  47. GM_addStyle('body { display: none !important }')
  48.  
  49. $(document).on('DOMContentLoaded', function () {
  50. $(CUSTOM_CSS).prop('disabled', true)
  51. GM_addStyle('body { display: initial !important }')
  52. })
  53. }
  54.  
  55. GM_registerMenuCommand('Toggle Custom CSS', toggle)