Gitlab Wiki Player

Play Gitlab wiki like PPT!

当前为 2018-08-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Gitlab Wiki Player
  3. // @name:zh-cn Gitlab WIKI 播放器
  4. // @namespace http://chengxuan.li
  5. // @version 0.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. // @grant none
  11. // ==/UserScript==
  12. (function() {
  13. 'use strict';
  14.  
  15. // WIKI每页数据
  16. let wikis = [];
  17.  
  18. // 当前看的是第几页(从0开始计算)
  19. let current = -1;
  20.  
  21. // 检测是否可以执行
  22. if (typeof jQuery !== "undefined" && $("header.navbar-gitlab").length) {
  23. main();
  24. }
  25.  
  26. // 主入口
  27. function main() {
  28. // 插入播放按钮
  29. $(".nav-controls").prepend('<a id="wiki-ppt-play" class="btn" href="javascript:void(0)"><span class="fa fa-play"></span></a>');
  30.  
  31. // 绑定PPT按钮播放事件
  32. $("#wiki-ppt-play").click(play);
  33. }
  34.  
  35. // 执行播放操作
  36. function play() {
  37. // 删除除主体外全部元素
  38. $("#feedly-mini,aside,header,.nav-sidebar,.wiki-page-header,.alert-wrapper").remove();
  39. $(".content-wrapper").removeClass("content-wrapper");
  40. $(".layout-page").removeClass("page-with-contextual-sidebar right-sidebar-expanded");
  41.  
  42. // 整理数据,并清空现有WIKI数据结构
  43. if (!wikis.length) {
  44. $(".wiki").children().each(function(k, v){
  45. let index = wikis.length - 1;
  46. let newIndex = false;
  47. if (index < 0) {
  48. index = 0;
  49. newIndex = true;
  50. }
  51. if (v.tagName === "H2") {
  52. index++;
  53. newIndex = true;
  54. }
  55.  
  56. if (newIndex) {
  57. wikis[index] = [];
  58. }
  59.  
  60. wikis[index].push(v)
  61. })
  62. }
  63.  
  64. // 开始播放
  65. current = -1;
  66. $(".wiki").html("");
  67. next();
  68. $("body").css("zoom", "2.5")
  69.  
  70. // 绑定按键
  71. $("body").keydown(function(e){
  72. switch (e.which) {
  73. case 37 :
  74. prev();
  75. break;
  76. case 39 :
  77. next();
  78. break;
  79. }
  80. });
  81.  
  82. }
  83.  
  84. // 下一页
  85. function next() {
  86. if (wikis.length >= current + 2) {
  87. current++;
  88. show(current);
  89. }
  90. }
  91.  
  92. // 上一页
  93. function prev() {
  94. if (current > 0) {
  95. current--;
  96. show(current);
  97. }
  98. }
  99.  
  100. // 展示内容
  101. function show(index) {
  102. $(".wiki").html("").hide().append(wikis[index]).fadeIn();
  103. $("body,html").scrollTop(0);
  104. }
  105.  
  106. })()