Discourse Pro

增强 Discourse 论坛。

目前为 2024-12-17 提交的版本,查看 最新版本

  1. /******/ (() => { // webpackBootstrap
  2. /******/ "use strict";
  3. var __webpack_exports__ = {};
  4.  
  5. ;// CONCATENATED MODULE: ./utils/src/gm/Store.ts
  6. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  7. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  8. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
  9. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  10. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
  11. function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
  12. /**
  13. * 存储
  14. */
  15. var Store = /*#__PURE__*/function () {
  16. function Store() {
  17. _classCallCheck(this, Store);
  18. }
  19. return _createClass(Store, null, [{
  20. key: "get",
  21. value:
  22. /**
  23. * 获取
  24. * @param key 键
  25. */
  26. function get(key) {
  27. return GM_getValue(key);
  28. }
  29.  
  30. /**
  31. * 设置
  32. * @param key 键
  33. * @param value 值
  34. */
  35. }, {
  36. key: "set",
  37. value: function set(key, value) {
  38. GM_setValue(key, value);
  39. }
  40. }]);
  41. }();
  42.  
  43. ;// CONCATENATED MODULE: ./discourse-pro/src/module/dragBar.ts
  44.  
  45. function loadDragBar(options) {
  46. var mainOutletWrapper = options.mainOutletWrapper,
  47. sidebarWrapper = options.sidebarWrapper,
  48. sidebar = options.sidebar,
  49. headerSidebarToggleBtn = options.headerSidebarToggleBtn,
  50. sidebarWidthKey = options.sidebarWidthKey,
  51. minSidebarWidth = options.minSidebarWidth,
  52. maxSidebarWidth = options.maxSidebarWidth;
  53. var $mainOutletWrapper = $(mainOutletWrapper),
  54. $sidebarWrapper = $(sidebarWrapper),
  55. $sidebar = $(sidebar),
  56. $headerSidebarToggleBtn = $(headerSidebarToggleBtn);
  57.  
  58. // 侧边栏是否存在
  59. var sidebarExist = $sidebar.length > 0;
  60. if (sidebarExist) {
  61. debugger;
  62. // 读取存储的侧边栏宽度
  63. var storeSidebarWidth = Store.get(sidebarWidthKey);
  64. if (storeSidebarWidth) {
  65. $mainOutletWrapper.css('grid-template-columns', "".concat(storeSidebarWidth, "px minmax(0, 1fr)"));
  66. }
  67. }
  68.  
  69. // 在侧边栏内部追加一个拖拽条
  70. $sidebarWrapper.append("\n <div class=\"drag-bar\" style=\"width: 4px; cursor: ew-resize\"></div>\n ");
  71.  
  72. // 拖拽条
  73. var $dragBar = $sidebarWrapper.find('.drag-bar');
  74. // 是否正在拖拽
  75. var isDragging = false;
  76. // 鼠标按下时的 clientX
  77. var startClientX = 0;
  78. // 鼠标按下时的侧边栏宽度
  79. var startSidebarWidth = 0;
  80. // 侧边栏新宽度
  81. var newSidebarWidth = 0;
  82.  
  83. // 鼠标按下事件
  84. $dragBar.on('mousedown', function (e) {
  85. startClientX = e.clientX;
  86. startSidebarWidth = $sidebarWrapper.width() || 0;
  87. isDragging = true;
  88. // 改变鼠标样式
  89. document.body.style.cursor = 'ew-resize';
  90. // 设置拖拽条背景色
  91. $dragBar.css('background-color', '#e6e6e6');
  92. // 防止文本被选中
  93. e.preventDefault();
  94. });
  95.  
  96. // 鼠标移动事件
  97. $(document).on('mousemove', function (e) {
  98. if (!isDragging) return;
  99.  
  100. // 计算新的宽度
  101. var deltaX = e.clientX - startClientX;
  102. newSidebarWidth = Math.min(maxSidebarWidth, Math.max(minSidebarWidth, startSidebarWidth + deltaX));
  103. $mainOutletWrapper.css('grid-template-columns', "".concat(newSidebarWidth, "px minmax(0, 1fr)"));
  104. });
  105.  
  106. // 鼠标松开事件
  107. $(document).on('mouseup', function () {
  108. if (!isDragging) return;
  109. isDragging = false;
  110. // 恢复鼠标样式
  111. document.body.style.cursor = 'default';
  112. // 恢复拖拽条背景色
  113. $dragBar.css('background-color', 'transparent');
  114. // 记忆侧边栏宽度
  115. Store.set(sidebarWidthKey, newSidebarWidth);
  116. });
  117.  
  118. // 展开收起侧边栏按钮点击事件
  119. $headerSidebarToggleBtn.on('click', function () {
  120. sidebarExist = !sidebarExist;
  121. $mainOutletWrapper.css('grid-template-columns', "".concat(sidebarExist ? Store.get(sidebarWidthKey) + 'px' : '0', " minmax(0, 1fr)"));
  122. });
  123. }
  124. ;// CONCATENATED MODULE: ./discourse-pro/src/main.ts
  125. // ==UserScript==
  126. // @name Discourse Pro
  127. // @namespace http://tampermonkey.net/
  128. // @version 0.0.1
  129. // @description 增强 Discourse 论坛。
  130. // @author duanluan
  131. // @copyright 2024, duanluan (https://github.com/duanluan)
  132. // @license Apache-2.0 https://www.apache.org/licenses/LICENSE-2.0.txt
  133. // @homepage https://greasyfork.org/zh-CN/scripts/520817
  134. // @supportURL https://github.com/duanluan/tampermonkey-scripts/issues
  135. // @match *://linux.do/*
  136. // @match *://meta.appinn.net/*
  137. // @require https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.slim.min.js
  138. // @grant GM_getValue
  139. // @grant GM_setValue
  140. // ==/UserScript==
  141.  
  142. // ==OpenUserJS==
  143. // @author duanluan
  144. // @updateURL https://raw.kkgithub.com/duanluan/tampermonkey-scripts/main/discourse-pro/dist/discourse-pro.user.js
  145. // ==/OpenUserJS==
  146.  
  147.  
  148. (function (_$) {
  149. 'use strict';
  150.  
  151. // 判断是否为 Discourse
  152. var generator = (_$ = $('meta[name="generator"]')) === null || _$ === void 0 ? void 0 : _$.attr('content');
  153. if (!generator || generator.indexOf('Discourse') == -1) return;
  154. var selector = {
  155. // 侧边栏展开收起按钮
  156. headerSidebarToggleBtn: '.header-sidebar-toggle>button',
  157. // 侧边栏和主内容的父容器
  158. mainOutletWrapper: '#main-outlet-wrapper',
  159. // 侧边栏
  160. sidebarWrapper: '.sidebar-wrapper',
  161. sidebar: '#d-sidebar',
  162. // 主内容
  163. mainOutlet: '#main-outlet'
  164. };
  165. var storeKeys = {
  166. // 侧边栏宽度
  167. sidebarWidth: 'sidebarWidth_'
  168. };
  169.  
  170. // 加载拖拽条
  171. loadDragBar({
  172. mainOutletWrapper: selector.mainOutletWrapper,
  173. sidebarWrapper: selector.sidebarWrapper,
  174. sidebar: selector.sidebar,
  175. headerSidebarToggleBtn: selector.headerSidebarToggleBtn,
  176. sidebarWidthKey: storeKeys.sidebarWidth + location.host,
  177. minSidebarWidth: 180,
  178. maxSidebarWidth: 500
  179. });
  180. })();
  181. /******/ })()
  182. ;
  183. //# sourceMappingURL=discourse-pro.user.js.map