GitHub Image Preview

A userscript that adds clickable image thumbnails

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

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址