// ==UserScript== // @name 语雀知识库列表 // @namespace http://tampermonkey.net/ // @version 0.6 // @description 快速切换语雀知识库,请将脚本内所有的 itviewer 替换为你的用户名,token 替换为你的 Access Token // @author itviewer // @match https://www.yuque.com/itviewer/* // @match https://www.yuque.com/dashboard* // @exclude https://www.yuque.com/itviewer/*/edit // @license MIT // @grant none // @downloadURL https://update.greasyfork.icu/scripts/455661/%E8%AF%AD%E9%9B%80%E7%9F%A5%E8%AF%86%E5%BA%93%E5%88%97%E8%A1%A8.user.js // @updateURL https://update.greasyfork.icu/scripts/455661/%E8%AF%AD%E9%9B%80%E7%9F%A5%E8%AF%86%E5%BA%93%E5%88%97%E8%A1%A8.meta.js // ==/UserScript== (function() { 'use strict'; // Your code here... // console.log('脚本开始'); let url = window.location.href; if(url.indexOf('www.yuque.com/dashboard')!=-1) { }else { 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 = `${item.name}`; 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 = '显示知识库列表'; button.onclick = function() { if (reposidebar.style.display == 'none') { reposidebar.style.display="inline"; button.innerHTML = '隐藏知识库列表'; } else { reposidebar.style.display="none"; button.innerHTML = '显示知识库列表'; } }; header.appendChild(button); } // 虽然默认在文档加载完后执行,但我们需要的网页内容 header 是加载完动态创建的 setTimeout(function(){ getRepos(); },1000); } // 语雀其实是单页应用,虽然切换文章时地址栏和真实切换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'); // }); })();