Gitlab Wiki Player

Play Gitlab wiki like PPT!

目前為 2020-07-12 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Gitlab Wiki Player
  3. // @name:zh-cn Gitlab WIKI 播放器
  4. // @namespace http://chengxuan.li
  5. // @version 12.9.2
  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)"><span class="fa fa-play"></span></a>');
  40. // MergeRequest、ISSUE:插入播放按钮
  41. $(".detail-page-header-actions a.btn:first").before('<a id="wiki-ppt-play" class="btn pull-left" href="javascript:void(0)"><span class="fa fa-play"></span></a>');
  42. // Blob:插入播放按钮
  43. $(".tree-controls").prepend('<a id="wiki-ppt-play" class="btn" href="javascript:void(0)"><span class="fa fa-play"></span></a>');
  44.  
  45. // 绑定PPT按钮播放事件
  46. $("#wiki-ppt-play").click(function() {
  47. play();
  48. });
  49.  
  50. // 绑定H1/H2标题播放按钮
  51. $wiki.on("click", "[data-node=wiki-ppt-play-page]", function() {
  52. $(this).remove();
  53. play($(this).data("index"));
  54. }).find("h1,h2").each(function(k, v) {
  55. let obj = $(v)
  56. obj.hover(function(){
  57. obj.append(' <a data-node="wiki-ppt-play-page" data-index="' + k + '" href="javascript:void(0)"><span class="fa fa-play"></span></a>');
  58. }, function() {
  59. obj.find("[data-node=wiki-ppt-play-page]").remove();
  60. })
  61. });
  62. }
  63.  
  64. // 执行播放操作
  65. function play(show_index = 0) {
  66. // 删除WIKI除主体外全部元素
  67. $("#feedly-mini,aside,header,.nav-sidebar,.wiki-page-header,.alert-wrapper").remove();
  68. $(".content-wrapper").removeClass("content-wrapper");
  69. $(".layout-page").removeClass("page-with-contextual-sidebar right-sidebar-expanded").css("padding-left", 0);
  70.  
  71. // 删除mergeRequest除主体外全部元素,调整吸顶
  72. let mergeRequestTimer;
  73. let mergeRequestShow = () => {
  74. if ($(".file-title").length) {
  75. $(".alert-wrapper,.detail-page-header,.mr-state-widget,.merge-request-tabs-holder,.mr-version-controls").remove();
  76. $(".file-title").css("top", 0);
  77. clearInterval(mergeRequestTimer)
  78. }
  79. }
  80.  
  81. // MergeRequest调整
  82. if (location.href.search("/merge_requests/") > 0) {
  83. if ($("#diffs-tab.active").length) {
  84. mergeRequestShow()
  85. } else {
  86. $("#diffs-tab a").trigger("click")
  87. mergeRequestTimer = setInterval(mergeRequestShow, 200)
  88. }
  89. }
  90.  
  91. // 删除ISSUE讨论区,并全屏
  92. if (location.href.search("/issues/") > 0) {
  93. $(".emoji-block,.issuable-discussion").remove();
  94. fullScreen()
  95. }
  96.  
  97. // 删除blob所有内容,调整样式,并全屏
  98. if (location.href.search("/-/blob/") > 0) {
  99. $(".nav-block,.info-well,.js-file-title").remove();
  100. $(".container-fluid").removeClass("container-fluid");
  101. $(".file-content").css("padding", "8px");
  102. fullScreen()
  103. }
  104.  
  105. if ($wiki.length) {
  106. // 整理数据,并清空现有WIKI数据结构
  107. if (!wikis.length) {
  108. $wiki.children().each(function(k, v){
  109. let index = wikis.length - 1;
  110. let newIndex = false;
  111. if (index < 0) {
  112. index = 0;
  113. newIndex = true;
  114. }
  115. if (v.tagName === "H2") {
  116. if (typeof wikis[index] !== "undefined") {
  117. index++;
  118. }
  119. newIndex = true;
  120. }
  121.  
  122. if (newIndex) {
  123. wikis[index] = [];
  124. }
  125.  
  126. wikis[index].push(v)
  127. })
  128. }
  129.  
  130. // 开始播放
  131. $wiki.empty();
  132. show(show_index)
  133. fullScreen()
  134. }
  135.  
  136. // 绑定按键
  137. $("body").keydown(function(e){
  138. switch (e.which) {
  139. case 37 :
  140. prev();
  141. break;
  142. case 39 :
  143. next();
  144. break;
  145. }
  146. });
  147.  
  148. }
  149.  
  150. // 下一页
  151. function next() {
  152. if (wikis.length >= current + 2) {
  153. show(current + 1);
  154. }
  155. }
  156.  
  157. // 上一页
  158. function prev() {
  159. if (current > 0) {
  160. show(current - 1);
  161. }
  162. }
  163.  
  164. // 展示内容
  165. function show(index) {
  166. current = index;
  167. $wiki.html("").hide().css({"padding-top":"30px"}).append(wikis[index]).fadeIn();
  168. $("body,html").scrollTop(0);
  169. $("h1,h2").css({
  170. "position":"fixed",
  171. "z-index":9999,
  172. "top":0,
  173. "left":0,
  174. "width":"100%",
  175. "padding":"6px 15px 8px 15px",
  176. "background-color":"black",
  177. "color":"white"
  178. });
  179. }
  180.  
  181. // 全屏
  182. function fullScreen() {
  183. let element = document.documentElement;
  184. if(element.requestFullscreen) {
  185. element.requestFullscreen();
  186. } else if(element.mozRequestFullScreen) {
  187. element.mozRequestFullScreen();
  188. } else if(element.webkitRequestFullScreen) {
  189. element.webkitRequestFullScreen();
  190. } else if (document.msExitFullscreen) {
  191. document.msExitFullscreen();
  192. }
  193. $("body").css("zoom", "2");
  194. };
  195. })()