scripts/tampermonkey/csdn.net/remove-ai-ads.js

129 lines
4.4 KiB
JavaScript
Raw Normal View History

// ==UserScript==
// @name Remove Tab List Item
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Remove AI post
// @author You
// @match https://bbs.csdn.net/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=csdn.net
// @grant none
// @run-at document-end
// ==/UserScript==
class FilterConfig {
constructor() {
this.textFilters = localStorage.getItem('textFilters') ? JSON.parse(localStorage.getItem('textFilters')) : [];
this.initUI();
this.addEventListeners();
this.updateTextFiltersList();
}
initUI() {
this.configContainer = document.createElement('div');
this.configContainer.style.display = 'none';
this.configContainer.style.position = 'fixed';
this.configContainer.style.bottom = '35px';
this.configContainer.style.left = '15px';
this.configContainer.style.backgroundColor = 'white';
this.configContainer.style.padding = '10px';
this.configContainer.style.border = '1px solid black';
this.textFilterInput = document.createElement('input');
this.textFilterInput.placeholder = 'Add text to filter';
this.configContainer.appendChild(this.textFilterInput);
this.addFilterButton = document.createElement('button');
this.addFilterButton.innerText = 'Add';
this.addFilterButton.style.marginLeft = "5px";
this.addFilterButton.style.border = "solid 1px gray";
this.configContainer.appendChild(this.addFilterButton);
this.textFiltersList = document.createElement('ul');
this.configContainer.appendChild(this.textFiltersList);
document.body.appendChild(this.configContainer);
}
addEventListeners() {
this.textFilterInput.addEventListener("keyup", (event) => {
if (event.keyCode === 13) {
this.addTextFilter(this.textFilterInput.value);
this.textFilterInput.value = '';
this.filterElements();
}
});
this.addFilterButton.addEventListener('click', () => {
this.addTextFilter(this.textFilterInput.value);
this.textFilterInput.value = '';
this.filterElements();
});
}
addTextFilter(text) {
if (!text) return;
this.textFilters.push(text);
localStorage.setItem('textFilters', JSON.stringify(this.textFilters));
this.updateTextFiltersList();
}
updateTextFiltersList() {
this.textFiltersList.innerHTML = '';
this.textFilters.forEach(textFilter => {
const listItem = document.createElement('li');
listItem.innerText = textFilter;
this.textFiltersList.appendChild(listItem);
});
}
filterElements() {
const tabListItems = document.querySelectorAll('.tab-list-item');
tabListItems.forEach(tabListItem => {
const nameSpan = tabListItem.querySelector('.name');
if (nameSpan && this.textFilters.includes(nameSpan.innerText)) {
tabListItem.remove();
console.log('移除了一条帖子');
}
});
}
}
const filterConfig = new FilterConfig();
const redCircle = document.createElement('div');
redCircle.style.width = '15px';
redCircle.style.height = '15px';
redCircle.style.borderRadius = '15px';
redCircle.style.backgroundColor = 'red';
redCircle.style.position = 'fixed';
redCircle.style.bottom = '15px';
redCircle.style.left = '15px';
redCircle.style.cursor = 'pointer';
document.body.appendChild(redCircle);
redCircle.addEventListener('click', () => {
filterConfig.configContainer.style.display =
filterConfig.configContainer.style.display === 'none' ? 'block' : 'none';
});
(() => {
filterConfig.filterElements();
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
if (node.classList && node.classList.contains('tab-list-item')) {
const nameSpan = node.querySelector('.name');
if (nameSpan && filterConfig.textFilters.includes(nameSpan.innerText)) {
node.remove();
console.log('[监控中]移除了一条帖子');
}
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
})();