# 滑动以打开聊天

**URL:** <https://meta.discourse.org/t/swipe-pour-le-chat/411449>\
**Category:** Feature\
**Tags:** chat\
**Created:** [2026年九月2日 06:41 UTC](https://meta.discourse.org/t/swipe-pour-le-chat/411449 "2026-09-02T06:41:44Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![gilles](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gilles/32/549022_2.png) [@gilles](https://meta.discourse.org/u/gilles)\
**Post date:** [2026年九月27日 10:48 UTC](https://meta.discourse.org/t/swipe-pour-le-chat/411449/5 "2026-09-27T10:48:24Z")

</div>

我写了一个脚本，用于通过从左向右滑动来显示侧边栏，并且在聊天中，当处于对话中时，通过滑动来显示列表。目的是改善用户体验。请将此脚本复制到分配给您主题的 common\head 组件中。之后，我认为仍然有改进的空间

```plaintext
<script>
  (function() {
    if (window.innerWidth > 768) return;

    let startX = 0;
    let startY = 0;

    // 使用 'window' 以确保在聊天之前捕获手势
    window.addEventListener("touchstart", function(e) {
      if (e.touches.length === 1) {
        startX = e.touches[0].clientX;
        startY = e.touches[0].clientY;
      }
    }, { passive: true, capture: true });

    window.addEventListener("touchend", function(e) {
      if (!startX) return;

      const touch = e.changedTouches[0];
      const deltaX = touch.clientX - startX;
      const deltaY = touch.clientY - startY;

      if (startX >= 10 && startX <= 120 && deltaX > 50 && Math.abs(deltaY) < 50) {
        
        // 1. 我们是否在聊天频道中？
        // 检查 URL 是否包含 Discourse 聊天频道的典型关键字
        const currentUrl = window.location.pathname;
        const isInChatChannel = currentUrl.includes('/chat/c/') || currentUrl.includes('/chat/channel/');
        
        if (isInChatChannel) {
          // 首先尝试点击返回链接（如果它是标准化的）
          const chatBackBtn = document.querySelector('.chat-channel-header a, a[href="/chat"]');
          if (chatBackBtn) {
            chatBackBtn.click();
          } else {
            // 万无一失的方法：要求浏览器后退一页
            window.history.back();
          }
          startX = 0;
          return; // 在此处停止脚本
        }

        // 2. 如果我们位于论坛的其他地方（打开菜单）
        const sidebarSelectors = [
          ".btn-sidebar-toggle",
          ".sidebar-toggle",
          "button.header-dropdown-toggle.hamburger-dropdown",
          "#toggle-hamburger-menu"
        ];

        for (const selector of sidebarSelectors) {
          const btn = document.querySelector(selector);
          if (btn && btn.getBoundingClientRect().width > 0) {
            btn.click();
            break; 
          }
        }
      }

      startX = 0;
      startY = 0;
    }, { passive: true, capture: true });
  })();
</script>

```

---

_[View the full topic](https://meta.discourse.org/t/swipe-pour-le-chat/411449)._
