语雀知识库列表

快速切换语雀知识库,请将脚本内所有的 itviewer 替换为你的用户名,token 替换为你的 Access Token

目前为 2022-11-30 提交的版本。查看 最新版本

// ==UserScript==
// @name         语雀知识库列表
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  快速切换语雀知识库,请将脚本内所有的 itviewer 替换为你的用户名,token 替换为你的 Access Token
// @author       itviewer
// @match        https://www.yuque.com/itviewer/*
// @exclude      https://www.yuque.com/itviewer/*/edit
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    window.onload = () => {
        function getRepos() {
            let url = 'https://www.yuque.com/api/v2/users/itviewer/repos'
            let token = 'spILuQ8ixJcajMyZJ1g6AAwQ1kPi2Y7GBOaBBXml' // 语雀开发者 token,建议最小权限

            const xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var result = JSON.parse(xhr.responseText); // 将字符串转化为对象,然后才能获取到返回字符串中的某一个值
                    renderSidebar(result.data);
                }
            };
            xhr.open('GET', url, true); // 开启一个请求,但还没有向服务器端发起请求,执行后redayState的值变为1
            xhr.setRequestHeader('X-Auth-Token', token); // setRequestHeader()方法需要在open()和send()方法之间调用
            xhr.send(); // 向服务器端发起请求,执行后redayState的值变为2
        }
        
        function renderSidebar(repos) {
            const ul = document.createElement("ul");
            ul.setAttribute(
                "class",
                "ant-menu aside-container menu-site ant-menu-light ant-menu-root ant-menu-inline"
            );
            ul.setAttribute(
                "style",
                "padding-top: 10px;height: 100%; overflow-y: auto"
            );

            const fragment = document.createDocumentFragment();

            if (Array.isArray(repos) && repos.length) {
                repos.forEach((item)=> {
                    // console.log(item.name + ' ' + item.namespace);
                    const li = document.createElement("li");
                    li.setAttribute("class", `ant-menu-item ant-menu-item-only-child`);
                    li.setAttribute("style", "padding-left: 10px;");
                    li.innerHTML = `<a href="https://www.yuque.com/${item.namespace}" title="${item.name}" style="overflow: hidden;text-overflow: ellipsis;">${item.name}</a>`;
                    fragment.appendChild(li);
                })
            }
            ul.appendChild(fragment);

            const container = document.createElement("div");
            container.setAttribute(
                "style",
                "display: none; overflow: hidden; position: fixed; top: 0; right: 0; width: 280px; height: 100%; max-height: 100vh; z-index: 998;"
            );
            container.appendChild(ul);
            document.body.appendChild(container);

            const header = document.querySelector(".header-action");
            const container2 = document.createElement("div");
            container2.setAttribute("class", `header-action-item`);
            container2.setAttribute("style", "margin-right: 100px;");
            const button = document.createElement("button");
            button.setAttribute("class", `ant-btn ant-btn-primary`);
            button.innerHTML = '<span>显示知识库列表</span>';
            button.onclick = function() {
                if (container.style.display == 'none') {
                    container.style.display="inline";
                    button.innerHTML = '<span>隐藏知识库列表</span>';
                } else {
                    container.style.display="none";
                    button.innerHTML = '<span>显示知识库列表</span>';
                }
            };
            container2.appendChild(button);
            header.prepend(container2);
        }
        getRepos();
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址