// ==UserScript== // @name 企业微信后台权限管理优化 // @namespace https://bbs.tampermonkey.net.cn/ // @version 0.2.1 // @description 在页面上创建一个悬浮按钮,点击后显示所有的管理员姓名和他所管理的部门 // @author 肆散的尘埃i // @match http*://work.weixin.qq.com/* // @require https://scriptcat.org/lib/637/1.4.3/ajaxHooker.js // @require https://scriptcat.org/lib/2311/1.0.1/createFloatingButton.js // @grant none // ==/UserScript== (function() { 'use strict'; // 使用技术:https://bbs.tampermonkey.net.cn/forum.php?mod=viewthread&tid=3284&page=1 // 设置脚本启动时间,根据访问访问此链接的时间,适当添加 @run-at document-start ajaxHooker.hook(request => { // 角色列表 if (request.url.includes('/wework_admin/profile/role/getRoleList')) { request.response = res => { const responseText = res.response; // 注意保存原数据 // 存储 localStorage.setItem("RoleList",responseText); console.log("已获取到数据,并且存入localStorage,名称为:RoleList") }; } }); let originalTableData = []; // 保存原始数据,供重置时使用 // 创建全屏显示的 div function createFullScreenDIV(htmlString) { // 禁用页面滚动 document.body.style.overflow = 'hidden'; // 创建全屏遮罩层 const overlay = document.createElement('div'); overlay.style.cssText = ` position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; background: rgba(0, 0, 0, 0.8); z-index: 10000; display: flex; justify-content: center; align-items: center; overflow-y: auto; `; // 创建内容容器 const contentContainer = document.createElement('div'); contentContainer.style.cssText = ` max-height: 80vh; max-width: 90vw; overflow-y: auto; background-color: white; padding: 20px; border-radius: 8px; `; // 创建关闭按钮 const closeButton = document.createElement('div'); closeButton.innerText = '×'; closeButton.style.cssText = ` position: absolute; top: 10px; right: 25px; width: 30px; height: 30px; background: rgba(255, 255, 255, 0.3); color: white; text-align: center; line-height: 30px; font-size: 20px; cursor: pointer; border-radius: 50%; z-index: 10001; `; // 关闭全屏显示的函数 function closeFullScreen() { document.body.removeChild(overlay); document.body.style.overflow = ''; document.removeEventListener('keydown', handleKeyDown); // 移除键盘事件监听器 } // 处理键盘事件的函数 function handleKeyDown(event) { if (event.key === 'Escape') { closeFullScreen(); } } // 添加键盘事件监听器,监听 ESC 键 document.addEventListener('keydown', handleKeyDown); // 设置关闭按钮的点击事件 closeButton.addEventListener('click', closeFullScreen); // 将传入的 HTML 字符串添加到内容容器中 contentContainer.appendChild(htmlString); // 将内容容器和关闭按钮添加到遮罩层中 overlay.appendChild(contentContainer); overlay.appendChild(closeButton); // 将遮罩层添加到文档中 document.body.appendChild(overlay); } // 搜索函数,只处理组织权限列表搜索,并高亮匹配到的部分 function searchTable(partyAuthSearch) { const rows = document.querySelectorAll('table tr:not(:first-child)'); rows.forEach((row) => { const partyAuthCell = row.cells[2]; // 恢复默认样式 partyAuthCell.style.backgroundColor = ''; partyAuthCell.innerHTML = partyAuthCell.innerText; // 恢复文本内容,去掉可能的高亮HTML let partyAuthList = partyAuthCell.innerText; // 处理组织权限列表搜索并部分高亮 let partyAuthMatch = true; if (partyAuthSearch) { const regex = new RegExp(partyAuthSearch, 'gi'); const matches = partyAuthList.match(regex); if (matches) { partyAuthList = partyAuthList.replace(regex, (match) => { return `${match}`; }); partyAuthCell.innerHTML = partyAuthList; // 用innerHTML更新内容 partyAuthMatch = true; } else { partyAuthMatch = false; // 如果没匹配到,设置为false } } // 使用 visibility 来隐藏/显示行,但不影响表格宽度 if (partyAuthMatch) { row.style.visibility = 'visible'; // 显示行 row.style.height = 'auto'; // 恢复行高 } else { row.style.visibility = 'collapse'; // 隐藏行但保留宽度 row.style.height = '0'; // 折叠行高 } }); } // 初始化表格,并保存原始数据 function initTable(roleList) { const table = document.createElement('table'); table.style.cssText = ` width: 100%; border-collapse: collapse; background-color: white; `; const headerRow = document.createElement('tr'); const headers = ['序号', '管理员名称', '组织权限列表']; headers.forEach(headerText => { const th = document.createElement('th'); th.innerText = headerText; th.style.cssText = ` border: 1px solid #ddd; padding: 8px; text-align: center; background-color: #f2f2f2; font-weight: bold; `; headerRow.appendChild(th); }); table.appendChild(headerRow); originalTableData = []; // 清空之前的原始数据 roleList.forEach((role, index) => { // 创建一行 const tr = document.createElement('tr'); // 序号 const idTd = document.createElement('td'); idTd.innerText = index + 1; idTd.style.cssText = ` width: 5%; border: 1px solid #ddd; padding: 8px; text-align: center; `; tr.appendChild(idTd); // 管理员姓名 const adminNameTd = document.createElement('td'); if (role.admin_list.item.length > 0) { const admin_list = role.admin_list.item.map(item => `${item.name}` ).join(', '); adminNameTd.innerHTML = admin_list; } else { adminNameTd.innerText = '无管理员'; } adminNameTd.style.cssText = ` width: 10%; border: 1px solid #ddd; padding: 8px; text-align: center; `; tr.appendChild(adminNameTd); // 所管理的组织 const partyAuthTd = document.createElement('td'); const filteredPartyAuthList = role.party_auth_list.item .filter(item => item.name !== '未加入组织的成员') .map(item => item.name) .join(' , '); partyAuthTd.innerText = filteredPartyAuthList ; partyAuthTd.style.cssText = ` width: 80%; border: 1px solid #ddd; padding: 8px; text-align: left; `; tr.appendChild(partyAuthTd); originalTableData.push(tr.cloneNode(true)); // 保存每行的初始状态 table.appendChild(tr); }); return table; } function performing_function() { const roleList = JSON.parse(localStorage.getItem("RoleList")); const data = roleList.data.role_list.item; const container = document.createElement('div'); const searchContainer = document.createElement('div'); searchContainer.style.cssText = ` margin-bottom: 10px; display: flex; justify-content: flex-start; gap: 10px; margin-left: 10%; /* 确保位置与表格对齐 */ `; // 搜索栏 const partyAuthInput = document.createElement('input'); partyAuthInput.placeholder = '搜索组织权限'; partyAuthInput.style.cssText = ` width: 80%; padding: 8px; border: 1px solid #ccc; `; searchContainer.appendChild(partyAuthInput); // 搜索按钮 const searchButton = document.createElement('button'); searchButton.innerText = '搜索'; searchButton.style.cssText = ` padding: 8px 16px; background-color: #007bff; color: white; border: none; cursor: pointer; `; searchButton.addEventListener('click', () => { searchTable(partyAuthInput.value); }); searchContainer.appendChild(searchButton); // 重置按钮 const resetButton = document.createElement('button'); resetButton.innerText = '重置'; resetButton.style.cssText = ` padding: 8px 16px; background-color: #6c757d; color: white; border: none; cursor: pointer; `; resetButton.addEventListener('click', () => { // 重置表格为原始数据 const table = document.querySelector('table'); table.querySelectorAll('tr:not(:first-child)').forEach(row => { row.style.visibility = 'visible'; // 恢复所有行的显示 row.style.height = 'auto'; // 恢复高度 row.cells[2].innerHTML = row.cells[2].innerText; // 清除高亮 }); // 清空搜索框 partyAuthInput.value = ''; }); searchContainer.appendChild(resetButton); container.appendChild(searchContainer); // 创建表格 const table = initTable(data); container.appendChild(table); createFullScreenDIV(container); } // 创建悬浮按钮,设置名称并绑定点击事件 createFloatingButton(() => { performing_function(); }, "显示"); })();