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.1
  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: /(.jpe?g|.png|.gif|.ico|.tiff?)$/,
  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. floater.style.zIndex = "999";
  53. document.body.appendChild(floater);
  54.  
  55. var floaterMouseAlign = document.createElement("div");
  56. floaterMouseAlign.style.position = "absolute";
  57. floaterMouseAlign.style.bottom = "5px";
  58. floaterMouseAlign.style.left = "5px";
  59. floaterMouseAlign.style.border = "1px solid #b7c7cf";
  60. floaterMouseAlign.style.borderRadius = "3px";
  61. floaterMouseAlign.style.fontSize = "11px";
  62. floater.appendChild(floaterMouseAlign);
  63.  
  64. var floaterTitle = GithubImageViewer._floaterTitle = document.createElement("div");
  65. floaterTitle.style.backgroundColor = "#e6f1f6";
  66. floaterTitle.style.borderBottom = "1px solid #d8e6ec";
  67. floaterTitle.style.padding = "3px 5px";
  68. floaterMouseAlign.appendChild(floaterTitle);
  69.  
  70. var floaterCenter = document.createElement("div");
  71. floaterCenter.style.minWidth = "40px";
  72. floaterCenter.style.minHeight = "40px";
  73. floaterCenter.style.display = "flex";
  74. floaterCenter.style.flexDirection = "column";
  75. floaterCenter.style.backgroundColor = "#f8f8f8";
  76. floaterCenter.style.padding = "3px";
  77. floaterMouseAlign.appendChild(floaterCenter);
  78.  
  79. var floaterImage = GithubImageViewer._floaterImage = document.createElement("img");
  80. floaterImage.setAttribute("src", GithubImageViewer._loaderSrc);
  81. floaterImage.style.margin = "auto";
  82. floaterImage.style.maxWidth = floaterImage.style.maxHeight = "200px";
  83. floaterCenter.appendChild(floaterImage);
  84.  
  85. var floaterMeta = GithubImageViewer._floaterMeta = document.createElement("div");
  86. floaterMeta.style.backgroundColor = "#f8f8f8";
  87. floaterMeta.style.padding = "3px";
  88. floaterMeta.style.textAlign = "center";
  89. floaterMeta.style.whiteSpace = "nowrap";
  90. floaterMouseAlign.appendChild(floaterMeta);
  91. GithubImageViewer.SetMeta();
  92.  
  93. GithubImageViewer.Attach();
  94. },
  95.  
  96. Attach: function() {
  97. document.getElementById("js-repo-pjax-container").addEventListener("mousemove", function(e) {
  98. var target = e.target;
  99. if (target.classList && target.classList.contains("js-directory-link")
  100. && GithubImageViewer._imageRegex.test(target.href)) {
  101.  
  102. if (GithubImageViewer._visible) {
  103. GithubImageViewer.Show(e.pageX, e.pageY);
  104. } else {
  105. GithubImageViewer.AddTimer(proxy(function() {
  106. GithubImageViewer.ClearTimers();
  107.  
  108. GithubImageViewer.Show(e.pageX, e.pageY);
  109.  
  110. var href = target.href;
  111. if (GithubImageViewer._imageUrl !== href) {
  112. GithubImageViewer._imageUrl = href;
  113. GithubImageViewer.SetImage(GithubImageViewer._imageUrl);
  114.  
  115. GithubImageViewer.SetTitle(target.getAttribute("title"));
  116. }
  117. }));
  118. }
  119. } else {
  120. GithubImageViewer.ClearTimers();
  121.  
  122. GithubImageViewer.Hide();
  123.  
  124. GithubImageViewer._imageUrl = GithubImageViewer._loaderSrc;
  125. GithubImageViewer.SetImage(GithubImageViewer._imageUrl);
  126.  
  127. GithubImageViewer.SetTitle("Loading...");
  128. }
  129. });
  130. },
  131.  
  132. _visible: false,
  133. Show: function(x, y) {
  134. GithubImageViewer._visible = true;
  135. GithubImageViewer._floater.style.left = x + "px";
  136. GithubImageViewer._floater.style.top = y + "px";
  137. },
  138. Hide: function() {
  139. GithubImageViewer._visible = false;
  140. GithubImageViewer._floater.style.left = "-1000px";
  141. GithubImageViewer._floater.style.top = "-1000px";
  142. },
  143.  
  144. _timers: [],
  145. _timeout: 700,
  146. AddTimer: function(fn) {
  147. GithubImageViewer._timers.push(window.setTimeout(fn, GithubImageViewer._timeout));
  148. },
  149. ClearTimers: function() {
  150. Array.prototype.forEach.call(GithubImageViewer._timers, function(timer) {
  151. window.clearTimeout(timer);
  152. });
  153. },
  154.  
  155. SetTitle: function(text) {
  156. GithubImageViewer._floaterTitle.textContent = text;
  157. },
  158.  
  159. SetImage: function(src) {
  160. src = src.replace("/blob/", "/raw/");
  161. if (src !== GithubImageViewer._loaderSrc) {
  162. var temp = document.createElement("img");
  163. temp.style.visibility = "hidden";
  164. temp.addEventListener("load", function() {
  165. GithubImageViewer.SetMeta(this.width, this.height);
  166. this.parentNode.removeChild(temp);
  167. });
  168. temp.setAttribute("src", src);
  169. document.body.appendChild(temp);
  170. } else {
  171. GithubImageViewer.SetMeta();
  172. }
  173.  
  174. GithubImageViewer._floaterImage.setAttribute("src", src);
  175. },
  176.  
  177. SetMeta: function(w, h) {
  178. if (!w && !h) {
  179. GithubImageViewer._floaterMeta.style.display = "none";
  180. } else {
  181. GithubImageViewer._floaterMeta.style.display = "block";
  182. GithubImageViewer._floaterMeta.innerHTML = String.format("<strong>W:</strong> {0}px | <strong>H:</strong> {1}px", w, h);
  183. }
  184. }
  185. };
  186.  
  187. if (document.getElementById("js-repo-pjax-container")) {
  188. GithubImageViewer.Initialize();
  189. }
  190.  
  191. })();

QingJ © 2025

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