npmjs.com visual tweaks

Makes READMEs on npmjs.com look more like READMEs on GitHub (font, size, padding, some colors); also makes the content wider

Verze ze dne 06. 07. 2018. Zobrazit nejnovější verzi.

  1. // ==UserScript==
  2. // @name npmjs.com visual tweaks
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.7.12
  5. // @description Makes READMEs on npmjs.com look more like READMEs on GitHub (font, size, padding, some colors); also 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. // This allows the README to expand to the full width below the info sidebar
  19. const floatTheSidebar = true;
  20.  
  21. // I want to scale down the fonts and everything else a bit. This was an easy way to do that.
  22. //GM_addStyle('.container { transform: scale(0.92); transform-origin: 50% 0; }');
  23.  
  24. // Fill (almost) the whole width of the screen, like Wide-Github does.
  25. //GM_addStyle('.container { width: 98%; max-width: 100%; }');
  26. GM_addStyle(".vistweaks #top { max-width: 100%; }");
  27. // OK not the entire width. Stop the width when it starts to look ugly.
  28. GM_addStyle(".vistweaks #top { max-width: 1500px; }");
  29.  
  30. // Set fonts like GitHub
  31. GM_addStyle('.vistweaks #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; }');
  32. GM_addStyle('#readme { font-family: line-height: 1.5;}');
  33. //GM_addStyle('pre, code, kbd, samp { font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace; }');
  34. // Set fonts how I like them (configurable through browser)
  35. //GM_addStyle('pre, code, kbd, samp { font-family: monospace; }');
  36. // I don't know why, but "monospace" shrinks the font considerably on my Linux, whilst "monospace,monospace" does not.
  37. // Also "monospace,monospace" is what the site itself adopted on 2018/3/22. This rule will just ensure it stays this way!
  38. GM_addStyle('.vistweaks .readme__readme___tmT33 pre, .vistweaks .readme__readme___tmT33 code, .vistweaks .readme__readme___tmT33 kbd, .vistweaks .readme__readme___tmT33 samp { font-family: monospace,monospace; }');
  39.  
  40. // Set font sizes like GitHub
  41. // A snippet of code appearing within a paragraph
  42. GM_addStyle('.vistweaks code { font-size: 85%; }');
  43. // A line of code on its own
  44. GM_addStyle('.vistweaks pre > code { font-size: 100%; }');
  45. // A block of code
  46. GM_addStyle('.vistweaks pre { font-size: 82%; line-height: 1.4; }');
  47.  
  48. // Darker text
  49. if (navigator.userAgent.match(/Mac OS X/)) {
  50. // 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.
  51. GM_addStyle('.vistweaks .markdown p, .vistweaks .markdown li { color: #111; }');
  52. } else {
  53. // Github 2016 (my preference)
  54. GM_addStyle('.vistweaks .markdown p, .vistweaks .markdown li { color: #333; }');
  55. // Github 2017
  56. //GM_addStyle('.markdown p, .markdown li { color: #24292e; }');
  57. }
  58.  
  59. // The boxes around inline code snippets
  60. GM_addStyle('.vistweaks code { border-radius: 3px; padding: 0.2em 0.4em !important; }');
  61.  
  62. // Links should be normal weight (npm makes them bolder)
  63. GM_addStyle('.vistweaks .markdown p a, .vistweaks .markdown li a { font-weight: initial; }');
  64.  
  65. // Padding around code blocks and snippets
  66. // A snippet of code appearing within a paragraph
  67. GM_addStyle('.vistweaks code { padding: 0.2em 0.2em; }');
  68. // A line of code on its own
  69. GM_addStyle('.vistweaks pre > code { padding: 1em 2em; }');
  70.  
  71. // Reduce the large padding inside code blocks to be more like Github's
  72. GM_addStyle('.vistweaks pre { padding: 1.1rem !important; }');
  73.  
  74. // Lighter background on code blocks and snippets
  75. GM_addStyle('.vistweaks .markdown .highlight, .vistweaks .markdown code, .vistweaks .markdown pre { background-color: #f6f8fa; }');
  76.  
  77. // More of a gap after each paragraph? Wasn't actually needed. The problem was wrapping due to insufficient width.
  78. //GM_addStyle('div, .highlight { padding-bottom: 20px; }');
  79.  
  80. // Thicker font for section headers
  81. GM_addStyle('.vistweaks .markdown h1, .vistweaks .markdown h2, .vistweaks .markdown h3, .vistweaks .markdown h4, .vistweaks .markdown h5, .vistweaks .markdown h6 { font-weight: 600; }');
  82.  
  83. // 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.)
  84. //GM_addStyle(".markdown h1, .markdown h2, .markdown h3, .markdown h4, .markdown h5, .markdown h6 { font-family: 'Source Sans Pro', 'Lucida Grande', sans-serif; }");
  85.  
  86. // There was absolutely no padding on the new styling. At low widths, the text could touch the edge of the screen. No thanks!
  87. //GM_addStyle(".markdown { padding-left: 1em; padding-right: 1em; }");
  88. // Better to do it for the whole content, including the tabs across the top
  89. //GM_addStyle("#top { padding-left: 1em; padding-right: 1em; }");
  90. // At larger widths we can enjoy greater padding
  91. GM_addStyle(".vistweaks #top { padding-left: 2%; padding-right: 2%; }");
  92.  
  93. // Make the sidebar look like a hovering card
  94. // Sadly this now reaches all the way down to the bottom of the README
  95. GM_addStyle(".vistweaks .package__rightSidebar___9dMXo { padding: 0.9em 1.8em 0.1em 1.8em !important; margin-top: 2.75rem; }");
  96. //GM_addStyle(".package__rightSidebar___9dMXo { box-shadow: 0 0 8px 0 #00000033; }");
  97. GM_addStyle(".vistweaks .package__rightSidebar___9dMXo { border: 1px solid #0002 }");
  98. // This widens the gap between the two panels, so the shadow doesn't overlap too much
  99. GM_addStyle(".vistweaks .markdown { padding-right: 0.5em; }");
  100. // This graph does not scale down well to low resolutions, with or without our changes. I will wait and see if they fix that.
  101.  
  102. // This is one way of increasing the specificity of our selectors
  103. // It could also be used to toggle our tweaks on and off
  104. // But I have not yet added it to the selectors above
  105. document.body.classList.add("vistweaks");
  106.  
  107. // Drop the huge margins above section titles
  108. 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; }");
  109.  
  110. // Indent lists
  111. GM_addStyle(".vistweaks .markdown ul, .vistweaks .markdown ol { padding-left: 2em; }");
  112.  
  113. if (floatTheSidebar) {
  114. var checkTheSidebar = function () {
  115. const mainLeftPanel = document.querySelector('.package__main___3By_B');
  116. if (!mainLeftPanel) return;
  117.  
  118. const readmeElement = document.querySelector('#readme');
  119. if (!readmeElement) return;
  120.  
  121. const sidebarElement = document.querySelector('.package__rightSidebar___9dMXo');
  122. if (!sidebarElement) return;
  123. if (sidebarElement.parentNode.classList.contains('visual-tweaks-userscript-sidebar-container')) {
  124. //console.log("We have already handled this sidebar");
  125. return;
  126. }
  127.  
  128. mainLeftPanel.classList.remove('w-two-thirds-l');
  129. //mainLeftPanel.querySelector('section').classList.remove('mr3-ns');
  130.  
  131. // If there is nothing forcing the main pane to fill the width, then it won't.
  132. // That looks odd, because the floated sidebar will now not appear next to the right edge.
  133. // Example (a page without an image): https://www.npmjs.com/package/eslint-plugin-styled-components
  134. // So we force the main content to fill the available width.
  135. mainLeftPanel.style.width = '100%';
  136. mainLeftPanel.style.maxWidth = '100%';
  137.  
  138. const sidebarContainer = document.createElement('div');
  139. sidebarContainer.className = 'visual-tweaks-userscript-sidebar-container';
  140. sidebarContainer.style.float = 'right';
  141. sidebarContainer.style.background = 'white';
  142. sidebarContainer.style.paddingLeft = '3em';
  143. sidebarContainer.style.paddingBottom = '3em';
  144. // Move the width from the sidebar to the container
  145. sidebarElement.classList.remove('w-third-l');
  146. sidebarContainer.classList.add('w-third-l');
  147. sidebarContainer.appendChild(sidebarElement);
  148. GM_addStyle(".vistweaks .markdown { padding-right: 0; }");
  149. // Clear the existing margin. Leave a small margin for the shadow.
  150. GM_addStyle(".vistweaks .mr3-ns { margin-right: 4px; }");
  151. // Give the info card equal padding at the top and bottom
  152. GM_addStyle(".vistweaks .package__rightSidebar___9dMXo { padding-top: 1em !important; padding-bottom: 1em !important; }");
  153. //readmeElement.appendChild(sidebarElement);
  154. readmeElement.parentNode.insertBefore(sidebarContainer, readmeElement);
  155.  
  156. // BUG: At low resolutions, normally the sidebar will break to below the readme. But with our changes, the sidebar appears above the readme!
  157.  
  158. // If an image appears above the fold, and it takes 100% of the width, then it will cause the document to push down below the floating sidebar
  159. // For an example of such a page: https://www.npmjs.com/package/passport
  160. // To fix it, we set a max width on images
  161. // Of course this rule will apply to all images, even those later down on the page
  162. // npmjs have now set their own max, so we can remove this
  163. GM_addStyle(".vistweaks .markdown img { max-width: 66%; }");
  164. };
  165.  
  166. checkTheSidebar();
  167.  
  168. // Keep checking, in case we go to a new page
  169. new MutationObserver(mutations => checkTheSidebar()).observe(document.body, { childList: true, subtree: true });
  170. }
  171. })();