语雀知识库列表

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

目前為 2022-11-30 提交的版本,檢視 最新版本

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

(function() {
    'use strict';

    // Your code here...
    // console.log('脚本开始');
    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);
                renderButton();
            }
        };
        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",
            "height: 90%; 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", "width: 100%; height: 25px; line-height: 25px;");
                li.innerHTML = `<a href="https://www.yuque.com/${item.namespace}" target="_blank" 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("id", "reposidebar");
        container.setAttribute(
            "style",
            "display: none; overflow: hidden; position: fixed; top: 50px; left: 0; width: 280px; height: 100%; max-height: 100vh; z-index: 999;"
        );
        container.appendChild(ul);
        document.body.appendChild(container);
    }

    function renderButton() {
        const reposidebar = document.querySelector("#reposidebar");

        const header = document.querySelector("#asideHead>div:first-child");
        // console.log(header);
        const button = document.createElement("button");
        button.setAttribute("class", `ant-btn ant-btn-primary`);
        button.innerHTML = '<span>显示知识库列表</span>';

        button.onclick = function() {
            if (reposidebar.style.display == 'none') {
                reposidebar.style.display="inline";
                button.innerHTML = '<span>隐藏知识库列表</span>';
            } else {
                reposidebar.style.display="none";
                button.innerHTML = '<span>显示知识库列表</span>';
            }
        };
        header.appendChild(button);
    }

    getRepos();

    // 语雀其实是单页应用,虽然切换文章时地址栏和真实切换url一样
    /*     var _wr = function(type) {
        var orig = history[type];
        return function() {
            var rv = orig.apply(this, arguments);
            var e = new Event(type);
            e.arguments = arguments;
            window.dispatchEvent(e);
            return rv;
        };
    };
    history.pushState = _wr('pushState');
    history.replaceState = _wr('replaceState'); */

    // window.addEventListener('replaceState', function(e) {});
    // window.addEventListener('pushState', function(e) {
    //      console.log('pushState');
    // });
})();

QingJ © 2025

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