Greasy Fork 还支持 简体中文。

Gitlab Wiki Player

Play Gitlab wiki like PPT!

  1. // ==UserScript==
  2. // @name Gitlab Wiki Player
  3. // @name:zh-cn Gitlab WIKI 播放器
  4. // @namespace http://chengxuan.li
  5. // @version 13.12.8
  6. // @description Play Gitlab wiki like PPT!
  7. // @description:zh-cn 像PPT一样播放Gitlab WIKI
  8. // @author Leelmes <i@chengxuan.li>
  9. // @match http*://*/*/wikis/*
  10. // @match http*://*/*/merge_requests/*
  11. // @match http*://*/*/snippets/*
  12. // @match http*://*/*/issues/*
  13. // @include http*://*/-/blob/*/*.md
  14. // @contributionURL https://www.paypal.me/flyhope
  15. // @grant none
  16. // @supportURL https://github.com/flyhope/gitlab-script/issues
  17. // @license GNU General Public License v3.0 or later
  18. // ==/UserScript==
  19. (function() {
  20. 'use strict';
  21.  
  22. // WIKI每页数据
  23. let wikis = [];
  24.  
  25. // WIKI DOM对象
  26. let $wiki = $(".md[data-qa-selector=wiki_page_content]")
  27.  
  28. // 当前看的是第几页(从0开始计算)
  29. let current = -1;
  30.  
  31. // 检测是否可以执行
  32. if (typeof jQuery !== "undefined" && $("header.navbar-gitlab").length) {
  33. main();
  34. }
  35.  
  36. // 主入口
  37. function main() {
  38. // WIKI:插入播放按钮
  39. $(".nav-controls").prepend('<a id="wiki-ppt-play" class="btn" href="javascript:void(0)">🍨</a>');
  40. // MergeRequest、ISSUE:插入播放按钮
  41. $(".detail-page-header-actions a.btn:first").before('<a id="wiki-ppt-play" class="btn btn-grouped" href="javascript:void(0)">🍨</a>');
  42. $(".detail-page-header-actions > button:first").after('<a id="wiki-ppt-play" class="btn btn-grouped" href="javascript:void(0)">🍨</a>');
  43. // Blob:插入播放按钮
  44. $(".tree-controls").prepend('<a id="wiki-ppt-play" class="btn" href="javascript:void(0)">🍨</a>');
  45.  
  46. // 绑定PPT按钮播放事件
  47. $("#wiki-ppt-play").click(function() {
  48. play();
  49. });
  50.  
  51. // 绑定H1/H2标题播放按钮
  52. $wiki.on("click", "[data-node=wiki-ppt-play-page]", function() {
  53. $(this).remove();
  54. play($(this).data("index"));
  55. }).find("h1,h2").each(function(k, v) {
  56. let obj = $(v)
  57. obj.hover(function(){
  58. obj.append(' <a data-node="wiki-ppt-play-page" data-index="' + k + '" href="javascript:void(0)">🍨</a>');
  59. }, function() {
  60. obj.find("[data-node=wiki-ppt-play-page]").remove();
  61. })
  62. });
  63. }
  64.  
  65. // 执行播放操作
  66. function play(show_index = 0) {
  67. // 删除WIKI除主体外全部元素
  68. $("#feedly-mini,aside,header,.nav-sidebar,.wiki-page-header,.alert-wrapper").remove();
  69. $(".content-wrapper").removeClass("content-wrapper");
  70. $(".layout-page").removeClass("page-with-contextual-sidebar right-sidebar-expanded").css("padding-left", 0);
  71.  
  72. // 删除mergeRequest除主体外全部元素,调整吸顶
  73. let mergeRequestTimer;
  74. let mergeRequestShow = () => {
  75. if ($(".file-title").length) {
  76. $(".alert-wrapper,.detail-page-header,.mr-state-widget,.merge-request-tabs-holder,.mr-version-controls").remove();
  77. $(".file-title").css("top", 0);
  78. clearInterval(mergeRequestTimer)
  79. }
  80. }
  81.  
  82. // MergeRequest调整
  83. if (location.href.search("/merge_requests/") > 0) {
  84. if ($("#diffs-tab.active").length) {
  85. mergeRequestShow()
  86. } else {
  87. $("#diffs-tab a").trigger("click")
  88. mergeRequestTimer = setInterval(mergeRequestShow, 200)
  89. }
  90. }
  91.  
  92. // 删除ISSUE讨论区,并全屏
  93. if (location.href.search("/issues/") > 0) {
  94. let $style = $('<style type="text/css">.issue-sticky-header{ display:none }</style>')
  95. $("head").append($style)
  96. $(".emoji-block,.issuable-discussion,.flash-container").remove();
  97. fullScreen()
  98. }
  99.  
  100. // 删除blob所有内容,调整样式,并全屏
  101. if (location.href.search("/-/blob/") > 0) {
  102. $(".nav-block,.info-well,.js-file-title").remove();
  103. $(".container-fluid").removeClass("container-fluid");
  104. $(".file-content").css("padding", "8px");
  105. fullScreen()
  106. }
  107.  
  108. if ($wiki.length) {
  109. // 整理数据,并清空现有WIKI数据结构
  110. if (!wikis.length) {
  111. $wiki.children().each(function(k, v){
  112. let index = wikis.length - 1;
  113. let newIndex = false;
  114. if (index < 0) {
  115. index = 0;
  116. newIndex = true;
  117. }
  118. if (v.tagName === "H2") {
  119. if (typeof wikis[index] !== "undefined") {
  120. index++;
  121. }
  122. newIndex = true;
  123. }
  124.  
  125. if (newIndex) {
  126. wikis[index] = [];
  127. }
  128.  
  129. wikis[index].push(v)
  130. })
  131. }
  132.  
  133. // 开始播放
  134. $wiki.empty();
  135. show(show_index)
  136. fullScreen()
  137. }
  138.  
  139. // 绑定按键
  140. $("body").keydown(function(e){
  141. switch (e.which) {
  142. case 37 :
  143. prev();
  144. break;
  145. case 39 :
  146. next();
  147. break;
  148. }
  149. });
  150.  
  151. }
  152.  
  153. // 下一页
  154. function next() {
  155. if (wikis.length >= current + 2) {
  156. show(current + 1);
  157. }
  158. }
  159.  
  160. // 上一页
  161. function prev() {
  162. if (current > 0) {
  163. show(current - 1);
  164. }
  165. }
  166.  
  167. // 展示内容
  168. function show(index) {
  169. console.log(wikis)
  170. current = index;
  171. $wiki.html("").hide().css({"padding-top":"30px"}).append(wikis[index])
  172. $wiki.show();
  173. $("body,html").scrollTop(0);
  174. $("h1,h2").css({
  175. "position":"fixed",
  176. "z-index":9999,
  177. "top":0,
  178. "left":0,
  179. "width":"100%",
  180. "padding":"6px 15px 8px 15px",
  181. "background-color":"black",
  182. "color":"white"
  183. });
  184. }
  185.  
  186. // 全屏
  187. function fullScreen() {
  188. let element = document.documentElement;
  189. if(element.requestFullscreen) {
  190. element.requestFullscreen();
  191. } else if(element.mozRequestFullScreen) {
  192. element.mozRequestFullScreen();
  193. } else if(element.webkitRequestFullScreen) {
  194. element.webkitRequestFullScreen();
  195. } else if (document.msExitFullscreen) {
  196. document.msExitFullscreen();
  197. }
  198. $("body").css("zoom", "2");
  199. };
  200. })()