Gitlab Wiki Player

try to take over the world!

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

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