https://www.jkforum.net/forum.php?mod=misc&action=viewratings&tid=19286379&pid=168231550…
- User
https://www.jkforum.net/forum.php?mod=misc&action=viewratings&tid=19286379&pid=168231550
I would like to highlight some names of thread from the website https://www.jkforum.net/* using Tampermonkey. The name list is to be found by selecting a thread and pressing a button called 查看全部評分 from it. The element is shown below.
查看全部評分
When the button is pressed, a window will pop up and the element containing the names is shown below.
The name(s) I need is located at c floatwrap table.list tbody tr td a.
After collecting all the names from the table.list, I would like to locate and hightlight them in the reply section on the pages of the thread.
In other words, compare the name from table.list with a.name.xi2 span. If a match is found, hightlight a.name.xi2 span.
A normal thread has a url like this. https://www.jkforum.net/p/thread-19465064-1-1.html The 8 digit number is the thread id. Every page of the same thread consist of this thead id in its url like this. https://www.jkforum.net/p/forum.php?mod=viewthread&tid=19465064&extra=page%3D1&ordertype=0&page=2
- Assistant
- User
Could you go back to the thread after names are collected?
- Assistant
- User
After the popup window is loaded, it went to the href of the button. Does the script collect the name from the popup window or from the href of the button?
- Assistant
- User
The button opens a window. This is the element.
- Assistant
- User
I don't know see any highlighted name. Since I cannot see if the script has captured the names or not, would you give me a prompt showing the captured names?
- Assistant
- User
It is hard to see the names from the console. Could you show a message with the captured name for 3 seconds?
- Assistant
- User
if I dont' see the message, does it mean no name is captured?
- Assistant
If you do not see the message, it could be indicative of a few different issues, not necessarily that no names were captured. Here's a checklist to help determine why the message might not be appearing and ensure the script functions correctly:
- User
I don't see the popup window open automatically...does it mean the script did not trigger the button?
- Assistant
- User
I can see the popup window opened. Could you close the window by pressing the close window button?
關閉
Moreover, after opening the windows, I still cannot see the message of the collected names.
- Assistant
- User
I found the error. The @match should be // @match https://www.jkforum.net/*
- Assistant
- User
I have noticed the names are not highlighted on pages other than the first page.
- Assistant
- User
did you store the thread ID so that you can check the url if it contains the same thread ID before highlighting the names?
- Assistant
- User
the modification made the page jumped to the button's href like before.
- Assistant
- User
it is no longer capturing name form the table.list.
- Assistant
- User
- Assistant
- User
it opens the popup window, and says it captures no name.
- Assistant
- User
the given version is able to capture the names without any problem.
- Assistant
Great! I'm glad to hear that the script is now capturing the names correctly. If everything is working as expected, you should be able to see the captured usernames announced via the temporary message, and they should also be highlighted correctly in the replies across different pages of the thread.
Here’s a quick checklist to ensure it continues to work smoothly:
-
Verify Highlighting: Make sure the usernames are consistently highlighted in thread replies, especially when navigating across different pages in the thread.
-
Check LocalStorage: The use of
localStorageto persist data like usernames and the thread ID should help in keeping the information consistent across page loads and reloads. -
Logging and Maintenance: Keep the debug logs active in your script if you are still testing or getting occasional issues, as they can be very helpful in real-time adjustments.
-
Performance: Ensure the script doesn't slow down page loads significantly, especially if you're observing a lot of DOM manipulations.
-
- User
sorry.. I meant to say that the older script given to you has no problem capturing the names. However, your modified script is not able to capture any names. Is it because capturing the 8 digit thread id created some problem. Moreover, there is no need to capture the name everything a new page is loaded. As long as the thread id is the same, the same captured name is use for comparison.
- Assistant
- User
same issue! popup window is opened but no name is captured. Also the popup window doesn't close by itself.
- Assistant
- User
problem still exist. I would like to know how do you capture the thread ID?
- Assistant
The thread ID is typically extracted from the page URL because it's a unique identifier for the thread. In your script's context, it seems that the thread URL contains a distinctive pattern that can be matched using a regular expression.
Here's the methodology for capturing the thread ID from the URL:
- User
How about from this url? https://www.jkforum.net/p/forum.php?mod=viewthread&tid=19461253&extra=page%3D1&ordertype=0&page=2
- Assistant
Ah, I see that the URL you're referring to uses query parameters rather than a path-based format to denote the thread ID. In this case, the thread ID (
tid) is part of the query string parameters. You should extract it using JavaScript by parsing the URL's search parameters.Here’s how you can extract the thread ID (
tid) from this URL format:Extracting Thread ID from Query Parameters:
To extract the thread ID when it's provided as a query parameter (
tid), you can use the URL API available in JavaScript: - User
the thread will start with this type of url https://www.jkforum.net/p/thread-19461253-1-1.html When a new page is selected this type of url will appear. https://www.jkforum.net/p/forum.php?mod=viewthread&tid=19461253&extra=page%3D1&ordertype=0&page=2 once the thread ID and name list are captured from the start, it will be use to compare the tid value url from other pages. If thread IDs are the same, use the existing name list to compare names on the page. There is no need to capture the names again because the correct list only exist at the beginning.
- Assistant
- User
it seems like the new name capturing method is not working. The old one works well.
// Function to extract usernames from the modal function extractUsernames() { const names = []; const ratingWindow = document.querySelector('#fwin_viewratings .floatwrap .list'); if (ratingWindow) { const rows = ratingWindow.querySelectorAll('tbody tr'); rows.forEach(row => { const nameCell = row.querySelector('td a'); if (nameCell) { names.push(nameCell.textContent.trim()); } }); } // Show the captured usernames for 3 seconds showTemporaryMessage('Captured Usernames: ' + (names.length > 0 ? names.join(', ') : 'None'), 3000); return names; }Only the first page of the thread should trigger name capture.
- Assistant
- User
Let's change our approach. Please modify the old script so that name capture will only trigger if the url is in this format. Also store the thread ID from the url.
- Assistant
- User
what do these line of code do?
const currentThreadId = getThreadIdFromUrl(); if (names.length > 0 && currentThreadId) { localStorage.setItem('jkforumUsernames', JSON.stringify(names)); localStorage.setItem('jkforumThreadId', currentThreadId); showTemporaryMessage('Captured Usernames: ' + names.join(', '), 3000); } } - Assistant
These lines of code are responsible for capturing usernames from the ratings modal and storing them along with the thread ID in the browser's
localStorage. Let me break it down for you:-
Extract Thread ID:
-
- User
somehow the name capturing part gets stuck...and the window will not close.
- Assistant
- User
now I get two messages saying no usernames captured.
I don't understand why it is not working. The old function works fine.
// Function to extract usernames from the modal function extractUsernames() { const names = []; const ratingWindow = document.querySelector('#fwin_viewratings .floatwrap .list'); if (ratingWindow) { const rows = ratingWindow.querySelectorAll('tbody tr'); rows.forEach(row => { const nameCell = row.querySelector('td a'); if (nameCell) { names.push(nameCell.textContent.trim()); } }); } // Show the captured usernames for 3 seconds showTemporaryMessage('Captured Usernames: ' + (names.length > 0 ? names.join(', ') : 'None'), 3000); return names; } - Assistant
- User
it is still not getting the usernames. Moreover, it falls to close the window.
- Assistant
- User
great...we are almost there!
it is able to highlight username when url is in the following format. https://www.jkforum.net/p/forum.php?mod=viewthread&tid=19465032&extra=page%3D1&ordertype=0&page=1 but it fails to highlight the username when it is in this format. https://www.jkforum.net/p/thread-19465032-1-1.html both urls above goes to the same page. - Assistant
- User
it still doesn't highlight user when url is in thread-XXXXX URL structure. Perhaps it highlight function is not run immediately after usernames are captured.
- Assistant
- User
- remove the message for showing the username.
- make it run only if
- Assistant
- User
correction... find this element and make sure uid=1698412 before it runs the script.
- Assistant
- User
correction! instead of uid=1698412 triggering the script. The title of this element has to equal to maestro in order to run the script. maestro
- Assistant
- User
is there a way to speed up the name collection?
- Assistant
- User
is it possible not to load the window and still capture the names?
- Assistant
- User
it is not working...but thanks anyway. I think it will stop at the previous script.
- Assistant
Understood, returning to and optimizing for the approach that uses the modal should work well within the constraints you're encountering. Keeping the script functioning as intended with the modal ensures you can collect and utilize the required data within the known user flows and UI structures.
If you need further adjustments or run into challenges later on, feel free to revisit and consider other potential enhancements, or reach out again for help. Best of luck with your userscript - it sounds like an effective tool for your needs!