自动图标
// ==UserScript==
// @name 自动图标
// @namespace http://tampermonkey.net/
// @version 0.6
// @description try to take over the world!
// @author You
// @match https://mubu.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=mubu.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
var icons = {"手机":"📱","每月":"㏽", "周末":"🏠", "联系":"📞", "安排":"🕹️", "撰写":"📝","处理":"➡️", "等待":"⌛", "考虑":"💡","审阅":"💻","组织":"⛺","完成":"📅","每":"🌱", "备忘":"📎", "上网":"🕸️", "阅读":"📖","研究":"🔬","外出":"🚗"};
var keys = "";
for(var key in icons){
keys = keys + key + "|";
}
keys = keys.substring(0,keys.length - 1);
var re = new RegExp(keys, 'g');
//更新元素下面所有bullet-dot节点
function displayIcon(item){
//查找所有bullet-dot节点
var bullets = item.querySelectorAll("div.bullet-dot");
bullets.forEach (function (bullet) {
//隐藏原来显示的圆点
bullet.style.top = "0px";
bullet.style.left = "0px";
bullet.style.width = "0px";
bullet.style.height = "0px";
//为节点添加图标
var r = bullet.parentNode.parentNode.nextElementSibling.nextElementSibling.innerText.match(re);
if (r){
//根据关键字设置图标
bullet.innerText = icons[r[0]];
}
else{
//没有关键字的设置文件夹图标
var l = bullet.parentNode.parentNode.parentNode.parentNode.parentNode.classList;
if(l.contains("collapsed")){
//没有展开的节点显示有标记的关闭的文件夹
bullet.innerText = "🗂️";
}else if(l.contains("no-children")){
//没有内容的节点显示关闭的文件夹
bullet.innerText = "📁";
}else{
//展开的节点显示打开的文件夹
bullet.innerText = "📂";
}
}
})
}
//延时等待页面准备好
function waitForReady(){
var pane = document.getElementById("js-outliner");
if(pane){
//查找到列表则更新全部列表
displayIcon(document);
//设置监视列表变化
var paneconfig = {characterData:true, attributes:true, attributeFilter:["class"], childList:true, subtree:true};
var paneobserver = new MutationObserver(function (mutationsList, observer) {
//处理所有的列表变化
for(var i = 0; i < mutationsList.length; i++){
var mutation = mutationsList[i];
console.log("开始更新:" + mutation.target + " " + mutation.type);
if(mutation.type == "characterData"){
//如果是文本变化更新上级节点
displayIcon(mutation.target.parentNode.parentNode.parentNode);
continue;
}
if(mutation.type == "childList"){
//如果是节点列表更新,则更新所有增加的节点
if(mutation.target.classList.contains("outliner-tree-content")){
mutation.addedNodes.forEach(function(item){
if (item.classList.contains("outliner-node")){
displayIcon(item);
}
})
}
}
if(mutation.type == "attributes" && mutation.attributeName == "class" && mutation.target.classList.contains("outliner-node")){
//如果是节点的class属性变化则更新图标,实现文件夹图标状态变化
displayIcon(mutation.target);
}
}
});
paneobserver.observe(pane, paneconfig);
}
else{
//没有查找到列表则继续等待
setTimeout(waitForReady, 500);
}
}
//设置延时等待
setTimeout(waitForReady, 500);
})();