NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name AO3 Filter
// @namespace https://openuserjs.org/user/mika_37524
// @version 2.5
// @description 标签匹配 + 添加标签+ 模式切换 + 跳页 + 标签数量筛选
// @author Mika
// @license MIT
// @include https://archiveofourown.org/works*
// @include https://archiveofourown.org/tags/*/works*
// @grant none
// ==/UserScript==
// ==OpenUserJS==
// @author mika_37524
// ==OpenUserJS==
(function() {
'use strict';
// ====== 状态管理 ======
let SELECTED_TAGS = [];
let FILTER_MODE = 'strict';
let MANUAL_TAGS = [];
let TAG_COUNT_FILTER = null;
const STORAGE_KEY = 'ao3_selected_tags';
const MODE_KEY = 'ao3_filter_mode';
const MANUAL_KEY = 'ao3_manual_tags';
const JUMP_COUNT_KEY = 'ao3_jump_count';
const TAG_COUNT_KEY = 'ao3_tag_count_filter';
// ====== 工具函数 ======
function extractRawTagName(tagElement) {
const href = tagElement.getAttribute('href');
if (!href) return tagElement.textContent.trim();
let raw = href.replace(/^\/tags\//, '').replace(/\/works$/, '');
raw = decodeURIComponent(raw);
raw = raw.replace(/_/g, ' ');
return raw.trim();
}
// ====== 加载保存的数据 ======
function loadSavedTags() {
const saved = sessionStorage.getItem(STORAGE_KEY);
if (saved) {
try {
SELECTED_TAGS = JSON.parse(saved);
} catch (e) {
SELECTED_TAGS = [];
}
}
}
function saveTags() {
sessionStorage.setItem(STORAGE_KEY, JSON.stringify(SELECTED_TAGS));
}
function loadFilterMode() {
const mode = sessionStorage.getItem(MODE_KEY);
if (mode === 'strict' || mode === 'merge') {
FILTER_MODE = mode;
}
}
function saveFilterMode() {
sessionStorage.setItem(MODE_KEY, FILTER_MODE);
}
function loadManualTags() {
const saved = localStorage.getItem(MANUAL_KEY);
if (saved) {
try {
MANUAL_TAGS = JSON.parse(saved);
return;
} catch (e) {
MANUAL_TAGS = [];
}
}
const oldSaved = sessionStorage.getItem(MANUAL_KEY);
if (oldSaved) {
try {
MANUAL_TAGS = JSON.parse(oldSaved);
saveManualTags();
sessionStorage.removeItem(MANUAL_KEY);
return;
} catch (e) {
MANUAL_TAGS = [];
}
}
MANUAL_TAGS = [];
}
function saveManualTags() {
localStorage.setItem(MANUAL_KEY, JSON.stringify(MANUAL_TAGS));
}
function clearAllManualTags() {
MANUAL_TAGS = [];
localStorage.removeItem(MANUAL_KEY);
}
function loadTagCountFilter() {
const saved = localStorage.getItem(TAG_COUNT_KEY);
if (saved !== null) {
try {
TAG_COUNT_FILTER = JSON.parse(saved);
return;
} catch (e) {
TAG_COUNT_FILTER = null;
}
}
TAG_COUNT_FILTER = null;
}
function saveTagCountFilter() {
localStorage.setItem(TAG_COUNT_KEY, JSON.stringify(TAG_COUNT_FILTER));
}
loadSavedTags();
loadFilterMode();
loadManualTags();
loadTagCountFilter();
// ====== 创建悬浮按钮 ======
function createFloatingButtons() {
if (document.getElementById('my-filter-toggle')) return;
const container = document.createElement('div');
container.id = 'my-float-container';
container.style.cssText = `
position: fixed;
bottom: 24px;
right: 24px;
z-index: 99999;
display: flex;
flex-direction: column;
align-items: flex-end;
gap: 10px;
`;
const baseStyle = `
height: 48px;
width: 48px;
min-width: 48px;
border: none;
border-radius: 14px;
box-shadow: 0 4px 14px rgba(0,0,0,0.13);
font-size: 18px;
cursor: pointer;
transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
display: flex;
align-items: center;
justify-content: center;
user-select: none;
padding: 0;
overflow: hidden;
white-space: nowrap;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
`;
const countBtn = document.createElement('button');
countBtn.id = 'my-count-btn';
const countActive = TAG_COUNT_FILTER !== null;
countBtn.style.cssText = baseStyle + `
background: ${countActive ? 'linear-gradient(135deg, #f7971e, #ffd200)' : '#e8e8e8'};
color: ${countActive ? '#fff' : '#888'};
`;
countBtn.textContent = '🏷️';
countBtn.title = '标签数量筛选';
countBtn.addEventListener('mouseenter', function() {
const status = TAG_COUNT_FILTER !== null ? `≤${TAG_COUNT_FILTER}个` : '不限';
this.style.width = '150px';
this.style.minWidth = '150px';
this.style.borderRadius = '14px';
this.style.justifyContent = 'flex-start';
this.style.padding = '0 16px';
this.style.gap = '8px';
this.style.fontSize = '14px';
this.style.fontWeight = '500';
this.innerHTML = `🏷️ ${status}`;
this.style.transform = 'translateY(-2px)';
this.style.boxShadow = '0 8px 24px rgba(0,0,0,0.18)';
});
countBtn.addEventListener('mouseleave', function() {
this.style.width = '48px';
this.style.minWidth = '48px';
this.style.borderRadius = '14px';
this.style.justifyContent = 'center';
this.style.padding = '0';
this.style.gap = '0';
this.style.fontSize = '18px';
this.style.fontWeight = 'normal';
this.textContent = '🏷️';
this.style.transform = 'translateY(0)';
this.style.boxShadow = '0 4px 14px rgba(0,0,0,0.13)';
});
countBtn.addEventListener('mousedown', function() {
this.style.transform = 'scale(0.94)';
});
countBtn.addEventListener('mouseup', function() {
this.style.transform = 'translateY(-2px)';
});
countBtn.addEventListener('click', function(e) {
e.stopPropagation();
toggleCountPanel();
});
const jumpBtn = document.createElement('button');
jumpBtn.id = 'my-jump-btn';
jumpBtn.style.cssText = baseStyle + `
background: linear-gradient(135deg, #4a90e2, #357abd);
color: white;
`;
jumpBtn.textContent = '📄';
jumpBtn.title = '跳至下一匹配页';
jumpBtn.addEventListener('mouseenter', function() {
this.style.width = '150px';
this.style.minWidth = '150px';
this.style.borderRadius = '14px';
this.style.justifyContent = 'flex-start';
this.style.padding = '0 16px';
this.style.gap = '8px';
this.style.fontSize = '14px';
this.style.fontWeight = '500';
this.innerHTML = '📄 跳页';
this.style.transform = 'translateY(-2px)';
this.style.boxShadow = '0 8px 24px rgba(74, 144, 226, 0.4)';
});
jumpBtn.addEventListener('mouseleave', function() {
this.style.width = '48px';
this.style.minWidth = '48px';
this.style.borderRadius = '14px';
this.style.justifyContent = 'center';
this.style.padding = '0';
this.style.gap = '0';
this.style.fontSize = '18px';
this.style.fontWeight = 'normal';
this.textContent = '📄';
this.style.transform = 'translateY(0)';
this.style.boxShadow = '0 4px 14px rgba(0,0,0,0.13)';
});
jumpBtn.addEventListener('mousedown', function() {
this.style.transform = 'scale(0.94)';
});
jumpBtn.addEventListener('mouseup', function() {
this.style.transform = 'translateY(-2px)';
});
jumpBtn.addEventListener('click', function() {
sessionStorage.setItem(JUMP_COUNT_KEY, '0');
forceJumpToNextMatch();
});
const toggleBtn = document.createElement('button');
toggleBtn.id = 'my-filter-toggle';
toggleBtn.style.cssText = baseStyle + `
background: linear-gradient(135deg, #ff6b6b, #ee5a24);
color: white;
`;
toggleBtn.textContent = '✨';
toggleBtn.title = '打开筛选面板';
toggleBtn.addEventListener('mouseenter', function() {
this.style.width = '150px';
this.style.minWidth = '150px';
this.style.borderRadius = '14px';
this.style.justifyContent = 'flex-start';
this.style.padding = '0 16px';
this.style.gap = '8px';
this.style.fontSize = '14px';
this.style.fontWeight = '500';
this.innerHTML = '✨ 筛选';
this.style.transform = 'translateY(-2px)';
this.style.boxShadow = '0 8px 24px rgba(255, 107, 107, 0.4)';
});
toggleBtn.addEventListener('mouseleave', function() {
this.style.width = '48px';
this.style.minWidth = '48px';
this.style.borderRadius = '14px';
this.style.justifyContent = 'center';
this.style.padding = '0';
this.style.gap = '0';
this.style.fontSize = '18px';
this.style.fontWeight = 'normal';
this.textContent = '✨';
this.style.transform = 'translateY(0)';
this.style.boxShadow = '0 4px 14px rgba(0,0,0,0.13)';
});
toggleBtn.addEventListener('mousedown', function() {
this.style.transform = 'scale(0.94)';
});
toggleBtn.addEventListener('mouseup', function() {
this.style.transform = 'translateY(-2px)';
});
container.appendChild(countBtn);
container.appendChild(jumpBtn);
container.appendChild(toggleBtn);
document.body.appendChild(container);
createCountPanel(countBtn);
return toggleBtn;
}
// ====== 数量筛选面板 ======
function createCountPanel(countBtn) {
if (document.getElementById('my-count-panel')) return;
const panel = document.createElement('div');
panel.id = 'my-count-panel';
panel.style.cssText = `
position: fixed;
bottom: 190px;
right: 24px;
z-index: 99998;
background: white;
border-radius: 16px;
padding: 24px 20px 20px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
width: 260px;
display: none;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
font-size: 14px;
color: #333;
border: 1px solid rgba(255,255,255,0.1);
`;
panel.innerHTML = `
<div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:16px;">
<h4 style="margin:0; font-size:15px; font-weight:600; color:#222;">🏷️ 标签数量筛选</h4>
<button id="my-count-close" style="
background:none; border:none; font-size:18px; cursor:pointer;
color:#999; transition: transform 0.2s; width:28px; height:28px;
display:flex; align-items:center; justify-content:center;
border-radius:50%;
">✕</button>
</div>
<div style="margin-bottom:8px;">
<div style="display:flex; justify-content:space-between; font-size:13px; color:#888;">
<span>不限</span>
<span id="count-display" style="font-weight:600; color:#f7971e; font-size:15px;">
${TAG_COUNT_FILTER !== null ? `≤ ${TAG_COUNT_FILTER} 个` : '不限'}
</span>
<span>10</span>
</div>
<input type="range" id="count-slider" min="0" max="10" step="1"
value="${TAG_COUNT_FILTER !== null ? TAG_COUNT_FILTER : 0}"
style="
width: 100%;
margin: 6px 0 4px;
accent-color: #f7971e;
height: 4px;
border-radius: 2px;
">
<div style="display:flex; justify-content:space-between; font-size:9px; color:#ccc; padding:0 2px;">
<span>0</span><span>1</span><span>2</span><span>3</span><span>4</span>
<span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
</div>
</div>
<div style="display:flex; gap:8px; margin-top:14px;">
<button id="count-clear-btn" style="
flex:1; padding:8px 0; background:#f0f0f0; color:#666; border:none;
border-radius:10px; cursor:pointer; font-size:13px; font-weight:500;
transition: background 0.2s;
">取消筛选</button>
<button id="count-apply-btn" style="
flex:1; padding:8px 0; background:linear-gradient(135deg, #f7971e, #ffd200);
color:white; border:none; border-radius:10px; cursor:pointer;
font-size:13px; font-weight:600; transition: transform 0.15s;
">✓ 应用</button>
</div>
`;
document.body.appendChild(panel);
document.getElementById('my-count-close').addEventListener('click', () => {
panel.style.display = 'none';
});
const slider = document.getElementById('count-slider');
const display = document.getElementById('count-display');
slider.addEventListener('input', function() {
const val = parseInt(this.value);
display.textContent = val === 0 ? '不限' : `≤ ${val} 个`;
});
document.getElementById('count-clear-btn').addEventListener('click', function() {
TAG_COUNT_FILTER = null;
saveTagCountFilter();
slider.value = 0;
display.textContent = '不限';
panel.style.display = 'none';
updateCountButton();
filterWorks();
updateStats();
});
document.getElementById('count-apply-btn').addEventListener('click', function() {
const val = parseInt(slider.value);
TAG_COUNT_FILTER = val === 0 ? null : val;
saveTagCountFilter();
panel.style.display = 'none';
updateCountButton();
filterWorks();
updateStats();
});
document.addEventListener('click', function(e) {
const panelEl = document.getElementById('my-count-panel');
const btn = document.getElementById('my-count-btn');
if (panelEl && btn && panelEl.style.display !== 'none') {
if (!panelEl.contains(e.target) && !btn.contains(e.target)) {
panelEl.style.display = 'none';
}
}
});
}
function toggleCountPanel() {
const panel = document.getElementById('my-count-panel');
if (!panel) return;
if (panel.style.display === 'none') {
panel.style.display = 'block';
const slider = document.getElementById('count-slider');
const display = document.getElementById('count-display');
if (slider) {
slider.value = TAG_COUNT_FILTER !== null ? TAG_COUNT_FILTER : 0;
display.textContent = TAG_COUNT_FILTER !== null ? `≤ ${TAG_COUNT_FILTER} 个` : '不限';
}
} else {
panel.style.display = 'none';
}
}
function updateCountButton() {
const btn = document.getElementById('my-count-btn');
if (!btn) return;
if (TAG_COUNT_FILTER !== null) {
btn.style.background = 'linear-gradient(135deg, #f7971e, #ffd200)';
btn.style.color = '#fff';
} else {
btn.style.background = '#e8e8e8';
btn.style.color = '#888';
}
if (!btn.matches(':hover')) {
btn.textContent = '🏷️';
btn.style.width = '48px';
btn.style.minWidth = '48px';
btn.style.justifyContent = 'center';
btn.style.padding = '0';
btn.style.gap = '0';
btn.style.fontSize = '18px';
btn.style.fontWeight = 'normal';
}
}
// ====== 主面板 ======
function createPanel(toggleBtn) {
if (document.getElementById('my-filter-panel')) return;
const panel = document.createElement('div');
panel.id = 'my-filter-panel';
panel.style.cssText = `
position: fixed; bottom: 80px; right: 24px; z-index: 99998;
background: white; border-radius: 16px;
padding: 24px 22px 22px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
width: 380px; display: none;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
font-size: 14px; color: #333;
max-height: 75vh;
overflow-y: auto;
border: 1px solid rgba(255,255,255,0.1);
`;
panel.innerHTML = `
<div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:18px;">
<h3 style="margin:0; font-size:17px; font-weight:600; color:#222;">✨ 筛选面板</h3>
<button id="my-filter-close" style="
background:none; border:none; font-size:18px; cursor:pointer;
color:#999; transition: transform 0.2s; width:28px; height:28px;
display:flex; align-items:center; justify-content:center;
border-radius:50%;
">✕</button>
</div>
<div style="margin-bottom:14px;">
<div style="display:flex; align-items:center; gap:10px; margin-bottom:6px;">
<span style="font-weight:600; font-size:13px; color:#555;">🔀 模式</span>
<button id="mode-strict" style="
padding:5px 16px; border-radius:20px; border:1px solid #ddd;
background:${FILTER_MODE === 'strict' ? '#ff6b6b' : '#f5f5f5'};
color:${FILTER_MODE === 'strict' ? 'white' : '#666'};
cursor:pointer; font-size:12px; font-weight:500;
transition: all 0.2s;
">严格匹配</button>
<button id="mode-merge" style="
padding:5px 16px; border-radius:20px; border:1px solid #ddd;
background:${FILTER_MODE === 'merge' ? '#ff6b6b' : '#f5f5f5'};
color:${FILTER_MODE === 'merge' ? 'white' : '#666'};
cursor:pointer; font-size:12px; font-weight:500;
transition: all 0.2s;
">合并搜索</button>
</div>
<div id="mode-description" style="font-size:12px; color:#999; margin-top:2px; padding-left:4px;">
${FILTER_MODE === 'strict' ? '✅ 文章标签完全等于已选标签' : '✅ 文章标签包含在已选标签内'}
</div>
</div>
<div style="background:linear-gradient(135deg, #f8f9fa, #f1f3f5); padding:10px 14px; border-radius:10px; margin-bottom:14px;">
<div style="display:flex; justify-content:space-between; font-size:13px;">
<span style="color:#555;">📊 统计</span>
<span id="filter-stats" style="font-weight:500; color:#222;">加载中...</span>
</div>
${TAG_COUNT_FILTER !== null ? `<div style="font-size:11px; color:#f7971e; margin-top:3px;">🏷️ 数量筛选: ≤ ${TAG_COUNT_FILTER} 个</div>` : ''}
</div>
<div id="selected-tag-area" style="margin-bottom:14px; min-height:40px; border:2px dashed #e8e8e8; border-radius:10px; padding:12px; background:#fafafa;">
<div style="font-weight:600; margin-bottom:8px; font-size:12px; color:#888;">✅ 已选标签(点击移除)</div>
<div id="selected-tags-container" style="display:flex; flex-wrap:wrap; gap:6px;">
<span style="color:#bbb; font-size:13px;">暂无</span>
</div>
</div>
<div id="manual-tag-area" style="margin-bottom:14px; border:1px solid #eee; border-radius:10px; overflow:hidden;">
<div id="manual-toggle" style="
padding:10px 14px; background:#f8f9fa; cursor:pointer;
display:flex; justify-content:space-between; align-items:center;
font-size:13px; color:#555; font-weight:600;
">
<span>✏️ 手动标签 <span style="font-weight:400; color:#bbb; font-size:11px;">(永久保存)</span></span>
<span id="manual-arrow" style="color:#bbb;">▶</span>
</div>
<div id="manual-content" style="display:none; padding:14px; background:#fff;">
<div style="display:flex; gap:8px; margin-bottom:10px;">
<input type="text" id="manual-input" placeholder="输入标签名称..." style="
flex:1; padding:8px 12px; border:1px solid #ddd; border-radius:8px;
font-size:13px; outline:none; transition: border 0.2s;
">
<button id="manual-add-btn" style="
padding:8px 16px; background:linear-gradient(135deg, #4a90e2, #357abd);
color:white; border:none; border-radius:8px; cursor:pointer; font-size:13px;
font-weight:500; transition: transform 0.15s;
">添加</button>
</div>
<div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:6px;">
<span style="font-size:11px; color:#888;">已保存的手动标签</span>
<button id="clear-manual-btn" style="
padding:2px 12px; background:#ff6b6b; color:white; border:none;
border-radius:6px; cursor:pointer; font-size:11px;
">清除全部</button>
</div>
<div id="manual-history" style="display:flex; flex-wrap:wrap; gap:6px;">
<span style="color:#bbb; font-size:12px;">暂无</span>
</div>
</div>
</div>
<div id="all-tag-area" style="margin-bottom:14px;">
<div style="font-weight:600; margin-bottom:8px; font-size:12px; color:#888;">📌 点击标签添加</div>
<input type="text" id="tag-search-input" placeholder="🔍 搜索标签..." style="
width:100%; padding:8px 12px; margin-bottom:10px;
border:1px solid #ddd; border-radius:8px; font-size:13px;
box-sizing:border-box; outline:none; transition: border 0.2s;
">
<div id="all-tags-container" style="display:flex; flex-wrap:wrap; gap:6px;">
<span style="color:#bbb; font-size:13px;">加载中...</span>
</div>
</div>
<button id="refresh-btn" style="
width:100%; padding:9px; background:#e8e8e8; color:#666; border:none;
border-radius:10px; cursor:pointer; font-size:13px; font-weight:500;
transition: all 0.2s;
">🔄 刷新标签列表</button>
`;
document.body.appendChild(panel);
const closeBtn = document.getElementById('my-filter-close');
closeBtn.addEventListener('click', () => { panel.style.display = 'none'; });
toggleBtn.addEventListener('click', function() {
if (panel.style.display === 'none') {
panel.style.display = 'block';
extractAndDisplayTags();
updateStats();
renderManualHistory();
} else {
panel.style.display = 'none';
}
});
document.getElementById('mode-strict').addEventListener('click', function() {
FILTER_MODE = 'strict';
saveFilterMode();
this.style.background = '#ff6b6b';
this.style.color = 'white';
document.getElementById('mode-merge').style.background = '#f5f5f5';
document.getElementById('mode-merge').style.color = '#666';
document.getElementById('mode-description').textContent = '✅ 文章标签完全等于已选标签';
filterWorks();
updateStats();
});
document.getElementById('mode-merge').addEventListener('click', function() {
FILTER_MODE = 'merge';
saveFilterMode();
this.style.background = '#ff6b6b';
this.style.color = 'white';
document.getElementById('mode-strict').style.background = '#f5f5f5';
document.getElementById('mode-strict').style.color = '#666';
document.getElementById('mode-description').textContent = '✅ 文章标签包含在已选标签内';
filterWorks();
updateStats();
});
const manualToggle = document.getElementById('manual-toggle');
const manualContent = document.getElementById('manual-content');
const manualArrow = document.getElementById('manual-arrow');
let manualOpen = false;
manualToggle.addEventListener('click', function() {
manualOpen = !manualOpen;
manualContent.style.display = manualOpen ? 'block' : 'none';
manualArrow.textContent = manualOpen ? '▼' : '▶';
if (manualOpen) renderManualHistory();
});
const manualInput = document.getElementById('manual-input');
const manualAddBtn = document.getElementById('manual-add-btn');
function addManualTag() {
const tag = manualInput.value.trim();
if (!tag) { alert('请输入标签名称'); return; }
if (MANUAL_TAGS.includes(tag)) { alert('已存在'); return; }
MANUAL_TAGS.push(tag);
saveManualTags();
manualInput.value = '';
renderManualHistory();
if (!SELECTED_TAGS.includes(tag)) {
SELECTED_TAGS.push(tag);
saveTags();
}
extractAndDisplayTags();
filterWorks();
updateStats();
}
manualAddBtn.addEventListener('click', addManualTag);
manualInput.addEventListener('keydown', function(e) {
if (e.key === 'Enter') addManualTag();
});
document.getElementById('clear-manual-btn').addEventListener('click', function() {
if (MANUAL_TAGS.length === 0) { alert('没有手动标签'); return; }
if (confirm(`清除全部 ${MANUAL_TAGS.length} 个手动标签?`)) {
MANUAL_TAGS.forEach(tag => {
SELECTED_TAGS = SELECTED_TAGS.filter(t => t !== tag);
});
saveTags();
clearAllManualTags();
renderManualHistory();
extractAndDisplayTags();
filterWorks();
updateStats();
}
});
document.getElementById('refresh-btn').addEventListener('click', function() {
SELECTED_TAGS = [];
saveTags();
sessionStorage.removeItem(JUMP_COUNT_KEY);
extractAndDisplayTags();
filterWorks();
updateStats();
});
document.getElementById('tag-search-input').addEventListener('input', function() {
const keyword = this.value.trim().toLowerCase();
document.querySelectorAll('#all-tags-container button').forEach(btn => {
btn.style.display = btn.textContent.toLowerCase().includes(keyword) ? 'inline-block' : 'none';
});
});
}
// ====== 渲染手动标签 ======
function renderManualHistory() {
const container = document.getElementById('manual-history');
if (!container) return;
if (MANUAL_TAGS.length === 0) {
container.innerHTML = '<span style="color:#bbb; font-size:12px;">暂无</span>';
return;
}
container.innerHTML = '';
MANUAL_TAGS.forEach(tag => {
const btn = document.createElement('button');
btn.textContent = tag + ' ✕';
btn.style.cssText = `
padding: 3px 12px; background:#e8f0fe; border:1px solid #4a90e2;
border-radius:14px; cursor:pointer; font-size:12px; color:#1a73e8;
transition: all 0.15s;
`;
btn.addEventListener('click', function() {
MANUAL_TAGS = MANUAL_TAGS.filter(t => t !== tag);
saveManualTags();
renderManualHistory();
if (SELECTED_TAGS.includes(tag)) {
if (confirm(`同时从已选标签中移除 "${tag}"?`)) {
SELECTED_TAGS = SELECTED_TAGS.filter(t => t !== tag);
saveTags();
extractAndDisplayTags();
filterWorks();
updateStats();
}
}
});
container.appendChild(btn);
});
}
// ====== 标签抓取 ======
function extractAndDisplayTags() {
const works = document.querySelectorAll('.work.blurb');
if (works.length === 0) {
const container = document.getElementById('all-tags-container');
if (container) container.innerHTML = '<span style="color:#bbb;">当前页面没有文章</span>';
return;
}
const tagMap = new Map();
works.forEach(work => {
work.querySelectorAll('.fandoms .tag').forEach(tag => {
const raw = extractRawTagName(tag);
const display = tag.textContent.trim();
if (raw && display && !tagMap.has(raw)) {
tagMap.set(raw, display);
}
});
});
const sorted = Array.from(tagMap.entries()).sort((a, b) => a[0].localeCompare(b[0]));
if (sorted.length === 0) {
const container = document.getElementById('all-tags-container');
if (container) container.innerHTML = '<span style="color:#bbb;">未找到标签</span>';
return;
}
renderTagLists(sorted);
}
function renderTagLists(tagEntries) {
const selectedContainer = document.getElementById('selected-tags-container');
const allContainer = document.getElementById('all-tags-container');
if (!selectedContainer || !allContainer) return;
selectedContainer.innerHTML = '';
allContainer.innerHTML = '';
const displayMap = new Map(tagEntries);
MANUAL_TAGS.forEach(tag => {
if (!displayMap.has(tag)) displayMap.set(tag, tag + ' 📌');
});
if (SELECTED_TAGS.length === 0) {
selectedContainer.innerHTML = '<span style="color:#bbb; font-size:13px;">暂无</span>';
} else {
SELECTED_TAGS.forEach(raw => {
const display = displayMap.get(raw) || raw;
const isManual = MANUAL_TAGS.includes(raw);
const btn = document.createElement('button');
btn.textContent = display + (isManual ? ' 📌' : '');
btn.dataset.rawTag = raw;
btn.style.cssText = `
padding: 4px 14px;
background: ${isManual ? 'linear-gradient(135deg, #4a90e2, #357abd)' : '#ff6b6b'};
color: white; border: none; border-radius: 18px;
cursor: pointer; font-size: 12px; font-weight:500;
transition: all 0.15s;
`;
btn.addEventListener('click', function() {
SELECTED_TAGS = SELECTED_TAGS.filter(t => t !== this.dataset.rawTag);
saveTags();
extractAndDisplayTags();
filterWorks();
updateStats();
});
selectedContainer.appendChild(btn);
});
}
const available = tagEntries.filter(([raw]) => !SELECTED_TAGS.includes(raw));
if (available.length === 0 && MANUAL_TAGS.every(t => SELECTED_TAGS.includes(t))) {
allContainer.innerHTML = '<span style="color:#bbb; font-size:13px;">没有更多标签</span>';
} else {
available.forEach(([raw, display]) => {
const btn = document.createElement('button');
btn.textContent = display;
btn.dataset.rawTag = raw;
btn.style.cssText = `
padding: 4px 14px; background:#f0f0f0; border:1px solid #e0e0e0;
border-radius:18px; cursor:pointer; font-size:12px;
transition: all 0.15s; color:#555;
`;
btn.addEventListener('click', function() {
const raw = this.dataset.rawTag;
if (!SELECTED_TAGS.includes(raw)) {
SELECTED_TAGS.push(raw);
saveTags();
extractAndDisplayTags();
filterWorks();
updateStats();
}
});
allContainer.appendChild(btn);
});
}
document.getElementById('tag-search-input').value = '';
allContainer.querySelectorAll('button').forEach(btn => {
btn.style.display = 'inline-block';
});
}
// ====== 统计 ======
function updateStats() {
const total = document.querySelectorAll('.work.blurb').length;
const hidden = document.querySelectorAll('.work.blurb[style*="display: none"]').length;
const kept = total - hidden;
const statsElem = document.getElementById('filter-stats');
if (statsElem) {
let text = `共 ${total} 篇,保留 ${kept} 篇`;
if (TAG_COUNT_FILTER !== null) text += ` | 🏷️ ≤${TAG_COUNT_FILTER}`;
statsElem.textContent = text;
}
}
// ====== 核心过滤 ======
function filterWorks() {
const works = document.querySelectorAll('.work.blurb');
if (works.length === 0) return;
if (SELECTED_TAGS.length === 0 && TAG_COUNT_FILTER === null) {
works.forEach(w => w.style.display = '');
updateStats();
return;
}
let hiddenCount = 0;
works.forEach(function(work) {
const rawNames = Array.from(work.querySelectorAll('.fandoms .tag')).map(t => extractRawTagName(t));
const tagCount = rawNames.length;
let isMatch = true;
if (SELECTED_TAGS.length > 0) {
let contentMatch = false;
if (FILTER_MODE === 'strict') {
contentMatch = rawNames.length === SELECTED_TAGS.length &&
SELECTED_TAGS.every(t => rawNames.some(n => n.toLowerCase() === t.toLowerCase()));
} else {
contentMatch = rawNames.every(n => SELECTED_TAGS.some(t => n.toLowerCase() === t.toLowerCase()));
}
if (!contentMatch) isMatch = false;
}
if (isMatch && TAG_COUNT_FILTER !== null) {
if (tagCount > TAG_COUNT_FILTER) isMatch = false;
}
work.style.display = isMatch ? '' : 'none';
if (!isMatch) hiddenCount++;
});
updateStats();
}
// ====== 跳转 ======
function forceJumpToNextMatch() {
filterWorks();
const nextLink = document.querySelector('.pagination .next a, .pagination .next button');
if (!nextLink) {
sessionStorage.removeItem(JUMP_COUNT_KEY);
showNotice('⚠️ 已到达最后一页', '#ff6b6b');
return;
}
let count = parseInt(sessionStorage.getItem(JUMP_COUNT_KEY) || '0');
count++;
sessionStorage.setItem(JUMP_COUNT_KEY, String(count));
if (count <= 5) {
window.location.href = nextLink.href;
return;
}
if (count <= 9) {
window.location.href = nextLink.href;
return;
}
if (count === 10) {
sessionStorage.removeItem(JUMP_COUNT_KEY);
showNotice('⚠️ 已连续跳转10页,未找到匹配', '#ff6b6b');
}
}
function showNotice(text, color) {
const notice = document.createElement('div');
notice.style.cssText = `
position: fixed; top: 24px; left: 50%; transform: translateX(-50%);
background: ${color}; color: white; padding: 12px 28px;
border-radius: 12px; font-size: 14px; z-index: 99999;
box-shadow: 0 4px 20px rgba(0,0,0,0.2);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
font-weight: 500;
`;
notice.textContent = text;
document.body.appendChild(notice);
setTimeout(() => notice.remove(), 4000);
}
// ====== 自动跳转 ======
function autoContinueJump() {
const count = sessionStorage.getItem(JUMP_COUNT_KEY);
if (!count) return;
if (SELECTED_TAGS.length === 0 && TAG_COUNT_FILTER === null) {
sessionStorage.removeItem(JUMP_COUNT_KEY);
return;
}
setTimeout(function() {
const works = document.querySelectorAll('.work.blurb');
if (works.length === 0) {
setTimeout(autoContinueJump, 300);
return;
}
filterWorks();
const visible = document.querySelectorAll('.work.blurb:not([style*="display: none"])');
if (visible.length > 0) {
sessionStorage.removeItem(JUMP_COUNT_KEY);
showNotice('✅ 找到匹配文章,已停在该页', '#4a90e2');
return;
}
forceJumpToNextMatch();
}, 500);
}
// ====== 初始化 ======
let isRunning = false;
const observer = new MutationObserver(function() {
if (isRunning) return;
const works = document.querySelectorAll('.work.blurb');
if (works.length > 0) {
isRunning = true;
setTimeout(function() {
if (SELECTED_TAGS.length > 0 || TAG_COUNT_FILTER !== null) {
filterWorks();
}
isRunning = false;
}, 300);
}
});
observer.observe(document.body, { childList: true, subtree: true });
window.addEventListener('load', function() {
setTimeout(function() {
const toggleBtn = createFloatingButtons();
createPanel(toggleBtn);
updateCountButton();
if (SELECTED_TAGS.length > 0 || TAG_COUNT_FILTER !== null) {
setTimeout(function() {
filterWorks();
autoContinueJump();
}, 500);
}
}, 500);
});
})();