Gitlab Wiki Player

Play Gitlab wiki like PPT!

当前为 2020-07-03 提交的版本,查看 最新版本

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