首页  编辑  

油猴脚本: 查询SharePoint用户

Tags: /计算机文档/软件应用技巧/   Date Created:
在使用SharePoint.com的过程当中,我们常常需要查询某些用户的具体信息,如查看用户名对应的邮箱,方便去公司内部查看对应的人员。因为有重名的用户,光从用户名是无法看出来对应哪个用户的。此外如何想查看某个用户所有的文章,也是很麻烦的事情,本文的插件可以实现相关的功能。

如果要查看详细的SharePoint信息,可以使用 SP Insider 插件:。

利用下面的脚本,可以方便查询对应用户的详细信息:
// ==UserScript==
// @name         SharePoint 用户查询
// @namespace    http://tampermonkey.net/
// @version      2025-01-23
// @license      GPL-3.0-only
// @description  输入关键字查询SharePoint用户详细信息和查看该用户所有文章,方便确认作者身份。按 Ctrl+Q 激活功能。
// @author       Kingron
// @match        https://*.sharepoint.com/sites/*
// @match        https://*.sharepoint.cn/sites/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=sharepoint.com
// @run-at       document-start
// @grant        unsafeWindow
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    // 监听 Ctrl+Q 快捷键
    window.addEventListener('keydown', function(event) {
        if (event.ctrlKey && event.key === 'q') {
            event.preventDefault(); // 阻止默认行为

            const keyword = prompt("请输入关键字进行搜索:");
            if (keyword) {
                const siteName = getSiteNameFromURL(); // 获取当前 SharePoint 站点名称
                console.log("SharePoint站点名称: ", siteName);
                if (siteName) {
                    searchUsers(siteName, keyword);
                } else {
                    alert("无法从 URL 获取 SharePoint 站点名!");
                }
            }
        }
    });

    // 从当前 URL 中提取 SharePoint 站点名称,支持 .com 和 .cn 域名
    function getSiteNameFromURL() {
        // 优先匹配 sharepoint.com/sites/xxx/bbb/.../SitePages 或 sharepoint.cn/sites/xxx/bbb/.../SitePages
        let regex = /https:\/\/.*\.sharepoint\.(com|cn)\/sites\/(.*)\/(SitePages|_layouts|Lists|Shared%20Documents|SiteAssets|Pages)/;
        let match = window.location.href.match(regex);

        // 如果能匹配到 SitePages 结构,则返回匹配到的 xxx/bbb/... 部分
        if (match) {
            return match[2]; // match[2] 为 sites/后面到 /SitePages 之前的所有内容
        }

        regex = /https:\/\/.*\.sharepoint\.(com|cn)\/sites\/([^\/?]+)/;
        match = window.location.href.match(regex);
        return match ? match[2] : null;
    }

    function copyTable(tableElement) {
        const range = document.createRange();
        const selection = window.getSelection();
        selection.removeAllRanges();
        range.selectNode(tableElement);
        selection.addRange(range);
        try {
            document.execCommand('copy');
            alert('表格已复制到剪切板!');
        } catch (err) {
            alert('复制失败:' + err);
        }
        selection.removeAllRanges();
    }

    // 调用 SharePoint API 获取用户数据
    function searchUsers(siteName, keyword) {
        const apiUrl = `/sites/${siteName}/_api/Web/SiteUsers`;
        const headers = {
            "Accept": "application/json"
        };

        GM_xmlhttpRequest({
            method: "GET",
            url: apiUrl,
            headers: headers,
            onload: function(response) {
                const data = JSON.parse(response.responseText);
                const users = data.value || [];

                // 过滤匹配关键字的用户
                const regex = new RegExp(keyword, 'i');  // 'i' 表示不区分大小写
                const filteredUsers = users.filter(user => {
                    return regex.test(user.Title) || regex.test(user.Email) || regex.test(user.UserPrincipalName);
                });

                if (filteredUsers.length === 0) {
                    alert("没有找到匹配的用户。");
                } else {
                    displayResults(siteName, filteredUsers);
                }
            },
            onerror: function(error) {
                alert("请求失败!请稍后再试。");
            }
        });
    }

    // 在页面上展示搜索结果
    function displayResults(site, users) {
        const container = document.createElement('div');

        container.innerHTML = `
        <div tabindex=0 style="position:fixed; top:50%; left:50%; transform:translate(-50%, -50%); max-width:90%; max-height:70%; overflow:hidden; background-color:#fff; border:1px solid #ccc; border-radius:8px; box-shadow:0 0 10px rgba(0, 0, 0, 0.3); padding:10px; z-index: 9999; display:flex; flex-direction:column;}">
            <div style="flex: 1; overflow-y: auto">
                <style>
                    #_rl_table td { padding: 5px; border-bottom: 1px solid #ddd; }
                </style>
                <table id="_rl_table" style="width: 100%; border-collapse: collapse; background-color: #fff; border: 1px solid #ddd; border-radius: 5px">
                    <thead style="position: sticky; top: 0; background-color: #f8f8f8;">
                        <tr>
                            <th>Title</th>
                            <th>Email</th>
                            <th>UserPrincipalName</th>
                            <th>文件</th>
                        </tr>
                    </thead>
                    <tbody>
                        ${users.map(user => `
                            <tr>
                                <td><a href="/sites/${site}/SitePages/Forms/AllPages.aspx?view=7&q=author:*&useFiltersInViewXml=0&viewpath=/sites/${site}/SitePages/Forms/AllPages.aspx&FilterField1=Editor&FilterValue1=${user.Title}&FilterField1=DocIcon&FilterValue1=aspx&FilterType1=Computed&FilterOp1=In&FilterField2=Author&FilterValue2=${user.Title}&FilterType2=User&FilterOp2=In" target="_blank">${user.Title}</a></td>
                                <td><a href="mailto:${user.Email}">${user.Email}</a></td>
                                <td><a href="${user['odata.id']}" target="_blank">${user.UserPrincipalName}</a></td>
                                <td><a href='/sites/${site}/_layouts/15/search.aspx/siteall?oobRefiners={"FileType":["pptx","docx","xlsx","one","pdf","video","html"]}&q=author:${user.Title}&scope=site' target="_blank">查询</a></td>
                            </tr>
                        `).join('')}
                    </tbody>
                </table>
            </div>

            <div style="padding-top:10px; display: flex; align-items: center; justify-content: flex-end">按 Esc 关闭&nbsp;&nbsp;
                <button id="copyButton" style="padding: 10px 10px; background-color: #3CB371; color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 14px;outline: none;">复制表格</button>&nbsp;
                <button id="closeButton" style="padding: 10px 10px; background-color: #ff4d4f; color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 14px;outline: none;">关闭</button>
            </div>
        </div>
        `;
        document.body.appendChild(container);

        const copyButton = container.querySelector('#copyButton');
        copyButton.addEventListener('click', function() {
            copyTable(document.getElementById('_rl_table'));
        });

        const closeButton = container.querySelector('#closeButton');
        closeButton.addEventListener('click', function() {
            document.body.removeChild(container);
        });
        container.addEventListener('keydown', function(event) {
            if (event.key === 'Escape') { document.body.removeChild(container); };
        });
        closeButton.focus();
    }

    const authorRegex = /"_AuthorByline":\s?"([^"]+)"/;
    const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g;
    let authorByline;
    var observer = new MutationObserver(function(mutationsList) {
        for (var mutation of mutationsList) {
            if (mutation.type === 'childList') {
                for (var node of mutation.addedNodes) {
                    // 检查是否是 <script> 元素
                    if (node.tagName === 'SCRIPT') {
                        if (node.textContent.includes("_AuthorByline")) {
                            const match = authorRegex.exec(node.textContent);
                            if (match) {
                                authorByline = match[1];
                                console.log('提取的作者信息:', authorByline);
                            }
                        }
                    }
                }
            }
        }
    });
    // 监听整个文档树的变化
    observer.observe(document, { childList: true, subtree: true });
    window.addEventListener('load', function() {
        const div = document.querySelector('div[data-automation-id="personaDetails"] div.peopleName');
        if (div && authorByline) {
            const emailMatches = authorByline.match(emailRegex);
            const firstEmail = emailMatches ? emailMatches[0] : authorByline;
            div.innerText += "<" + firstEmail + ">";
        }
    });
})();

使用方法:
打开 sharepoint.com 或 sharepoint.cn 对应的网址,然后 按 Ctrl+Q,输入关键字,即可查询。点击用户名,可以打开新窗口,查看该用户创建的所有文章。

运行效果: