KJL后台-编缉器部件栏添加关闭所有干涉
// ==UserScript==
// @name KJL后台-编缉器部件栏添加关闭所有干涉
// @namespace Violentmonkey Scripts
// @match https://www.kujiale.com/vc/modeleditor/new*
// @grant none
// @version 1.0
// @author hejie13250
// @description 2024/11/28 08:13:03
// ==/UserScript==
(function() {
'use strict';
function createToggleButton(targetElement) {
// 检查按钮是否已经存在,以避免重复添加
if (targetElement.nextElementSibling && targetElement.nextElementSibling.classList.contains('ikKMNI')) {
return;
}
// 创建按钮
var button = document.createElement('button');
button.setAttribute('type', 'button');
button.classList.add('ikKMNI');
button.style.marginLeft= '5px';
button.style.marginLeft= '10px';
button.style.fontSize = '12px';
button.style.cursor = 'pointer'; // 设置光标为手形
button.textContent = '关闭所有干涉';
button.addEventListener('mouseover', function() {
this.style.color = '#fd00fd';
this.style.setProperty('color', '#fd00fd', 'important');
});
button.addEventListener('mouseout', function() {
this.style.removeProperty('color');
});
// 按钮点击事件处理函数
button.addEventListener('click', async function() {
var items = document.querySelectorAll('ul.tui-tree-new:nth-child(2) > li > span:nth-child(2) > span:nth-child(2) > div:nth-child(1) > div:nth-child(1) > span:nth-child(1) > span:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2)');
for (let index = 0; index < items.length; index++) {
const item = items[index];
if (item) {
item.click();
console.log('点击第 ' + (index + 1) + ' 项,共 ' + items.length + ' 项');
// 等待 specificElement1 出现
const specificElement1 = await waitElement('.element-attr-groups > li:nth-child(11) > li:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > ul:nth-child(1) > li:nth-child(10) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1)', 500);
if (specificElement1) {
specificElement1.click();
console.log('点击 => 忽略内部干涉', specificElement1);
// 等待 specificElement2 出现
const specificElement2 = await waitElement('.select-input-options > li.option-item:nth-child(2)', 500);
if (specificElement2) {
specificElement2.click();
console.log('选择 => 是', specificElement2);
}
}
}
}
});
function waitElement(selector, timeout) {
return new Promise(function(resolve) {
const startTime = Date.now();
const interval = setInterval(function() {
const element = document.querySelector(selector);
if (element || (Date.now() - startTime > timeout)) {
clearInterval(interval);
resolve(element);
}
}, 100);
});
}
// 在目标元素后插入切换按钮
targetElement.parentNode.insertBefore(button, targetElement.nextSibling);
}
// MutationObserver配置
var observer = new MutationObserver(function(mutationsList) {
mutationsList.forEach(function(mutation) {
if (mutation.type === 'childList') {
// 检查目标元素是否出现或变化
var targetElement = document.querySelector('.tui-tabs-bar-tab-content-flex');
if (targetElement) {
// 在目标元素旁边创建切换按钮
createToggleButton(targetElement);
}
}
});
});
// 观察器选项(监控子节点列表变化和子树变化)
var config = { childList: true, subtree: true };
// 开始监视body的变化
observer.observe(document.body, config);
})();