GitHub Image Preview

A userscript that adds clickable image thumbnails

当前为 2017-04-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Image Preview
  3. // @version 1.1.5
  4. // @description A userscript that adds clickable image thumbnails
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  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=188043
  17. // @icon https://github.com/fluidicon.png
  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. tiled = `
  61. <svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 16 16">
  62. <path d="M0 0h7v7H0zM9 9h7v7H9zM9 0h7v7H9zM0 9h7v7H0z"/>
  63. </svg>
  64. `,
  65. fullWidth = `
  66. <svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 16 16">
  67. <path d="M0 0h16v7H0zM0 9h16v7H0z"/>
  68. </svg>
  69. `,
  70. imgTemplate = [
  71. // not using backticks here
  72. "<a href='${url}' class='exploregrid-item image m-3 float-left js-navigation-open' rel='nofollow'>",
  73. "<span class='border-wrap'>${image}</span>",
  74. "</a>"
  75. ].join(""),
  76. spanTemplate = [
  77. "<span class='exploregrid-item image m-3 float-left'>",
  78. "<span class='border-wrap'>${image}</span>",
  79. "</span>"
  80. ].join("");
  81.  
  82. function addToggles() {
  83. if ($(".gh-img-preview")) {
  84. return;
  85. }
  86. const div = document.createElement("div"),
  87. btn = `btn btn-sm BtnGroup-item tooltipped tooltipped-n" aria-label="Show`;
  88. div.className = "BtnGroup float-right gh-img-preview";
  89. div.innerHTML = `
  90. <div class="ghip-tiled ${btn} tiled files with image preview">${tiled}</div>
  91. <div class="ghip-fullw ${btn} full width files with image preview">${fullWidth}</div>
  92. `;
  93. $(".file-navigation").appendChild(div);
  94.  
  95. $(".ghip-tiled", div).addEventListener("click", () => {
  96. openView("tiled");
  97. });
  98. $(".ghip-fullw", div).addEventListener("click", () => {
  99. openView("fullw");
  100. });
  101. }
  102.  
  103. function setInitState() {
  104. const view = GM_getValue("gh-image-preview");
  105. if (view) {
  106. openView(view);
  107. }
  108. }
  109.  
  110. function openView(name) {
  111. const el = $(".ghip-" + name);
  112. if (el) {
  113. el.classList.toggle("selected");
  114. if (el.classList.contains("selected")) {
  115. GM_setValue("gh-image-preview", name);
  116. showPreview(name);
  117. } else {
  118. GM_setValue("gh-image-preview", "");
  119. showList();
  120. }
  121. }
  122. }
  123.  
  124. function showPreview(size) {
  125. buildPreviews();
  126. const table = $("table.files"),
  127. btn1 = "ghip-" + size,
  128. btn2 = "ghip-" + (size === "fullw" ? "tiled" : "fullw");
  129. table.classList.add(...["ghip-show-previews", btn1]);
  130. $(".btn." + btn1).classList.add("selected");
  131. table.classList.remove(btn2);
  132. $(".btn." + btn2).classList.remove("selected");
  133. }
  134.  
  135. function showList() {
  136. $("table.files").classList.remove(...[
  137. "ghip-show-previews",
  138. "ghip-tiled",
  139. "ghip-fullw"
  140. ]);
  141. $(".btn.ghip-tiled").classList.remove("selected");
  142. $(".btn.ghip-fullw").classList.remove("selected");
  143. }
  144.  
  145. function buildPreviews() {
  146. let template, url, temp, noExt,
  147. imgs = "<td colspan='4' class='ghip-content'>",
  148. indx = 0;
  149. const row = document.createElement("tr"),
  150. table = $("table.files tbody:last-child"),
  151. files = $$("tr.js-navigation-item"),
  152. len = files.length;
  153. row.className = "ghip-image-previews";
  154. if ($(".ghip-image-previews")) {
  155. temp = $(".ghip-image-previews");
  156. temp.parentNode.removeChild(temp);
  157. }
  158. if (table) {
  159. for (indx = 0; indx < len; indx++) {
  160. // not every submodule includes a link; reference examples from
  161. // see https://github.com/electron/electron/tree/v1.1.1/vendor
  162. temp = $("td.content a", files[indx]) ||
  163. $("td.content span span", files[indx]);
  164. // use innerHTML because some links include path - see "third_party/lss"
  165. template = temp ? temp.innerHTML.trim() + "</h4>" : "";
  166. // temp = temp && $("a", temp);
  167. url = temp && temp.nodeName === "A" ? temp.href : "";
  168. // add link color
  169. template = "<h4 class='ghip-file-name" + (url ? " text-blue" : "") +
  170. "'>" + template;
  171. if (imgExt.test(url)) {
  172. // *** image preview ***
  173. template += "<img src='" + url + "?raw=true'/>";
  174. imgs += imgTemplate
  175. .replace("${url}", url)
  176. .replace("${image}", template);
  177. } else if (svgExt.test(url)) {
  178. // *** svg preview ***
  179. // loaded & encoded because GitHub sets content-type headers as
  180. // a string
  181. temp = url.substring(url.lastIndexOf("/") + 1, url.length);
  182. template += `<img data-svg-holder="${temp}" alt="${temp}" />`;
  183. imgs += updateTemplate(url, template);
  184. getSVG(url + "?raw=true");
  185. } else {
  186. // *** non-images (file/folder icons) ***
  187. temp = $("td.icon svg", files[indx]);
  188. if (temp) {
  189. // non-files svg class: "octicon-file-directory" or
  190. // "octicon-file-submodule"
  191. noExt = temp.classList.contains("octicon-file-directory") ||
  192. temp.classList.contains("octicon-file-submodule");
  193. // add xmlns otherwise the svg won't work inside an img
  194. // GitHub doesn't include this attribute on any svg octicons
  195. temp = temp.outerHTML
  196. .replace("<svg", "<svg xmlns='http://www.w3.org/2000/svg'");
  197. // include "leaflet-tile-container" to invert icon for GitHub-Dark
  198. template += "<span class='leaflet-tile-container'>" +
  199. "<img class='ghip-non-image' src='data:image/svg+xml;base64," +
  200. window.btoa(temp) + "'/>" +
  201. "</span>";
  202. // get file name + extension
  203. temp = url.substring(url.lastIndexOf("/") + 1, url.length);
  204. // don't include extension for folders, or files with no extension,
  205. // or files starting with a "." (e.g. ".gitignore")
  206. template += (!noExt && temp.indexOf(".") > 0) ?
  207. "<h4 class='ghip-file-type'>" +
  208. temp
  209. .substring(temp.lastIndexOf(".") + 1, temp.length)
  210. .toUpperCase() +
  211. "</h4>" : "";
  212. imgs += url ?
  213. updateTemplate(url, template) :
  214. // empty url; use non-link template
  215. // see "depot_tools @ 4fa73b8" at
  216. // https://github.com/electron/electron/tree/v1.1.1/vendor
  217. updateTemplate(url, template, spanTemplate);
  218. } else if (files[indx].classList.contains("up-tree")) {
  219. // Up tree link
  220. temp = $("td:nth-child(2) a", files[indx]);
  221. url = temp ? temp.href : "";
  222. imgs += url ?
  223. updateTemplate(
  224. url,
  225. "<h4 class='text-blue'>&middot;&middot;</h4>"
  226. ) : "";
  227. }
  228. }
  229. }
  230. row.innerHTML = imgs + "</td>";
  231. table.appendChild(row);
  232. }
  233. }
  234.  
  235. function updateTemplate(url, img, tmpl) {
  236. return (tmpl || imgTemplate)
  237. .replace("${url}", url)
  238. .replace("${image}", img);
  239. }
  240.  
  241. function getSVG(url) {
  242. GM_xmlhttpRequest({
  243. method: "GET",
  244. url,
  245. onload: response => {
  246. let encoded;
  247. const url = response.finalUrl,
  248. file = url.substring(url.lastIndexOf("/") + 1, url.length),
  249. target = $("[data-svg-holder='" + file + "']");
  250. if (target) {
  251. encoded = window.btoa(response.responseText);
  252. target.src = "data:image/svg+xml;base64," + encoded;
  253. }
  254. }
  255. });
  256. }
  257.  
  258. function $(selector, el) {
  259. return (el || document).querySelector(selector);
  260. }
  261. function $$(selector, el) {
  262. return Array.from((el || document).querySelectorAll(selector));
  263. }
  264.  
  265. function init() {
  266. if ($("table.files")) {
  267. addToggles();
  268. setInitState();
  269. }
  270. }
  271.  
  272. document.addEventListener("ghmo:container", init);
  273. init();
  274. })();