Reddit - Toggle Custom CSS

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

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

  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. // @require https://cdn.rawgit.com/eclecto/jQuery-onMutate/79bbb2b8caccabfc9b9ade046fe63f15f593fef6/src/jquery.onmutate.min.js
  12. // @version 1.1.0
  13. // @run-at document-start
  14. // @grant GM_addStyle
  15. // @grant GM_deleteValue
  16. // @grant GM_getValue
  17. // @grant GM_setValue
  18. // @grant GM_registerMenuCommand
  19. // ==/UserScript==
  20.  
  21. // inspired by: http://userscripts-mirror.org/scripts/show/109818
  22.  
  23. const SUBREDDIT = location.pathname.match(/\/r\/(\w+)/)[1]
  24. const CUSTOM_CSS = 'link[ref^="applied_subreddit_"]'
  25.  
  26. function toggle () {
  27. const oldDisableCss = GM_getValue(SUBREDDIT, false)
  28. const disableCss = !oldDisableCss
  29.  
  30. $(CUSTOM_CSS).prop('disabled', disableCss)
  31.  
  32. if (disableCss) {
  33. GM_setValue(SUBREDDIT, true)
  34. } else {
  35. GM_deleteValue(SUBREDDIT)
  36. }
  37. }
  38.  
  39. // NOTE we need to disable the display rather than setting its visibility to
  40. // hidden as the latter doesn't hide the background (which leads to a flash of
  41. // styled content (FOSC) on subreddits with a custom background color and/or
  42. // image)
  43. //
  44. // XXX hide the html element rather than the body element as the latter still
  45. // results in a FOSC on some subreddits e.g. /r/firefox
  46. function hidePage () {
  47. GM_addStyle('html { display: none !important }')
  48. }
  49.  
  50. const disableCss = GM_getValue(SUBREDDIT, false)
  51.  
  52. if (disableCss) {
  53. $(document).onCreate('head', hidePage)
  54.  
  55. // https://wiki.greasespot.net/DOMContentLoaded#Workaround
  56. $(document).on('DOMContentLoaded', () => {
  57. $(CUSTOM_CSS).prop('disabled', true)
  58. GM_addStyle('html { display: initial !important }')
  59. })
  60. }
  61.  
  62. GM_registerMenuCommand('Toggle Custom CSS', toggle)