npmjs.com visual tweaks

Styles npmjs.com README pages similarly to GitHub's (font, size, colors, but not syntax highlighting), and makes the content wider

As of 2018-04-24. See the latest version.

  1. // ==UserScript==
  2. // @name npmjs.com visual tweaks
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.7.7
  5. // @description Styles npmjs.com README pages similarly to GitHub's (font, size, colors, but not syntax highlighting), and makes the content wider
  6. // @author joeytwiddle
  7. // @copyright 2018, Paul "Joey" Clark (http://neuralyte.org/~joey)
  8. // @license MIT
  9. // @match https://www.npmjs.com/*
  10. // @grant GM_addStyle
  11. // ==/UserScript==
  12.  
  13. // Motivation: I didn't like the visual context switching between reading Github READMEs and npm READMEs, so I made READMEs on npm look similar to READMEs on Github.
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. const floatTheSidebar = true;
  19.  
  20. // I want to scale down the fonts and everything else a bit. This was an easy way to do that.
  21. //GM_addStyle('.container { transform: scale(0.92); transform-origin: 50% 0; }');
  22.  
  23. // Fill (almost) the whole width of the screen, like Wide-Github does.
  24. //GM_addStyle('.container { width: 98%; max-width: 100%; }');
  25. GM_addStyle("#top { max-width: 100%; }");
  26.  
  27. // Set fonts like GitHub
  28. GM_addStyle('#readme { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Source Sans Pro", "Lucida Grande", sans-serif; }');
  29. GM_addStyle('#readme { font-family: line-height: 1.5;}');
  30. //GM_addStyle('pre, code, kbd, samp { font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace; }');
  31. // Set fonts how I like them (configurable through browser)
  32. //GM_addStyle('pre, code, kbd, samp { font-family: monospace; }');
  33. // I don't know why, but "monospace" shrinks the font considerably on my Linux, whilst "monospace,monospace" does not.
  34. // Also "monospace,monospace" is what the site itself adopted on 2018/3/22. This rule will just ensure it stays this way!
  35. GM_addStyle('pre, code, kbd, samp { font-family: monospace,monospace; }');
  36.  
  37. // Set font sizes like GitHub
  38. // A snippet of code appearing within a paragraph
  39. GM_addStyle('code { font-size: 85%; }');
  40. // A line of code on its own
  41. GM_addStyle('pre > code { font-size: 100%; }');
  42. // A block of code
  43. GM_addStyle('pre { font-size: 82%; line-height: 1.4; }');
  44.  
  45. // Darker text
  46. if (navigator.userAgent.match(/Mac OS X/)) {
  47. // Weirdly, on Mac, the font strokes on npmjs.com appear slightly thinner than those on Github, for a reason I cannot understand. To compensate, I use a darker color.
  48. GM_addStyle('.markdown p, .markdown li { color: #111; }');
  49. } else {
  50. // Github 2016 (my preference)
  51. GM_addStyle('.markdown p, .markdown li { color: #333; }');
  52. // Github 2017
  53. //GM_addStyle('.markdown p, .markdown li { color: #24292e; }');
  54. }
  55.  
  56. // The boxes around inline code snippets
  57. GM_addStyle('code { border-radius: 3px; padding: 0.2em 0.4em !important; }');
  58.  
  59. // Links should be normal weight (npm makes them bolder)
  60. GM_addStyle('.markdown p a, .markdown li a { font-weight: initial; }');
  61.  
  62. // Padding around code blocks and snippets
  63. // A snippet of code appearing within a paragraph
  64. GM_addStyle('code { padding: 0.2em 0.2em; }');
  65. // A line of code on its own
  66. GM_addStyle('pre > code { padding: 1em 2em; }');
  67.  
  68. // Reduce the large padding inside code blocks to be more like Github's
  69. GM_addStyle('pre { padding: 1.1rem !important; }');
  70.  
  71. // Lighter background on code blocks and snippets
  72. GM_addStyle('.vistweaks .markdown .highlight, .vistweaks .markdown code, .vistweaks .markdown pre { background-color: #f6f8fa; }');
  73.  
  74. // More of a gap after each paragraph? Wasn't actually needed. The problem was wrapping due to insufficient width.
  75. //GM_addStyle('div, .highlight { padding-bottom: 20px; }');
  76.  
  77. // Thicker font for section headers
  78. GM_addStyle('.markdown h1, .markdown h2, .markdown h3, .markdown h4, .markdown h5, .markdown h6 { font-weight: 600; }');
  79.  
  80. // Use the npm font for section headers, even though we aren't using it for main text. (This is a divergence from Github's markdown scheme.)
  81. //GM_addStyle(".markdown h1, .markdown h2, .markdown h3, .markdown h4, .markdown h5, .markdown h6 { font-family: 'Source Sans Pro', 'Lucida Grande', sans-serif; }");
  82.  
  83. // There was absolutely no padding on the new styling. At low widths, the text could touch the edge of the screen. No thanks!
  84. //GM_addStyle(".markdown { padding-left: 1em; padding-right: 1em; }");
  85. // Better to do it for the whole content, including the tabs across the top
  86. //GM_addStyle("#top { padding-left: 1em; padding-right: 1em; }");
  87. // At larger widths we can enjoy greater padding
  88. GM_addStyle("#top { padding-left: 2%; padding-right: 2%; }");
  89.  
  90. // Make the sidebar look like a hovering card
  91. // Sadly this now reaches all the way down to the bottom of the README
  92. GM_addStyle(".package__rightSidebar___9dMXo { padding: 0.9em 1.8em 0.1em 1.8em !important; margin-top: 2.75rem; }");
  93. //GM_addStyle(".package__rightSidebar___9dMXo { box-shadow: 0 0 8px 0 #00000033; }");
  94. GM_addStyle(".package__rightSidebar___9dMXo { border: 1px solid #0002 }");
  95. // This widens the gap between the two panels, so the shadow doesn't overlap too much
  96. GM_addStyle(".markdown { padding-right: 0.5em; }");
  97. // This graph does not scale down well to low resolutions, with or without our changes. I will wait and see if they fix that.
  98.  
  99. // This is one way of increasing the specificity of our selectors
  100. // It could also be used to toggle our tweaks on and off
  101. // But I have not yet added it to the selectors above
  102. document.body.classList.add("vistweaks");
  103.  
  104. // Drop the huge margins above section titles
  105. GM_addStyle(".vistweaks .markdown h1, .vistweaks .markdown h2, .vistweaks .markdown h3, .vistweaks .markdown h4, .vistweaks .markdown h5, .vistweaks .markdown h6 { margin-top: 1em; padding-top: 0; }");
  106.  
  107. // Indent lists
  108. GM_addStyle(".vistweaks .markdown ul, .vistweaks .markdown ol { padding-left: 2em; }");
  109.  
  110. if (floatTheSidebar) {
  111. var checkTheSidebar = function () {
  112. const mainLeftPanel = document.querySelector('.package__main___3By_B');
  113. if (!mainLeftPanel) return;
  114.  
  115. const readmeElement = document.querySelector('#readme');
  116. if (!readmeElement) return;
  117.  
  118. const sidebarElement = document.querySelector('.package__rightSidebar___9dMXo');
  119. if (!sidebarElement) return;
  120. if (sidebarElement.parentNode.classList.contains('visual-tweaks-userscript-sidebar-container')) {
  121. //console.log("We have already handled this sidebar");
  122. return;
  123. }
  124.  
  125. mainLeftPanel.classList.remove('w-two-thirds-ns');
  126. mainLeftPanel.classList.remove('mr3-ns');
  127.  
  128. const sidebarContainer = document.createElement('div');
  129. sidebarContainer.className = 'visual-tweaks-userscript-sidebar-container';
  130. sidebarContainer.style.float = 'right';
  131. sidebarContainer.style.background = 'white';
  132. sidebarContainer.style.paddingLeft = '3em';
  133. sidebarContainer.style.paddingBottom = '3em';
  134. // Move the width from the sidebar to the container
  135. sidebarElement.classList.remove('w-third-ns');
  136. sidebarContainer.classList.add('w-third-ns');
  137. sidebarContainer.appendChild(sidebarElement);
  138. GM_addStyle(".markdown { padding-right: 0; }");
  139. // Clear the existing margin. Leave a small margin for the shadow.
  140. GM_addStyle(".mr3-ns { margin-right: 4px; }");
  141. //readmeElement.appendChild(sidebarElement);
  142. readmeElement.parentNode.insertBefore(sidebarContainer, readmeElement);
  143.  
  144. // BUG: At low resolutions, normally the sidebar will break to below the readme. But with our changes, the sidebar appears above the readme!
  145. };
  146.  
  147. checkTheSidebar();
  148.  
  149. new MutationObserver(mutations => checkTheSidebar()).observe(document.body, { childList: true, subtree: true });
  150. }
  151. })();