Reddit - Toggle Custom CSS

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

目前为 2017-04-12 提交的版本。查看 最新版本

  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.1.1.min.js
  11. // @version 0.0.4
  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. // original: http://userscripts-mirror.org/scripts/show/109818
  21.  
  22. // Why this remix? Because a) I don't use the other toggles (and the Unix philosophy
  23. // suggests they should be separate userscripts) and b) several subreddits mangle the
  24. // extra buttons so it's impossible to see/click them e.g. /r/ConTalks/
  25. // Also, this is much easier to understand/maintain.
  26.  
  27. var SUBREDDIT = location.pathname.match(/\/r\/(\w+)/)[1];
  28. var CUSTOM_CSS = 'link[title="applied_subreddit_stylesheet"]';
  29.  
  30. function toggle () {
  31. var disableCss = !GM_getValue(SUBREDDIT, false);
  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. var disableCss = GM_getValue(SUBREDDIT, false);
  43.  
  44. if (disableCss) {
  45. // https://wiki.greasespot.net/DOMContentLoaded#Workaround
  46. GM_addStyle("body { visibility: hidden }");
  47.  
  48. $(document).on('DOMContentLoaded', function () {
  49. $(CUSTOM_CSS).prop('disabled', true)
  50. GM_addStyle("body { visibility: visible !important }");
  51. });
  52. }
  53.  
  54. GM_registerMenuCommand('Toggle Custom CSS', toggle);