GitHub Image Preview

A userscript that adds clickable image thumbnails

当前为 2016-05-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Image Preview
  3. // @version 1.0.4
  4. // @description A userscript that adds clickable image thumbnails
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @namespace http://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. /* global GM_addStyle, GM_getValue, GM_setValue, GM_xmlhttpRequest */
  18. /*jshint unused:true */
  19. (function() {
  20. "use strict";
  21.  
  22. GM_addStyle([
  23. "table.files tr.ghip-image-previews, table.files.ghip-show-previews tbody tr.js-navigation-item { display:none; }",
  24. "table.files.ghip-show-previews tr.ghip-image-previews { display:table-row; }",
  25. "table.files.ghip-show-previews .ghip-non-image { height:80px; margin-top:15px; opacity:.8; }",
  26. "table.files.ghip-show-previews .image { position:relative; overflow:hidden; text-align:center; }",
  27. ".ghip-image-previews .image { padding:10px; }",
  28. "table.files.ghip-tiled .image { width:21.9%; }",
  29. "table.files.ghip-tiled .image .border-wrap img, .ghip-image-previews .border-wrap svg { max-height:130px; }",
  30. "table.files.ghip-fullw .image { width:97%; height:auto; }",
  31. "table.files.ghip-tiled .image:hover img:not(.ghip-non-image) { zoom:3; }",
  32. ".ghip-image-previews .border-wrap img, .ghip-image-previews .border-wrap svg { max-width:95%; }",
  33. ".ghip-image-previews .border-wrap h4 { overflow:hidden; white-space:nowrap; text-overflow:ellipsis; margin-bottom:5px; }",
  34. ".btn.ghip-tiled > *, .btn.ghip-fullw > *, .ghip-image-previews iframe { pointer-events:none; }",
  35. ".image .ghip-file-type { font-size:18px; margin-top:10px; }",
  36. // override GitHub-Dark styles
  37. "table.files img[src*='octocat-spinner'], img[src='/images/spinner.gif'] { width:auto !important; height:auto !important; }"
  38. ].join(""));
  39.  
  40. var busy = false,
  41.  
  42. // supported img types
  43. imgExt = /(png|jpg|jpeg|gif|tif|tiff|bmp|webp)$/,
  44. svgExt = /svg$/,
  45.  
  46. tiled = [
  47. "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 16 16'>",
  48. "<path d='M0 0h7v7H0zM9 9h7v7H9zM9 0h7v7H9zM0 9h7v7H0z'/>",
  49. "</svg>"
  50. ].join(""),
  51.  
  52. fullWidth = [
  53. "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 16 16'>",
  54. "<path d='M0 0h16v7H0zM0 9h16v7H0z'/>",
  55. "</svg>"
  56. ].join(""),
  57.  
  58. imgTemplate = [
  59. "<a href='${url}' class='exploregrid-item image js-navigation-open' rel='nofollow'>",
  60. "<span class='border-wrap'>${image}</span>",
  61. "</a>"
  62. ].join(""),
  63.  
  64. addToggles = function() {
  65. if (document.querySelector(".gh-img-preview")) { return; }
  66. busy = true;
  67. var div = document.createElement("div"),
  68. btn = " btn btn-sm tooltipped tooltipped-n' aria-label='Show ";
  69. div.className = "btn-group right gh-img-preview";
  70. div.innerHTML = [
  71. "<div class='ghip-tiled" + btn + "tiled files with image preview'>" + tiled + "</div>",
  72. "<div class='ghip-fullw" + btn + "full width files with image preview'>" + fullWidth + "</div>"
  73. ].join("");
  74. document.querySelector(".file-navigation").appendChild(div);
  75.  
  76. div.querySelector(".ghip-tiled").addEventListener("click", function() {
  77. openView("tiled");
  78. });
  79. div.querySelector(".ghip-fullw").addEventListener("click", function() {
  80. openView("fullw");
  81. });
  82. busy = false;
  83. },
  84.  
  85. setInitState = function() {
  86. var view = GM_getValue("gh-image-preview");
  87. if (view) {
  88. openView(view);
  89. }
  90. },
  91.  
  92. openView = function(name) {
  93. var el = document.querySelector(".ghip-" + name);
  94. if (el) {
  95. el.classList.toggle("selected");
  96. if (el.classList.contains("selected")) {
  97. GM_setValue("gh-image-preview", name);
  98. showPreview(name);
  99. } else {
  100. GM_setValue("gh-image-preview", "");
  101. showList();
  102. }
  103. }
  104. },
  105.  
  106. showPreview = function(size) {
  107. buildPreviews();
  108. var table = document.querySelector("table.files"),
  109. btn1 = "ghip-" + size,
  110. btn2 = "ghip-" + (size === "fullw" ? "tiled" : "fullw");
  111. table.classList.add("ghip-show-previews");
  112. table.classList.add(btn1);
  113. document.querySelector(".btn." + btn1).classList.add("selected");
  114. table.classList.remove(btn2);
  115. document.querySelector(".btn." + btn2).classList.remove("selected");
  116. },
  117.  
  118. showList = function() {
  119. var table = document.querySelector("table.files");
  120. table.classList.remove("ghip-show-previews");
  121. table.classList.remove("ghip-tiled");
  122. table.classList.remove("ghip-fullw");
  123. document.querySelector(".btn.ghip-tiled").classList.remove("selected");
  124. document.querySelector(".btn.ghip-fullw").classList.remove("selected");
  125. },
  126.  
  127. buildPreviews = function() {
  128. busy = true;
  129. var template, url, temp, noExt,
  130. indx = 0,
  131. row = document.createElement("tr"),
  132. imgs = "<td colspan='4' class='ghip-content'>",
  133. table = document.querySelector("table.files tbody:last-child"),
  134. files = document.querySelectorAll("tr.js-navigation-item"),
  135. len = files.length;
  136. row.className = "ghip-image-previews";
  137. if (document.querySelector(".ghip-image-previews")) {
  138. temp = document.querySelector(".ghip-image-previews");
  139. temp.parentNode.removeChild(temp);
  140. }
  141. if (table) {
  142. for (indx = 0; indx < len; indx++) {
  143. temp = files[indx].querySelector("td.content a");
  144. template = temp ? "<h4>" + temp.textContent.trim() + "</h4>" : "";
  145. url = temp ? temp.href : "";
  146. if (imgExt.test(url)) {
  147. // *** image preview ***
  148. template += "<img src='" + url + "?raw=true'/>";
  149. imgs += imgTemplate.replace("${url}", url).replace("${image}", template);
  150. } else if (svgExt.test(url)) {
  151. // *** svg preview ***
  152. // loaded & encoded because GitHub sets content-type headers as a string
  153. temp = url.substring(url.lastIndexOf("/") + 1, url.length);
  154. template += "<img data-svg-holder='" + temp + "' alt='" + temp + "' />";
  155. imgs += updateTemplate(url, template);
  156. getSVG(url + "?raw=true");
  157. } else {
  158. // *** non-images (file/folder icons) ***
  159. temp = files[indx].querySelector("td.icon svg");
  160. if (temp) {
  161. noExt = temp.classList.contains("octicon-file-directory");
  162. // add xmlns otherwise the svg won't work inside an img
  163. // GitHub doesn't include this attribute on any svg octicons
  164. temp = temp.outerHTML.replace("<svg", "<svg xmlns='http://www.w3.org/2000/svg'");
  165. // include "leaflet-tile-container" to invert icon for GitHub-Dark
  166. template += "<span class='leaflet-tile-container'>" +
  167. "<img class='ghip-non-image' src='data:image/svg+xml;base64," + window.btoa(temp) + "'/>" +
  168. "</span>";
  169. // get file name + extension
  170. temp = url.substring(url.lastIndexOf("/") + 1, url.length);
  171. // don't include extension for folders, or files with no extension, or
  172. // files starting with a "." (e.g. ".gitignore")
  173. if (!noExt && temp.indexOf(".") > 0) {
  174. template += "<h4 class='ghip-file-type'>" +
  175. temp.substring(temp.lastIndexOf(".") + 1, temp.length).toUpperCase() + "</h4>";
  176. }
  177. imgs += updateTemplate(url, template);
  178. } else if (files[indx].classList.contains("up-tree")) {
  179. // Up tree link
  180. temp = files[indx].querySelector("td:nth-child(2) a");
  181. url = temp ? temp.href : "";
  182. if (url) {
  183. imgs += updateTemplate(url, "<h4>&middot;&middot</h4>");
  184. }
  185. }
  186. }
  187. }
  188. row.innerHTML = imgs + "</td>";
  189. table.appendChild(row);
  190. }
  191. busy = false;
  192. },
  193.  
  194. updateTemplate = function(url, img) {
  195. return imgTemplate
  196. .replace("${url}", url)
  197. .replace("${image}", img);
  198. },
  199.  
  200. getSVG = function(url) {
  201. GM_xmlhttpRequest({
  202. method: "GET",
  203. url: url,
  204. onload : function(response) {
  205. busy = true;
  206. var encoded,
  207. url = response.finalUrl,
  208. file = url.substring(url.lastIndexOf("/") + 1, url.length),
  209. target = document.querySelector("[data-svg-holder='" + file+ "']");
  210. if (target) {
  211. encoded = window.btoa(response.responseText);
  212. target.src = "data:image/svg+xml;base64," + encoded;
  213. }
  214. busy = false;
  215. }
  216. });
  217. },
  218.  
  219. init = function() {
  220. if (document.querySelector("table.files")) {
  221. addToggles();
  222. setInitState();
  223. }
  224. },
  225.  
  226. // timer needed for file list to update?
  227. timer,
  228.  
  229. // DOM targets - to detect GitHub dynamic ajax page loading
  230. targets = document.querySelectorAll([
  231. "#js-repo-pjax-container",
  232. ".context-loader-container",
  233. "[data-pjax-container]"
  234. ].join(","));
  235.  
  236. Array.prototype.forEach.call(targets, function(target) {
  237. new MutationObserver(function(mutations) {
  238. mutations.forEach(function(mutation) {
  239. // preform checks before adding code wrap to minimize function calls
  240. if (!busy && mutation.target === target) {
  241. clearTimeout(timer);
  242. timer = setTimeout(init, 200);
  243. }
  244. });
  245. }).observe(target, {
  246. childList: true,
  247. subtree: true
  248. });
  249. });
  250.  
  251. init();
  252.  
  253. })();