(async () => {
const actionStoragePath = '/data/storage/bazaar_filter.json';
const templateSelector = `[data-name="bazaar"]:has([data-type="myPlugin"]:not(.b3-button--outline)):has([data-type="downloaded"].item--focus)`;
const templateStyle = (actionStyle) => `
.b3-select_ld1731574996052 {display: none;}
${templateSelector} {
.b3-select_ld1731574996052 {display: block;}
${actionStyle || ''}
}
`;
const allCardSelector = '.b3-card';
const allStyle = templateStyle();
const onCardSelector = `.b3-card:has(.b3-switch[data-type="plugin-enable"][type="checkbox"]:not(:checked))`;
const onStyle = templateStyle(`
${onCardSelector} {
display: none;
}
`);
const offCardSelector = `.b3-card:has(.b3-switch[data-type="plugin-enable"][type="checkbox"]:checked)`;
const offStyle = templateStyle(`
${offCardSelector} {
display: none;
}
`);
watchForSetting('dialog-setting', (setting) => {
watchForEnableBtn('.config__tab-container[data-name="bazaar"]', '[data-type="plugins-enable"]', async (enableBtn) => {
const lastSpace = Array.from(enableBtn.parentElement.querySelectorAll('.fn__space')) ?.pop();
if (!lastSpace) return;
let countEl = enableBtn.nextElementSibling;
if (!countEl ?.classList ?.contains('counter')) countEl = null;
const select = document.createElement('select');
select.className = 'b3-select fn__flex-center b3-select_ld1731574996052';
select.style.marginTop = '-4px';
const options = [
{ value: 'all', text: '全部' },
{ value: 'on', text: '开启' },
{ value: 'off', text: '关闭' }
];
options.forEach(option => {
const opt = document.createElement('option');
opt.value = option.value;
opt.textContent = option.text;
select.appendChild(opt);
});
lastSpace.parentNode.insertBefore(select, lastSpace.nextSibling);
select.addEventListener('change', function () {
const selectedValue = this.value;
switch (selectedValue) {
case 'all':
manageStyle('update', allStyle);
saveActionState('all');
updateCount('all', countEl, setting);
break;
case 'on':
manageStyle('update', onStyle);
saveActionState('on');
updateCount('on', countEl, setting);
break;
case 'off':
manageStyle('update', offStyle);
saveActionState('off');
updateCount('off', countEl, setting);
break;
default:
manageStyle('update', allStyle);
saveActionState('all');
updateCount('all', countEl, setting);
}
});
const lastAction = await getActionState();
select.value = lastAction;
const styleAction = getStyleAction(lastAction);
manageStyle(styleAction.action, styleAction.cssText);
const stopObserve = observeCounterChange(countEl, () => {
stopObserve();
updateCount(lastAction, countEl, setting);
});
});
});
function updateCount(action, countEl, setting) {
if (!countEl) return;
if (action === 'all') {
countEl.textContent = setting.querySelectorAll(templateSelector + ' ' + allCardSelector).length;
}
if (action === 'on') {
countEl.textContent = setting.querySelectorAll(templateSelector + ' ' + onCardSelector).length;
}
if (action === 'off') {
countEl.textContent = setting.querySelectorAll(templateSelector + ' ' + offCardSelector).length;
}
}
function getStyleAction(action) {
if (action === 'all') {
return { action: 'add', cssText: allStyle };
}
if (action === 'on') {
return { action: 'add', cssText: onStyle };
}
if (action === 'off') {
return { action: 'add', cssText: offStyle };
}
}
async function saveActionState(action) {
try {
putFile(actionStoragePath, JSON.stringify({ action: action }))
} catch (e) {
console.log(e);
}
}
async function getActionState() {
try {
let lastAction = await getFile(actionStoragePath);
if (!lastAction) return 'all';
lastAction = JSON.parse(lastAction || '{}');
if (!lastAction || !lastAction.action) return 'all';
return lastAction.action;
} catch (e) {
console.log(e);
return 'all';
}
}
function manageStyle(action, cssText) {
const styleId = 'ld246_article_1731574996052';
let styleTag = document.getElementById(styleId);
if (action === 'add' || action === 'update') {
if (styleTag) {
styleTag.innerHTML = cssText;
} else {
styleTag = document.createElement('style');
styleTag.id = styleId;
styleTag.type = 'text/css';
styleTag.innerHTML = cssText;
document.head.appendChild(styleTag);
}
} else if (action === 'delete') {
if (styleTag) {
document.head.removeChild(styleTag);
}
}
}
function watchForSetting(key, callback) {
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
for (const node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE && node.getAttribute('data-key') === key) {
callback(node);
}
}
}
}
});
const config = {
childList: true,
subtree: true
};
const targetNode = document.body;
observer.observe(targetNode, config);
return () => {
observer.disconnect();
};
}
function watchForEnableBtn(containerSelector, targetSelector, callback) {
const container = document.querySelector(containerSelector);
if (!container) {
console.error('Container not found:', containerSelector);
return;
}
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
for (const node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
const element = node.querySelector(targetSelector);
if (element) {
callback(element);
}
}
}
}
}
});
const config = {
childList: true,
subtree: true
};
observer.observe(container, config);
return () => {
observer.disconnect();
};
}
function observeCounterChange(targetNode, callback) {
if (!targetNode) {
console.error(`Element with targetNode "${targetNode}" not found.`);
return;
}
const config = {
childList: true,
subtree: true,
characterData: true,
characterDataOldValue: true
};
const observerCallback = function (mutationsList, observer) {
for (let mutation of mutationsList) {
if (mutation.type === 'characterData' || mutation.type === 'childList') {
if (targetNode.innerText !== mutation.oldValue) {
callback(targetNode.innerText, mutation.oldValue);
}
}
}
};
const observer = new MutationObserver(observerCallback);
observer.observe(targetNode, config);
return () => {
observer.disconnect();
};
}
function putFile(storagePath, data) {
const formData = new FormData();
formData.append("path", storagePath);
formData.append("file", new Blob([data]));
return fetch("/api/file/putFile", {
method: "POST",
body: formData,
}).then((response) => {
if (response.ok) {
}
else {
throw new Error("Failed to save file");
}
}).catch((error) => {
console.error(error);
});
}
async function getFile(path) {
return fetch("/api/file/getFile", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
path,
}),
}).then((response) => {
if (response.ok) {
return response.text();
} else {
throw new Error("Failed to get file content");
}
}).catch((error) => {
console.error(error);
});
}
})();