Skip to content

Instantly share code, notes, and snippets.

@janxkoci
Last active January 18, 2021 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janxkoci/6328abcbdab3d8b2107fd19d9dd158bb to your computer and use it in GitHub Desktop.
Save janxkoci/6328abcbdab3d8b2107fd19d9dd158bb to your computer and use it in GitHub Desktop.
NightMode bookmarklets (copy one or all)
// this one inverts colours on a page; second click reverts it (because it's exact negative)
javascript:(function(){function RGBtoHSL(RGBColor){with(Math){var R,G,B;var cMax,cMin;var sum,diff;var Rdelta,Gdelta,Bdelta;var H,L,S;R=RGBColor[0];G=RGBColor[1];B=RGBColor[2];cMax=max(max(R,G),B);cMin=min(min(R,G),B);sum=cMax+cMin;diff=cMax-cMin;L=sum/2;if(cMax==cMin){S=0;H=0;}else{if(L<=(1/2))S=diff/sum;else S=diff/(2-sum);Rdelta=R/6/diff;Gdelta=G/6/diff;Bdelta=B/6/diff;if(R==cMax)H=Gdelta-Bdelta;else if(G==cMax)H=(1/3)+Bdelta-Rdelta;else H=(2/3)+Rdelta-Gdelta;if(H<0)H+=1;if(H>1)H-=1;}return[H,S,L];}}function getRGBColor(node,prop){var rgb=getComputedStyle(node,null).getPropertyValue(prop);var r,g,b;if(/rgb\((\d+),\s(\d+),\s(\d+)\)/.exec(rgb)){r=parseInt(RegExp.$1,10);g=parseInt(RegExp.$2,10);b=parseInt(RegExp.$3,10);return[r/255,g/255,b/255];}return rgb;}function hslToCSS(hsl){return "hsl("+Math.round(hsl[0]*360)+", "+Math.round(hsl[1]*100)+"%, "+Math.round(hsl[2]*100)+"%)";}var props=["color","background-color","border-left-color","border-right-color","border-top-color","border-bottom-color"];var props2=["color","backgroundColor","borderLeftColor","borderRightColor","borderTopColor","borderBottomColor"];if(typeof getRGBColor(document.documentElement,"background-color")=="string")document.documentElement.style.backgroundColor="white";revl(document.documentElement);function revl(n){var i,x,color,hsl;if(n.nodeType==Node.ELEMENT_NODE){for(i=0;x=n.childNodes[i];++i)revl(x);for(i=0;x=props[i];++i){color=getRGBColor(n,x);if(typeof(color)!="string"){hsl=RGBtoHSL(color);hsl[2]=1-hsl[2];n.style[props2[i]]=hslToCSS(hsl);}}}}})()
// this one changes all background to #666666 and all text to silver
javascript:(function(){var%20allElem=document.getElementsByTagName(%22*%22);for(var%20i=0;i%3CallElem.length;i++){var%20e=allElem[i];e.style.color=%22silver%22;e.style.backgroundColor=%22#333333%22;e.style.backgroundImage=%22none%22}})();
// MY FAVOURITE: this one also sets bg to #666666 and text to sliver, but links will be #ddffdd (I think)
javascript:(function(){N=document.createElement('link');S='*{background:#333333 !important;color:silver !important}:link,:link *{color:#ddddff !important}:visited,:visited *{color:#ddffdd !important}';N.rel='stylesheet';N.href='data:text/css,'+escape(S);document.getElementsByTagName('head')[0].appendChild(N);})()
@janxkoci
Copy link
Author

Disclaimer:

I found these bookmarklets somewhere on the internet about a decade ago, after googling for "nightmode bookmarklet" or something to that effect. I switched browsers, computers, and methods of saving them. This gist is how I save them now.

I have no idea what is their license. As far as I recall, there was none attached to them, but I may be wrong. If you own this code or know its licence, let me know if I should remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment