KJL后台-编缉器属性项添加锁定值
// ==UserScript==
// @name KJL后台-编缉器属性项添加锁定值
// @namespace Violentmonkey Scripts
// @match https://www.kujiale.com/vc/modeleditor/new*
// @grant none
// @version 1.1
// @author hejie13250
// @description 2024/9/9 11:42:07
// ==/UserScript==
(function() {
'use strict';
// 添加样式到文档
var style = document.createElement('style');
document.head.appendChild(style);
style.type = 'text/css';
style.appendChild(document.createTextNode(`
/* 右栏 属性 下拉框 */
div.attr-select-input div.attr-select-input-dropdown {
height: auto !important;
background: #fff !important;
border: 1px solid #1a7af8;
}
div.attr-select-input div.attr-select-input-dropdown ul.select-input-options a.lock {
position: absolute;
margin-top: -26px !important;
margin-left: 205px;
}
.attr-select-input .attr-select-input-dropdown .select-input-options {
height: auto !important;
max-height:1000px !important;
}
.attr-select-input .attr-select-input-dropdown .select-input-options .option-item {
width: 190px !important;
}
.attr-select-input .attr-select-input-dropdown .select-input-options .option-item .option-label .description {
width: 30px !important;
}
`));
function addLockAfterOptionItems() {
const dropdowns = document.querySelectorAll('.attr-select-input-dropdown');
dropdowns.forEach(dropdown => {
const optionItems = dropdown.querySelectorAll('.option-item');
optionItems.forEach((item, index) => {
if (index > 0) {
let nextSibling = item.nextElementSibling;
let hasLock = false;
while (nextSibling) {
if (nextSibling.classList.contains('lock')) {
hasLock = true;
break;
}
nextSibling = nextSibling.nextElementSibling;
}
if (!hasLock) {
const lockElement = document.createElement('a');
lockElement.textContent = '锁定';
lockElement.className = 'lock';
item.insertAdjacentElement('afterend', lockElement);
lockElement.addEventListener('click', function() {
const descriptionElement = item.querySelector('.description') || item.querySelector('.name');
const descriptionContent = descriptionElement.textContent;
const inputElement = document.querySelector('.custom-input > div:nth-child(1) > div:nth-child(1) > input:nth-child(1)');
if (inputElement) {
// 触发完整事件序列
const triggerEvents = ['focus', 'input', 'change', 'blur'];
// 使用原型方法设置值
const valueSetter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set;
let finalValue;
if (descriptionContent === 'true' || descriptionContent === 'false') {
finalValue = descriptionContent;
} else {
finalValue = '1*' + descriptionContent;
}
valueSetter.call(inputElement, finalValue);
// 触发框架事件
if (inputElement.__vue__) {
inputElement.__vue__.$emit('input', '1*' + descriptionContent);
}
// 派发完整事件
triggerEvents.forEach(eventType => {
const event = new Event(eventType, {
bubbles: true,
cancelable: true
});
inputElement.dispatchEvent(event);
});
// 保持焦点控制
inputElement.focus();
// 新增确认按钮点击(加在inputElement处理完成后)
setTimeout(() => {
document.querySelectorAll('.custom-input-btn').forEach(btn => {
if (getComputedStyle(btn).display !== 'none') {
btn.dispatchEvent(new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
}));
}
});
}, 150);
}
});
}
}
});
});
}
// 使用 MutationObserver 监听 DOM 变化
const observer = new MutationObserver(function(mutationsList) {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
addLockAfterOptionItems();
}
}
});
// 开始观察整个文档的 body,监听子节点变化
observer.observe(document.body, {
childList: true,
subtree: true
});
// 在页面卸载时停止观察
window.addEventListener('unload', function() {
observer.disconnect();
});
})();