GitHub Image Preview

A userscript that adds clickable image thumbnails

当前为 2017-03-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Image Preview
  3. // @version 1.1.4
  4. // @description A userscript that adds clickable image thumbnails
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @namespace https://github.com/Mottie
  7. // @include https://github.com/*
  8. // @run-at document-idle
  9. // @grant GM_addStyle
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_xmlhttpRequest
  13. // @connect github.com
  14. // @connect githubusercontent.com
  15. // @author Rob Garrison
  16. // ==/UserScript==
  17. (() => {
  18. "use strict";
  19.  
  20. GM_addStyle(`
  21. table.files tr.ghip-image-previews,
  22. table.files.ghip-show-previews tbody tr.js-navigation-item {
  23. display:none; }
  24. table.files.ghip-show-previews tr.ghip-image-previews { display:table-row; }
  25. table.files.ghip-show-previews .ghip-non-image {
  26. height:80px; margin-top:15px; opacity:.2; }
  27. table.files.ghip-show-previews .image { position:relative; overflow:hidden;
  28. text-align:center; }
  29. .ghip-image-previews .image { padding:10px; }
  30. table.files.ghip-tiled .image { width:22.5%; height:180px;
  31. margin:12px !important; /* GitHub uses !important flags now :( */ }
  32. table.files.ghip-tiled .image .border-wrap img,
  33. .ghip-image-previews .border-wrap svg { max-height:130px; }
  34. table.files.ghip-fullw .image { width:97%; height:auto; }
  35. /* zoom doesn't work in Firefox, but "-moz-transform:scale(3);"
  36. doesn't limit the size of the image, so it overflows */
  37. table.files.ghip-tiled .image:hover img:not(.ghip-non-image) { zoom:3; }
  38. .ghip-image-previews .border-wrap img,
  39. .ghip-image-previews .border-wrap svg { max-width:95%; }
  40. .ghip-image-previews .border-wrap h4 { white-space:nowrap;
  41. text-overflow:ellipsis; margin-bottom:5px; }
  42. .ghip-image-previews .border-wrap h4.ghip-file-name { overflow:hidden; }
  43. .btn.ghip-tiled > *, .btn.ghip-fullw > *, .ghip-image-previews iframe {
  44. pointer-events:none; vertical-align:baseline; }
  45. .image .ghip-file-type { font-size:30px; top:-1.8em; position:relative;
  46. z-index:2; }
  47. .ghip-content span.exploregrid-item .ghip-file-name { cursor:default; }
  48. /* override GitHub-Dark styles */
  49. table.files img[src*='octocat-spinner'], img[src='/images/spinner.gif'] {
  50. width:auto !important; height:auto !important; }
  51. table.files td .simplified-path { color:#888 !important; }
  52. `);
  53.  
  54. // supported img types
  55. const imgExt = /(png|jpg|jpeg|gif|tif|tiff|bmp|webp)$/i,
  56. svgExt = /svg$/i,
  57.  
  58. tiled = `
  59. <svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 16 16">
  60. <path d="M0 0h7v7H0zM9 9h7v7H9zM9 0h7v7H9zM0 9h7v7H0z"/>
  61. </svg>
  62. `,
  63. fullWidth = `
  64. <svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 16 16">
  65. <path d="M0 0h16v7H0zM0 9h16v7H0z"/>
  66. </svg>
  67. `,
  68. imgTemplate = [
  69. // not using backticks here
  70. "<a href='${url}' class='exploregrid-item image m-3 float-left js-navigation-open' rel='nofollow'>",
  71. "<span class='border-wrap'>${image}</span>",
  72. "</a>"
  73. ].join(""),
  74. spanTemplate = [
  75. "<span class='exploregrid-item image m-3 float-left'>",
  76. "<span class='border-wrap'>${image}</span>",
  77. "</span>"
  78. ].join("");
  79.  
  80. function addToggles() {
  81. if ($(".gh-img-preview")) {
  82. return;
  83. }
  84. const div = document.createElement("div"),
  85. btn = `btn btn-sm BtnGroup-item tooltipped tooltipped-n" aria-label="Show`;
  86. div.className = "BtnGroup float-right gh-img-preview";
  87. div.innerHTML = `
  88. <div class="ghip-tiled ${btn} tiled files with image preview">${tiled}</div>
  89. <div class="ghip-fullw ${btn} full width files with image preview">${fullWidth}</div>
  90. `;
  91. $(".file-navigation").appendChild(div);
  92.  
  93. $(".ghip-tiled", div).addEventListener("click", () => {
  94. openView("tiled");
  95. });
  96. $(".ghip-fullw", div).addEventListener("click", () => {
  97. openView("fullw");
  98. });
  99. }
  100.  
  101. function setInitState() {
  102. const view = GM_getValue("gh-image-preview");
  103. if (view) {
  104. openView(view);
  105. }
  106. }
  107.  
  108. function openView(name) {
  109. const el = $(".ghip-" + name);
  110. if (el) {
  111. el.classList.toggle("selected");
  112. if (el.classList.contains("selected")) {
  113. GM_setValue("gh-image-preview", name);
  114. showPreview(name);
  115. } else {
  116. GM_setValue("gh-image-preview", "");
  117. showList();
  118. }
  119. }
  120. }
  121.  
  122. function showPreview(size) {
  123. buildPreviews();
  124. const table = $("table.files"),
  125. btn1 = "ghip-" + size,
  126. btn2 = "ghip-" + (size === "fullw" ? "tiled" : "fullw");
  127. table.classList.add(...["ghip-show-previews", btn1]);
  128. $(".btn." + btn1).classList.add("selected");
  129. table.classList.remove(btn2);
  130. $(".btn." + btn2).classList.remove("selected");
  131. }
  132.  
  133. function showList() {
  134. $("table.files").classList.remove(...[
  135. "ghip-show-previews",
  136. "ghip-tiled",
  137. "ghip-fullw"
  138. ]);
  139. $(".btn.ghip-tiled").classList.remove("selected");
  140. $(".btn.ghip-fullw").classList.remove("selected");
  141. }
  142.  
  143. function buildPreviews() {
  144. let template, url, temp, noExt,
  145. imgs = "<td colspan='4' class='ghip-content'>",
  146. indx = 0;
  147. const row = document.createElement("tr"),
  148. table = $("table.files tbody:last-child"),
  149. files = $$("tr.js-navigation-item"),
  150. len = files.length;
  151. row.className = "ghip-image-previews";
  152. if ($(".ghip-image-previews")) {
  153. temp = $(".ghip-image-previews");
  154. temp.parentNode.removeChild(temp);
  155. }
  156. if (table) {
  157. for (indx = 0; indx < len; indx++) {
  158. // not every submodule includes a link; reference examples from
  159. // see https://github.com/electron/electron/tree/v1.1.1/vendor
  160. temp = $("td.content a", files[indx]) ||
  161. $("td.content span span", files[indx]);
  162. // use innerHTML because some links include path - see "third_party/lss"
  163. template = temp ? temp.innerHTML.trim() + "</h4>" : "";
  164. // temp = temp && $("a", temp);
  165. url = temp && temp.nodeName === "A" ? temp.href : "";
  166. // add link color
  167. template = "<h4 class='ghip-file-name" + (url ? " text-blue" : "") +
  168. "'>" + template;
  169. if (imgExt.test(url)) {
  170. // *** image preview ***
  171. template += "<img src='" + url + "?raw=true'/>";
  172. imgs += imgTemplate
  173. .replace("${url}", url)
  174. .replace("${image}", template);
  175. } else if (svgExt.test(url)) {
  176. // *** svg preview ***
  177. // loaded & encoded because GitHub sets content-type headers as
  178. // a string
  179. temp = url.substring(url.lastIndexOf("/") + 1, url.length);
  180. template += `<img data-svg-holder="${temp}" alt="${temp}" />`;
  181. imgs += updateTemplate(url, template);
  182. getSVG(url + "?raw=true");
  183. } else {
  184. // *** non-images (file/folder icons) ***
  185. temp = $("td.icon svg", files[indx]);
  186. if (temp) {
  187. // non-files svg class: "octicon-file-directory" or
  188. // "octicon-file-submodule"
  189. noExt = temp.classList.contains("octicon-file-directory") ||
  190. temp.classList.contains("octicon-file-submodule");
  191. // add xmlns otherwise the svg won't work inside an img
  192. // GitHub doesn't include this attribute on any svg octicons
  193. temp = temp.outerHTML
  194. .replace("<svg", "<svg xmlns='http://www.w3.org/2000/svg'");
  195. // include "leaflet-tile-container" to invert icon for GitHub-Dark
  196. template += "<span class='leaflet-tile-container'>" +
  197. "<img class='ghip-non-image' src='data:image/svg+xml;base64," +
  198. window.btoa(temp) + "'/>" +
  199. "</span>";
  200. // get file name + extension
  201. temp = url.substring(url.lastIndexOf("/") + 1, url.length);
  202. // don't include extension for folders, or files with no extension,
  203. // or files starting with a "." (e.g. ".gitignore")
  204. template += (!noExt && temp.indexOf(".") > 0) ?
  205. "<h4 class='ghip-file-type'>" +
  206. temp
  207. .substring(temp.lastIndexOf(".") + 1, temp.length)
  208. .toUpperCase() +
  209. "</h4>" : "";
  210. imgs += url ?
  211. updateTemplate(url, template) :
  212. // empty url; use non-link template
  213. // see "depot_tools @ 4fa73b8" at
  214. // https://github.com/electron/electron/tree/v1.1.1/vendor
  215. updateTemplate(url, template, spanTemplate);
  216. } else if (files[indx].classList.contains("up-tree")) {
  217. // Up tree link
  218. temp = $("td:nth-child(2) a", files[indx]);
  219. url = temp ? temp.href : "";
  220. imgs += url ?
  221. updateTemplate(
  222. url,
  223. "<h4 class='text-blue'>&middot;&middot;</h4>"
  224. ) : "";
  225. }
  226. }
  227. }
  228. row.innerHTML = imgs + "</td>";
  229. table.appendChild(row);
  230. }
  231. }
  232.  
  233. function updateTemplate(url, img, tmpl) {
  234. return (tmpl || imgTemplate)
  235. .replace("${url}", url)
  236. .replace("${image}", img);
  237. }
  238.  
  239. function getSVG(url) {
  240. GM_xmlhttpRequest({
  241. method: "GET",
  242. url,
  243. onload: response => {
  244. let encoded;
  245. const url = response.finalUrl,
  246. file = url.substring(url.lastIndexOf("/") + 1, url.length),
  247. target = $("[data-svg-holder='" + file + "']");
  248. if (target) {
  249. encoded = window.btoa(response.responseText);
  250. target.src = "data:image/svg+xml;base64," + encoded;
  251. }
  252. }
  253. });
  254. }
  255.  
  256. function $(selector, el) {
  257. return (el || document).querySelector(selector);
  258. }
  259. function $$(selector, el) {
  260. return Array.from((el || document).querySelectorAll(selector));
  261. }
  262.  
  263. function init() {
  264. if ($("table.files")) {
  265. addToggles();
  266. setInitState();
  267. }
  268. }
  269.  
  270. document.addEventListener("pjax:end", init);
  271. init();
  272. })();