GitHub Image Preview

A userscript that adds clickable image thumbnails

当前为 2018-01-18 提交的版本,查看 最新版本

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