sort all task in taskbar
当前为
// ==UserScript==
// @name MWI TaskManager
// @namespace http://tampermonkey.net/
// @version 0.1
// @description sort all task in taskbar
// @author shykai
// @match https://www.milkywayidle.com/*
// @match https://test.milkywayidle.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=milkywayidle.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
/* 支持修改版汉化插件 */
function getOriTextFromElement(elem) {
if (!elem) {
console.error("getTextFromElement null elem");
return "";
}
const translatedfrom = elem.getAttribute("script_translatedfrom");
if (translatedfrom) {
return translatedfrom;
}
return elem.textContent;
}
function addButton() {
const targetNode = document.querySelector("div.TasksPanel_taskSlotCount__nfhgS");
if (targetNode) {
let sortButton = targetNode.querySelector("#TaskSort");
if (!sortButton) {
sortButton = document.createElement("button");
sortButton.setAttribute("class", "Button_button__1Fe9z Button_small__3fqC7");
sortButton.id = "TaskSort";
sortButton.innerHTML = "TaskSort";
sortButton.addEventListener("click", function (evt) {
const list = document.querySelector("div.TasksPanel_taskList__2xh4k");
[...list.querySelectorAll("div.RandomTask_randomTask__3B9fA")]
.sort(function (a, b) {
var a_name = getOriTextFromElement(a.querySelector("div.RandomTask_name__1hl1b"));
var b_name = getOriTextFromElement(b.querySelector("div.RandomTask_name__1hl1b"));
if (a_name.startsWith("Defeat ") && b_name.startsWith("Defeat ")) {
var a_index = a.querySelector("span.script_taskMapIndex").textContent.replace(/\D/ig, "");
var b_index = b.querySelector("span.script_taskMapIndex").textContent.replace(/\D/ig, "");
if (a_index != b_index) {
return (Number(a_index) > Number(b_index) ? 1 : -1);
}
}
return a_name == b_name ? 0
: (a_name > b_name ? 1 : -1);
})
.forEach(node => list.appendChild(node));
});
targetNode.appendChild(sortButton);
}
}
}
const config = { attributes: true, childList: true, subtree: true };
const observer = new MutationObserver(function(mutationsList, observer) {
addButton();
});
observer.observe(document, config);
})();