If pressing the "查看全部評分" button doesn't result in the appearance of the pop-up window displaying collected names, let's do some troubleshooting and modifications.
Steps to Diagnose and Fix:
-
Verify HTML Structure:
- The script needs to target the appropriate HTML elements. Double-check the selectors (
table.list, .u-avatar .name span) to ensure they are correct for your page.
-
Add Detailed Logging:
- Insert
console.log statements to trace whether various parts of the code are being executed. This helps determine if the issue is with the element detection, name collection, or display logic.
-
Ensure Mutation Detection:
- Ensure that the pop-up content is actually being detected. If the pop-up is dynamically loaded (such as via AJAX), ensure the observer can detect this change.
Modified Script with Enhanced Logging:
// ==UserScript==
// @name Thread-wide Name Highlighter with Display
// @namespace http://tampermonkey.net/
// @version 0.6
// @description Collect and highlight names from Jkforum threads spanning multiple pages, with a display of collected names
// @author You
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Function to create a UI window to display collected names
function createNamesDisplay(names) {
console.log("Creating names display window...");
// Check if display already exists
let existingContainer = document.getElementById('namesDisplay');
if (existingContainer) {
console.log("Names display already exists. Updating names...");
updateNamesList(existingContainer, names);
return;
}
// Create display container
const container = document.createElement('div');
container.id = 'namesDisplay';
container.style.position = 'fixed';
container.style.top = '20px';
container.style.right = '20px';
container.style.backgroundColor = 'rgba(255,255,255,0.8)';
container.style.border = '1px solid #ccc';
container.style.padding = '10px';
container.style.zIndex = '1000';
container.style.maxHeight = '300px';
container.style.overflowY = 'auto';
container.style.borderRadius = '5px';
container.style.boxShadow = '0 0 10px rgba(0,0,0,0.2)';
// Create title and close button
const title = document.createElement('h4');
title.textContent = 'Collected Names';
title.style.margin = '0 0 5px 0';
container.appendChild(title);
const closeButton = document.createElement('button');
closeButton.textContent = 'Close';
closeButton.style.marginBottom = '10px';
closeButton.onclick = () => container.remove();
container.appendChild(closeButton);
// Create list of names
updateNamesList(container, names);
// Append to body
document.body.appendChild(container);
}
function updateNamesList(container, names) {
const list = document.createElement('ul');
names.forEach(name => {
const listItem = document.createElement('li');
listItem.textContent = name;
list.appendChild(listItem);
});
container.querySelector('ul')?.remove(); // Remove old list if exists
container.appendChild(list);
}
// Function to extract `tid` from the URL
function getThreadId() {
const url = window.location.href;
const match = /tid=(\d+)/.exec(url);
return match ? match[1] : null;
}
// Collector functions
function collectNamesFromTable() {
const elements = document.querySelectorAll('table.list tbody tr td:nth-child(2) a');
const names = Array.from(elements).map(el => el.textContent.trim());
console.log('Collected names from table:', names);
return names;
}
function collectNamesFromReplies() {
const elements = document.querySelectorAll('.u-avatar .name span');
const names = Array.from(elements).map(el => el.textContent.trim());
console.log('Collected names from replies:', names);
return names;
}
// Function to store and highlight names
function storeAndDisplayNames(newNames, tid) {
const storedNames = JSON.parse(localStorage.getItem(`threadNames_${tid}`)) || [];
const combinedNames = Array.from(new Set([...storedNames, ...newNames]));
// Store combined names in localStorage
localStorage.setItem(`threadNames_${tid}`, JSON.stringify(combinedNames));
// Create display window
createNamesDisplay(combinedNames);
// Highlight names in the document
highlightNames(combinedNames);
}
// Function to apply highlights
function highlightNames(names) {
const elements = document.querySelectorAll('table.list tbody tr td a, .u-avatar .name span');
elements.forEach(el => {
if (names.includes(el.textContent.trim())) {
el.style.backgroundColor = 'yellow';
console.log(`Highlighted: ${el.textContent.trim()}`);
}
});
}
// Function to handle dynamic content
function observeDynamicContent() {
const observer = new MutationObserver(() => {
const popupTable = document.querySelector('table.list');
if (popupTable) {
console.log("Detected popup table in the DOM.");
const tid = getThreadId();
if (tid) {
const popupNames = collectNamesFromTable();
const replyNames = collectNamesFromReplies();
storeAndDisplayNames([...popupNames, ...replyNames], tid);
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
console.log("MutationObserver set.");
}
function main() {
const tid = getThreadId();
if (!tid) {
console.warn('Thread ID not found in URL.');
return;
}
const storedNames = JSON.parse(localStorage.getItem(`threadNames_${tid}`)) || [];
// Display stored names immediately upon page load
createNamesDisplay(storedNames);
highlightNames(storedNames);
observeDynamicContent();
}
window.addEventListener('load', main);
})();
Key Modifications:
-
Console Logs: Added console logs at each major step to track the script's progress and diagnose where it might be failing.
-
Dynamic Content Check: Ensured the MutationObserver is effectively monitoring DOM changes for dynamically added content.
-
Update UI: The name display window updates when new names are added rather than re-creating the window, which should eliminate UI issues if names update quickly after the button is pressed.
Debugging Steps:
- Open Developer Console: Open the console (Press F12, then navigate to the Console tab) and look for logs when you press the "查看全部評分" button to check if the script detects these actions.
- Check Selectors: Make sure
.list and .u-avatar .name span are correctly targeting the elements that contain the names.
- Network & Scripting Delays: If there is a delay in loading content (AJAX requests), ensure the selector checks occur after content is available.
- Script Load: Verify the script is activated and running on the expected URLs with no restrictions preventing execution.
Once the script correctly executes, you should see a pop-up window included with the names.