WhatsApp Collapse

Collapse the contacts sidebar when you click the header.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         WhatsApp Collapse
// @namespace    alexchandel
// @version      1.1
// @description  Collapse the contacts sidebar when you click the header.
// @author       Alex Chandel
// @match        https://web.whatsapp.com/
// @grant        none
// ==/UserScript==
/* jshint esversion: 6 */

window.addEventListener('load', function() {
    'use strict';
    const toggleContacts = function (e) {
        // main header, side header, or side header div.
        const side = document.getElementById('side').parentElement
        if (e.target.tagName === 'HEADER' && (e.target.parentElement.id === 'main' || e.target.parentElement.id === 'side')
            || e.target.parentElement.tagName === 'HEADER' && e.target.parentElement.parentElement.id === 'side'
            || document.getElementById('main') == null && side.nextElementSibling.contains(e.target)) {
            if (side.style.display !== 'none') { // collapse
                side.style.display = 'none'
                side.previousElementSibling.children[0].style.display = 'none'
                const text = document.querySelector('#main > footer .selectable-text')
                if (text != null) text.focus()
            } else {
                side.style.display = ''
                side.previousElementSibling.children[0].style.display = ''
            }
        }
    }
    document.getElementById('app').addEventListener('click', toggleContacts, {'passive': true})
})