Advertisement
XT-8147

detect_night_mode.user

Sep 7th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Twitter - CSS Class for Night Mode
  3. // @namespace    http://xt-8147.blogspot.com/
  4. // @version      1.0
  5. // @description  Detects if you're using Night Mode on Twitter and sticks a CSS class on the body tag.
  6. // @author       XT-8147
  7. // @include      https://twitter.com/*
  8. // @grant        none
  9. // @run-at       document-start
  10. // ==/UserScript==
  11.  
  12. /* Version History
  13.  * Version  Notes
  14.  *   0.1    * Original version.
  15.  *   0.2    * Properly adds and removes the CSS class as necessary after page load, since the user is free to toggle it at will after page load.
  16.  *   0.3    * The head actually mutates twice every time Night Mode is toggled, the toggle doesn't complete until the second mutation.  Now detecting this.
  17.  *   1.0    * Code declared release-worthy.
  18.  */
  19.  
  20. function maintainCSSClass() {
  21.  var isNightMode = ( document.querySelector( 'link[rel="stylesheet"][href*="nightmode"]' ) !== null );
  22.  var pushingState = document.body.classList.contains( 'pushing-state' );
  23.  if( !pushingState ) {
  24.   if( isNightMode ) {
  25.    document.body.classList.add( 'night-mode' );
  26.   }
  27.   else {
  28.    document.body.classList.remove( 'night-mode' );
  29.   }
  30.  }
  31. }
  32.  
  33. var o = new MutationObserver( maintainCSSClass );
  34. o.observe( document.head, { characterData: false, attributes: false, childList: true, subtree: false } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement