Github Image Viewer

Preview images from within the listing.

目前为 2014-11-04 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @id Github_Image_Viewer@https://github.com/jerone/UserScripts
  3. // @name Github Image Viewer
  4. // @namespace https://github.com/jerone/UserScripts
  5. // @description Preview images from within the listing.
  6. // @author jerone
  7. // @copyright 2014+, jerone (http://jeroenvanwarmerdam.nl)
  8. // @license GNU GPLv3
  9. // @homepage https://github.com/jerone/UserScripts/tree/master/Github_Image_Viewer
  10. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Image_Viewer
  11. // @version 0.1.0
  12. // @grant none
  13. // @run-at document-end
  14. // @include https://github.com/*
  15. // ==/UserScript==
  16.  
  17. (function() {
  18.  
  19. String.format = function(string) {
  20. var args = Array.prototype.slice.call(arguments, 1, arguments.length);
  21. return string.replace(/{(\d+)}/g, function(match, number) {
  22. return typeof args[number] !== "undefined" ? args[number] : match;
  23. });
  24. };
  25.  
  26. function proxy(fn) {
  27. return function() {
  28. var that = this;
  29. return function(e) {
  30. var args = that.slice(0); // clone;
  31. args.unshift(e); // prepend event;
  32. fn.apply(this, args);
  33. };
  34. }.call([].slice.call(arguments, 1));
  35. }
  36.  
  37. var GithubImageViewer = {
  38. _floater: null,
  39. _floaterTitle: null,
  40. _floaterImage: null,
  41. _floaterMeta: null,
  42.  
  43. _imageUrl: null,
  44. _loaderSrc: "https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif",
  45. _imageRegex: /(.png|.jpg|.jpeg|.tif|.tiff|.gif|.ico)$/,
  46.  
  47. Initialize: function() {
  48. var floater = GithubImageViewer._floater = document.createElement("div");
  49. floater.style.position = "absolute";
  50. floater.style.top = "0";
  51. floater.style.left = "0";
  52. document.body.appendChild(floater);
  53.  
  54. var floaterMouseAlign = document.createElement("div");
  55. floaterMouseAlign.style.position = "absolute";
  56. floaterMouseAlign.style.bottom = "5px";
  57. floaterMouseAlign.style.left = "5px";
  58. floaterMouseAlign.style.border = "1px solid #b7c7cf";
  59. floaterMouseAlign.style.borderRadius = "3px";
  60. floaterMouseAlign.style.fontSize = "11px";
  61. floater.appendChild(floaterMouseAlign);
  62.  
  63. var floaterTitle = GithubImageViewer._floaterTitle = document.createElement("div");
  64. floaterTitle.style.backgroundColor = "#e6f1f6";
  65. floaterTitle.style.borderBottom = "1px solid #d8e6ec";
  66. floaterTitle.style.padding = "3px 5px";
  67. floaterMouseAlign.appendChild(floaterTitle);
  68.  
  69. var floaterCenter = document.createElement("div");
  70. floaterCenter.style.minWidth = "40px";
  71. floaterCenter.style.minHeight = "40px";
  72. floaterCenter.style.display = "flex";
  73. floaterCenter.style.flexDirection = "column";
  74. floaterCenter.style.backgroundColor = "#f8f8f8";
  75. floaterCenter.style.padding = "3px";
  76. floaterMouseAlign.appendChild(floaterCenter);
  77.  
  78. var floaterImage = GithubImageViewer._floaterImage = document.createElement("img");
  79. floaterImage.setAttribute("src", GithubImageViewer._loaderSrc);
  80. floaterImage.style.margin = "auto";
  81. floaterImage.style.maxWidth = floaterImage.style.maxHeight = "200px";
  82. floaterCenter.appendChild(floaterImage);
  83.  
  84. var floaterMeta = GithubImageViewer._floaterMeta = document.createElement("div");
  85. floaterMeta.style.backgroundColor = "#f8f8f8";
  86. floaterMeta.style.padding = "3px";
  87. floaterMeta.style.textAlign = "center";
  88. floaterMeta.style.whiteSpace = "nowrap";
  89. floaterMouseAlign.appendChild(floaterMeta);
  90. GithubImageViewer.SetMeta();
  91.  
  92. GithubImageViewer.Attach();
  93. },
  94.  
  95. Attach: function() {
  96. document.getElementById("js-repo-pjax-container").addEventListener("mousemove", function(e) {
  97. var target = e.target;
  98. if (target.classList && target.classList.contains("js-directory-link")
  99. && GithubImageViewer._imageRegex.test(target.href)) {
  100.  
  101. if (GithubImageViewer._visible) {
  102. GithubImageViewer.Show(e.pageX, e.pageY);
  103. } else {
  104. GithubImageViewer.AddTimer(proxy(function() {
  105. GithubImageViewer.ClearTimers();
  106.  
  107. GithubImageViewer.Show(e.pageX, e.pageY);
  108.  
  109. var href = target.href;
  110. if (GithubImageViewer._imageUrl !== href) {
  111. GithubImageViewer._imageUrl = href;
  112. GithubImageViewer.SetImage(GithubImageViewer._imageUrl);
  113.  
  114. GithubImageViewer.SetTitle(target.getAttribute("title"));
  115. }
  116. }));
  117. }
  118. } else {
  119. GithubImageViewer.ClearTimers();
  120.  
  121. GithubImageViewer.Hide();
  122.  
  123. GithubImageViewer._imageUrl = GithubImageViewer._loaderSrc;
  124. GithubImageViewer.SetImage(GithubImageViewer._imageUrl);
  125.  
  126. GithubImageViewer.SetTitle("Loading...");
  127. }
  128. });
  129. },
  130.  
  131. _visible: false,
  132. Show: function(x, y) {
  133. GithubImageViewer._visible = true;
  134. GithubImageViewer._floater.style.left = x + "px";
  135. GithubImageViewer._floater.style.top = y + "px";
  136. },
  137. Hide: function() {
  138. GithubImageViewer._visible = false;
  139. GithubImageViewer._floater.style.left = "-1000px";
  140. GithubImageViewer._floater.style.top = "-1000px";
  141. },
  142.  
  143. _timers: [],
  144. _timeout: 700,
  145. AddTimer: function(fn) {
  146. GithubImageViewer._timers.push(window.setTimeout(fn, GithubImageViewer._timeout));
  147. },
  148. ClearTimers: function() {
  149. Array.prototype.forEach.call(GithubImageViewer._timers, function(timer) {
  150. window.clearTimeout(timer);
  151. });
  152. },
  153.  
  154. SetTitle: function(text) {
  155. GithubImageViewer._floaterTitle.textContent = text;
  156. },
  157.  
  158. SetImage: function(src) {
  159. src = src.replace("/blob/", "/raw/");
  160. if (src !== GithubImageViewer._loaderSrc) {
  161. var temp = document.createElement("img");
  162. temp.style.visibility = "hidden";
  163. temp.addEventListener("load", function() {
  164. GithubImageViewer.SetMeta(this.width, this.height);
  165. this.parentNode.removeChild(temp);
  166. });
  167. temp.setAttribute("src", src);
  168. document.body.appendChild(temp);
  169. } else {
  170. GithubImageViewer.SetMeta();
  171. }
  172.  
  173. GithubImageViewer._floaterImage.setAttribute("src", src);
  174. },
  175.  
  176. SetMeta: function(w, h) {
  177. if (!w && !h) {
  178. GithubImageViewer._floaterMeta.style.display = "none";
  179. } else {
  180. GithubImageViewer._floaterMeta.style.display = "block";
  181. GithubImageViewer._floaterMeta.innerHTML = String.format("<strong>W:</strong> {0}px | <strong>H:</strong> {1}px", w, h);
  182. }
  183. }
  184. };
  185.  
  186. if (document.getElementById("js-repo-pjax-container")) {
  187. GithubImageViewer.Initialize();
  188. }
  189.  
  190. })();