I want to add a condition for adding the buttons. Only add the buttons when the address link consist of the word "thread". For example https://141love.net/thread-3217887 . Please see the script below.
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.5
// @description Automatically rate posts in a forum thread and handle already rated posts
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
code
// Function to update input values in the rate form and click the correct submit button
function updateInputsAndSubmit() {
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
if (score1 && score2 && score4) {
// Set the desired values
score1.value = 2;
score2.value = 20;
score4.value = 2;
console.log('Values set. Attempting to click the correct submit button.');
// Find and click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => {
submitButton.click();
}, 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200); // Retry if the button isn't found
}
} else {
console.log('One or more inputs not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200);
}
}
// Function to close the window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected. Updating inputs...');
setTimeout(updateInputsAndSubmit, 500);
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
// Find all rate buttons and click them one by one
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
container.appendChild(autoButton);
}
}
// Add the automation buttons on page load
window.addEventListener('load', () => {
const mainButtonStyles = {
backgroundColor: '#FE9500',
color: 'white',
border: 'none',
borderRadius: '4px',
padding: '7px 10px',
fontSize: '14.5px',
cursor: 'pointer',
margin: '0px'
};
const scrollTopButtonStyles = {
backgroundColor: '#F4F4F4', // Change color for the button in #scrolltop
color: '#BBBBBB', // Text color
borderTop: '1px solid #BBBBBB',
borderLeft: '0px solid #BBBBBB',
borderBottom: '0px solid #BBBBBB',
borderRight: '0px solid #BBBBBB',
borderRadius: '0px',
padding: '7px 7px',
fontSize: '11px',
fontWeight: 'bold', // Set font weight to bold
cursor: 'pointer',
margin: '0px',
writingMode: 'horizontal-lr' // Ensures horizontal text
};
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', mainButtonStyles);
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', scrollTopButtonStyles); // Use different styles here
// Start observing the document for rate windows
observer.observe(document.body, {
childList: true,
subtree: true
});
});
})();
Assistant
To add the condition that ensures buttons are only added if the URL contains the word "thread", you can use window.location.href to check the current URL. Here's how you can integrate that into your existing script:
Check if the URL contains "thread" before adding the buttons.
Wrap the button creation part of your code with this condition.
Here's the updated script with the additional condition:
javascript
User
I would like to add color change when mouse hovers over the button for the both the background and word for the buttons.
Assistant
To add hover effects that change the background color and text color of the buttons, you can use CSS. Specifically, you can set the :hover state for the buttons you create. Here’s how to implement this in your existing script:
Create a unique class for the buttons (e.g., .auto-rate-button).
Apply CSS styles for the normal state and the hover state.
Here’s the updated version of your script:
javascript
User
Colors don't change when mouse hovers over the buttons. Moreover, I would like different color change for each button.
Assistant
To achieve different hover effects for each button and ensure that they change colors correctly, we can define specific hover styles for each button. Instead of applying a general hover class, we will directly use unique classes or identifiers for each button, so we can control the hover state individually.
Here's an updated version of your script that implements this idea:
javascript
User
the hover colors covers the starting colors and they won't change.
Assistant
To ensure that the hover colors change correctly without overriding the initial styles indefinitely, we should adjust the way we handle hover states by using CSS. We can create specific styles for the buttons and use CSS to change the hover state instead of trying to set hover styles dynamically in JavaScript.
By using CSS classes for both the default and hover states, we will meet your requirement more effectively. Below is the updated version of your userscript:
javascript
User
I only want a border top for the the scroll top button. How do I change it?
Assistant
To add a border only to the top of the scroll top button while removing any other border settings (left, bottom, right), you simply need to adjust the CSS for that button. You can specify the border-top property and set the other borders to none.
Here is the updated part of your userscript to reflect this change:
javascript
// CSS for button styling and hover effects
const style = document.createElement('style');
style.textContent = `
.main-button {
background-color: #FE9500;
color: white;
border: none; /* No border */
border-radius: 4px;
padding: 7px 10px;
font-size: 14.5px;
cursor: pointer;
margin: 0px;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.main-button:hover {
background-color: #FF7F00; /* Change background color on hover */
color: #FFF; /* Change text color on hover */
}
.scroll-top-button {
background-color: #F4F4F4;
color: #BBBBBB;
border-top: 1px solid #BBBBBB; /* Only add border to the top */
border-left: none; /* No border on left */
border-bottom: none; /* No border on bottom */
border-right: none; /* No border on right */
border-radius: 0px;
padding: 7px 7px;
font-size: 11px;
font-weight: bold;
cursor: pointer;
margin: 0px;
writing-mode: horizontal-lr;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.scroll-top-button:hover {
background-color: #D4D4D4; /* Change background color on hover */
color: #222; /* Change text color on hover */
}
`;
document.head.appendChild(style);
Key Changes:
In the .scroll-top-button class, I've set border-top: 1px solid #BBBBBB; to add a solid border at the top.
The borders on the left, bottom, and right are explicitly set to none, ensuring no border is applied to those sides.
With these changes, your scroll top button will have a border only at the top, giving it the desired appearance you wanted.
User
I have made this adjustment.
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.8
// @description Automatically rate posts in a forum thread and handle already rated posts
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
code
Assistant
Your updated userscript looks good overall, but you've mentioned a couple of specific changes regarding hover styles that you want to implement for the buttons. It appears you want the hover effect on the "scroll-top-button" to be visually distinctive but still maintain its default state color.
Currently, the hover color for the scroll-top-button is set to the same as its default background color, which effectively means there will be no visible change when hovering over it. Below, I will adjust that so that the hover effect is noticeable.
Here's your userscript with the relevant change applied:
javascript
User
There is no need to change it for now. I would like to keep my version.
Assistant
Absolutely! It's perfectly fine to keep your current version as it suits your needs. If you have any other questions or need assistance in the future—whether it's fine-tuning your script, adding new features, or addressing any issues—feel free to reach out.
Your code looks very functional, and maintaining it as it is will keep it lightweight and easy to understand. Don't hesitate to ask if you want to explore new features or modifications later on! Happy coding!
User
Now I would like to differentiate the rating based on the name in div.authi a.xw1 . For the name Juven and 力王ER, the score values should be
score1.value = 5;
score2.value = 50;
score4.value = 5;
Assistant
To differentiate the scoring based on the username found in the div.authi a.xw1 elements, you can modify the updateInputsAndSubmit function in your script. You will need to check the text of the user link against the names you want to target (in this case, "Juven" and "力王ER") and set the score values accordingly.
Here's how you can modify your script:
javascript
User
It doesn't seem to work. This is the entire element for the thread.
Assistant
Thank you for providing the detailed HTML structure for the posts in the thread. It seems that the username you want to target (e.g., "Juven" and "力王ER") is located within the div.authi a.xw1 element for each post. To ensure that your script works correctly, you can enhance its logic to:
Loop through the posts in the thread.
Extract the username for each post.
Set the score values based on the username.
Here's the updated code that should work given the structure you've provided:
javascript
User
the scores remains unchange for Juven.
Assistant
To ensure that the script correctly assigns scores based on the username "Juven" and works properly, we need to make sure we're correctly identifying the relevant post element when the rating window appears.
From the HTML structure you've provided and the requirements you've outlined, it appears that the script needs to correctly gather the user context for when it's opened to submit ratings corresponding to multiple users.
Updated Code:
Here is an improved version of the userscript that correctly identifies and applies scores based on usernames in the forum posts:
javascript
User
As you can see, it doesn't give the right score.
Assistant
Based on the HTML structure you've provided and your requirements, it looks like there are nested elements which may not be correctly accessed when trying to get the user's name. The specific context of the current rating (which user is being rated) needs to be correctly identified.
Updated Approach
You need to ensure that when the rating window opens, you're pulling the correct username from the post that you are currently attempting to rate. This can be done by modifying the script to look for the specific post element that corresponds to the rating we're trying to submit.
Since each "rate" button is associated with a specific post, we will use this context. We can pass the containing element of the rate button to the updateInputsAndSubmit function to ensure it retrieves the correct username.
Here’s an updated version of the script to ensure it works as expected:
javascript
User
It stops at opening the table.fwin
Assistant
Thank you for the clarification. If the script is stopping when trying to open the fwin_rate window (which is presumably the window for submitting a rating), it may be due to how the script is attempting to extract the username or how it interfaces with the DOM elements.
To troubleshoot the issue, I'll provide a more robust approach that ensures we clearly identify the correct post context when the rating window opens and handle potential errors more effectively.
Updated Script:
The following code adds additional checks and ensures we're targeting the right elements:
javascript
User
Great! it works!
Is it possible to lock https://141love.net/ from jumping to the top of the page when go back to the previous page?
Assistant
Yes, you can lock the scroll position on a webpage so that it doesn't jump to the top when navigating back to a previous page. This can be achieved by saving the scroll position and restoring it when the page loads.
You can use the sessionStorage or localStorage APIs to store the scroll position before leaving the page and then restore it when returning. Here’s how you can implement this in your userscript:
Save the scroll position when leaving the page or when navigating to a link within it.
Restore this scroll position when returning to the page.
Here's how you can integrate this functionality into your existing script:
Updated Userscript to Lock Scroll Position
javascript
User
it still jump to the top of the page when refreshed or returned.
Assistant
If the scroll position is still not being retained as expected when refreshing or navigating back, we'll need to ensure several factors are addressed correctly. Here’s an updated approach that ensures:
The scroll position is properly saved before navigation.
The saved scroll position is restored correctly upon page load.
Updated Userscript to Retain Scroll Position
Please try the revised script below, which integrates more robust scroll position management:
javascript
User
How do I switch off this function?
Assistant
If you'd like the ability to easily enable or disable the scroll position retention feature in your userscript, you can implement a toggle mechanism using a variable. This way, you can switch the scroll locking feature on and off without having to remove any code.
Steps to Add a Toggle Functionality:
Create a Variable: Use a boolean variable (e.g., enableScrollLock) to control whether the scroll locking functionality is active.
Wrap the Scroll Logic: Check this variable before saving or restoring the scroll position.
Add a Toggle Button: You can add a button to the user interface that will allow you to toggle this functionality on and off.
Here's how you can implement these changes:
Updated Script with Toggle Functionality
javascript
User
can you put this button on every page of the website?
Assistant
To place the toggle button on every page of the website, you need to ensure the button is added to the DOM every time a relevant page loads. You can achieve this by including the button in the general setup of your userscript without placing it within any specific content area related to the thread.
Updated Script with Global Toggle Button
Here's how to modify the userscript so that the toggle button is displayed on every page of the website:
javascript
Not yet included in a published release.
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.5
// @description Automatically rate posts in a forum thread and handle already rated posts
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to update input values in the rate form and click the correct submit button
function updateInputsAndSubmit() {
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
if (score1 && score2 && score4) {
// Set the desired values
score1.value = 2;
score2.value = 20;
score4.value = 2;
console.log('Values set. Attempting to click the correct submit button.');
// Find and click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => {
submitButton.click();
}, 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200); // Retry if the button isn't found
}
} else {
console.log('One or more inputs not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200);
}
}
// Function to close the window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected. Updating inputs...');
setTimeout(updateInputsAndSubmit, 500);
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
// Find all rate buttons and click them one by one
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
container.appendChild(autoButton);
}
}
// Add the automation buttons on page load
window.addEventListener('load', () => {
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
const mainButtonStyles = {
backgroundColor: '#FE9500',
color: 'white',
border: 'none',
borderRadius: '4px',
padding: '7px 10px',
fontSize: '14.5px',
cursor: 'pointer',
margin: '0px'
};
const scrollTopButtonStyles = {
backgroundColor: '#F4F4F4',
color: '#BBBBBB',
borderTop: '1px solid #BBBBBB',
borderLeft: '0px solid #BBBBBB',
borderBottom: '0px solid #BBBBBB',
borderRight: '0px solid #BBBBBB',
borderRadius: '0px',
padding: '7px 7px',
fontSize: '11px',
fontWeight: 'bold',
cursor: 'pointer',
margin: '0px',
writingMode: 'horizontal-lr'
};
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', mainButtonStyles);
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', scrollTopButtonStyles);
// Start observing the document for rate windows
observer.observe(document.body, {
childList: true,
subtree: true
});
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
Changes Made:
Added a check for window.location.href.includes("thread") before adding the buttons. This way, the buttons will only be appended if the URL contains "thread".
If the condition is not met, a log will tell you that automation buttons will not be added.
This change ensures that your script behaves as expected and only activates in the correct context.
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.6
// @description Automatically rate posts in a forum thread and handle already rated posts
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to update input values in the rate form and click the correct submit button
function updateInputsAndSubmit() {
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
if (score1 && score2 && score4) {
// Set the desired values
score1.value = 2;
score2.value = 20;
score4.value = 2;
console.log('Values set. Attempting to click the correct submit button.');
// Find and click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => {
submitButton.click();
}, 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200); // Retry if the button isn't found
}
} else {
console.log('One or more inputs not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200);
}
}
// Function to close the window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected. Updating inputs...');
setTimeout(updateInputsAndSubmit, 500);
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
// Find all rate buttons and click them one by one
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = 'auto-rate-button'; // Add class for CSS styling
container.appendChild(autoButton);
}
}
// Add the automation buttons on page load
window.addEventListener('load', () => {
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
const mainButtonStyles = {
backgroundColor: '#FE9500',
color: 'white',
border: 'none',
borderRadius: '4px',
padding: '7px 10px',
fontSize: '14.5px',
cursor: 'pointer',
margin: '0px'
};
const scrollTopButtonStyles = {
backgroundColor: '#F4F4F4',
color: '#BBBBBB',
borderTop: '1px solid #BBBBBB',
borderLeft: '0px solid #BBBBBB',
borderBottom: '0px solid #BBBBBB',
borderRight: '0px solid #BBBBBB',
borderRadius: '0px',
padding: '7px 7px',
fontSize: '11px',
fontWeight: 'bold',
cursor: 'pointer',
margin: '0px',
writingMode: 'horizontal-lr'
};
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', mainButtonStyles);
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', scrollTopButtonStyles);
// Start observing the document for rate windows
observer.observe(document.body, {
childList: true,
subtree: true
});
// Add hover style for the buttons
const style = document.createElement('style');
style.textContent = `
.auto-rate-button:hover {
background-color: #FF7F00; /* Change background color on hover */
color: #FFFFFF; /* Change text color on hover */
}`;
document.head.appendChild(style);
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
Changes Made:
Hover Class: Added a class name auto-rate-button to the buttons during creation, which allows us to target them with CSS.
Added CSS Styles: Created a <style> element and added CSS rules for the hover effect, specifying what should happen when the mouse hovers over buttons (e.g., change the background color to a lighter orange and keep the text color white).
Customizations:
You can adjust the colors in the hover CSS to suit your design preferences.
The hover effect can be further customized with transitions for smoothness if you want to make it visually appealing (e.g., transition: background-color 0.3s, color 0.3s;).
Now, when a user hovers over the buttons, the background and text colors will change as specified.
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.7
// @description Automatically rate posts in a forum thread and handle already rated posts
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to update input values in the rate form and click the correct submit button
function updateInputsAndSubmit() {
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
if (score1 && score2 && score4) {
// Set the desired values
score1.value = 2;
score2.value = 20;
score4.value = 2;
console.log('Values set. Attempting to click the correct submit button.');
// Find and click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => {
submitButton.click();
}, 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200); // Retry if the button isn't found
}
} else {
console.log('One or more inputs not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200);
}
}
// Function to close the window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected. Updating inputs...');
setTimeout(updateInputsAndSubmit, 500);
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
// Find all rate buttons and click them one by one
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles, hoverStyles) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = 'auto-rate-button'; // Add class for basic styling
Object.assign(autoButton.style, hoverStyles); // Add hover styles dynamically
container.appendChild(autoButton);
}
}
// Add the automation buttons on page load
window.addEventListener('load', () => {
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
const mainButtonStyles = {
backgroundColor: '#FE9500',
color: 'white',
border: 'none',
borderRadius: '4px',
padding: '7px 10px',
fontSize: '14.5px',
cursor: 'pointer',
margin: '0px'
};
const scrollTopButtonStyles = {
backgroundColor: '#F4F4F4',
color: '#BBBBBB',
borderTop: '1px solid #BBBBBB',
borderLeft: '0px solid #BBBBBB',
borderBottom: '0px solid #BBBBBB',
borderRight: '0px solid #BBBBBB',
borderRadius: '0px',
padding: '7px 7px',
fontSize: '11px',
fontWeight: 'bold',
cursor: 'pointer',
margin: '0px',
writingMode: 'horizontal-lr'
};
// Define hover styles for both buttons
const mainButtonHoverStyles = {
transition: 'background-color 0.3s, color 0.3s',
backgroundColor: '#FF7F00', // Background color on hover
color: '#FFF' // Text color on hover
};
const scrollTopButtonHoverStyles = {
transition: 'background-color 0.3s, color 0.3s',
backgroundColor: '#D4D4D4', // Background color on hover
color: '#222' // Text color on hover
};
// Add buttons with individual hover effects
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', mainButtonStyles, mainButtonHoverStyles);
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', scrollTopButtonStyles, scrollTopButtonHoverStyles);
// Start observing the document for rate windows
observer.observe(document.body, {
childList: true,
subtree: true
});
// Add CSS for button hover effects globally (requires a general class)
const style = document.createElement('style');
style.textContent = `
.auto-rate-button:hover {
/* Hover effects can be controlled through JavaScript now */
}`;
document.head.appendChild(style);
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
Key Changes Made:
Individual Hover Styles: Each button now has unique hover styles defined directly when creating the button. Instead of using the same hover styles for both buttons, different hover colors have been applied:
Main Button: Changes to a brighter orange (hover color: #FF7F00, text color: #FFF).
Scroll Top Button: Changes to a light gray (hover color: #D4D4D4, text color: #222).
Transition Effects: A transition property has been added to make the color change smooth when hovering over the buttons.
CSS No Longer Required for Hover: The CSS for hover is controlled dynamically via inline styles when the buttons are created, allowing for easier customization.
Note:
This implementation requires that the styles change to take effect upon hover as set in the JavaScript; make sure the button is properly appended to the container and that there are no other conflicting styles that might prevent the hover styles from displaying correctly.
With this setup, hover effects should work as expected, and you can easily customize the colors as needed!
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.8
// @description Automatically rate posts in a forum thread and handle already rated posts
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to update input values in the rate form and click the correct submit button
function updateInputsAndSubmit() {
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
if (score1 && score2 && score4) {
// Set the desired values
score1.value = 2;
score2.value = 20;
score4.value = 2;
console.log('Values set. Attempting to click the correct submit button.');
// Find and click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => {
submitButton.click();
}, 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200); // Retry if the button isn't found
}
} else {
console.log('One or more inputs not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200);
}
}
// Function to close the window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected. Updating inputs...');
setTimeout(updateInputsAndSubmit, 500);
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
// Find all rate buttons and click them one by one
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles, buttonClass) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = buttonClass; // Add class for CSS styling
container.appendChild(autoButton);
}
}
// CSS for button styling and hover effects
const style = document.createElement('style');
style.textContent = `
.main-button {
background-color: #FE9500;
color: white;
border: none;
border-radius: 4px;
padding: 7px 10px;
font-size: 14.5px;
cursor: pointer;
margin: 0px;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.main-button:hover {
background-color: #FF7F00; /* Change background color on hover */
color: #FFF; /* Change text color on hover */
}
.scroll-top-button {
background-color: #F4F4F4;
color: #BBBBBB;
border: 1px solid #BBBBBB;
border-radius: 0px;
padding: 7px 7px;
font-size: 11px;
font-weight: bold;
cursor: pointer;
margin: 0px;
writing-mode: horizontal-lr;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.scroll-top-button:hover {
background-color: #D4D4D4; /* Change background color on hover */
color: #222; /* Change text color on hover */
}
`;
document.head.appendChild(style);
// Add the automation buttons on page load
window.addEventListener('load', () => {
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', {}, 'main-button');
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', {}, 'scroll-top-button');
// Start observing the document for rate windows
observer.observe(document.body, {
childList: true,
subtree: true
});
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
Key Changes Made:
CSS for Button Styles: Created separate CSS classes for each button's default and hover states. This includes:
.main-button for the main rate button, with hover styles defined under .main-button:hover.
.scroll-top-button for the scroll top button, also with hover styles defined under .scroll-top-button:hover.
Dynamic Class Assignment: When creating the buttons, specific class names are assigned instead of relying on inline styles for the hover state. This makes it easier to manage and visually consistent.
Smooth Transitions: Both button classes have a CSS transition property that allows for smooth changes when the hover state is activated.
This approach ensures that your buttons change colors correctly on hover without losing their initial styles, providing a better user experience. Feel free to adjust the colors as per your design preferences!
// Function to update input values in the rate form and click the correct submit button
function updateInputsAndSubmit() {
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
if (score1 && score2 && score4) {
// Set the desired values
score1.value = 2;
score2.value = 20;
score4.value = 2;
console.log('Values set. Attempting to click the correct submit button.');
// Find and click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => {
submitButton.click();
}, 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200); // Retry if the button isn't found
}
} else {
console.log('One or more inputs not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200);
}
}
// Function to close the window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected. Updating inputs...');
setTimeout(updateInputsAndSubmit, 500);
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
// Find all rate buttons and click them one by one
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles, buttonClass) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = buttonClass; // Add class for CSS styling
container.appendChild(autoButton);
}
}
// CSS for button styling and hover effects
const style = document.createElement('style');
style.textContent = `
.main-button {
background-color: #FE9510;
color: white;
border: none; /* No border */
border-radius: 4px;
padding: 7px 10px;
font-size: 14.5px;
cursor: pointer;
margin: 0px;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.main-button:hover {
background-color: #E78F1B; /* Change background color on hover */
color: #FFF; /* Change text color on hover */
}
.scroll-top-button {
background-color: #F4F4F4;
color: #BBBBBB;
border-top: 1px solid #BBBBBB; /* Only add border to the top */
border-left: none; /* No border on left */
border-bottom: none; /* No border on bottom */
border-right: none; /* No border on right */
border-radius: 0px;
padding: 7px 7px;
font-size: 11px;
font-weight: bold;
cursor: pointer;
margin: 0px;
writing-mode: horizontal-lr;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.scroll-top-button:hover {
background-color: #F4F4F4; /* Change background color on hover */
color: #7DA0CC; /* Change text color on hover */
}
`;
document.head.appendChild(style);
// Add the automation buttons on page load
window.addEventListener('load', () => {
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', {}, 'main-button');
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', {}, 'scroll-top-button');
// Start observing the document for rate windows
observer.observe(document.body, {
childList: true,
subtree: true
});
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.9
// @description Automatically rate posts in a forum thread and handle already rated posts
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to update input values in the rate form and click the correct submit button
function updateInputsAndSubmit() {
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
if (score1 && score2 && score4) {
// Set the desired values
score1.value = 2;
score2.value = 20;
score4.value = 2;
console.log('Values set. Attempting to click the correct submit button.');
// Find and click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => {
submitButton.click();
}, 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200); // Retry if the button isn't found
}
} else {
console.log('One or more inputs not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200);
}
}
// Function to close the window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected. Updating inputs...');
setTimeout(updateInputsAndSubmit, 500);
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
// Find all rate buttons and click them one by one
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles, buttonClass) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = buttonClass; // Add class for CSS styling
container.appendChild(autoButton);
}
}
// CSS for button styling and hover effects
const style = document.createElement('style');
style.textContent = `
.main-button {
background-color: #FE9510;
color: white;
border: none; /* No border */
border-radius: 4px;
padding: 7px 10px;
font-size: 14.5px;
cursor: pointer;
margin: 0px;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.main-button:hover {
background-color: #E78F1B; /* Change background color on hover */
color: #FFF; /* Change text color on hover */
}
.scroll-top-button {
background-color: #F4F4F4;
color: #BBBBBB;
border-top: 1px solid #BBBBBB; /* Only add border to the top */
border-left: none; /* No border on left */
border-bottom: none; /* No border on bottom */
border-right: none; /* No border on right */
border-radius: 0px;
padding: 7px 7px;
font-size: 11px;
font-weight: bold;
cursor: pointer;
margin: 0px;
writing-mode: horizontal-lr;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.scroll-top-button:hover {
background-color: #E8E8E8; /* Change background color on hover */
color: #7DA0CC; /* Change text color on hover */
}
`;
document.head.appendChild(style);
// Add the automation buttons on page load
window.addEventListener('load', () => {
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', {}, 'main-button');
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', {}, 'scroll-top-button');
// Start observing the document for rate windows
observer.observe(document.body, {
childList: true,
subtree: true
});
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
Changes Made:
scroll-top-button:hover Background Color:
The hover background color for the scroll-top-button has been set to a light gray (#E8E8E8) instead of using the same color as its default state. This will allow users to see the change when they hover over the button.
With these changes, you will have a clear hover effect on the "scroll-top-button," enhancing the user interaction experience on your page. You can further customize the colors to fit your theme!
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.10
// @description Automatically rate posts in a forum thread and handle already rated posts
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to update input values in the rate form and click the correct submit button
function updateInputsAndSubmit() {
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
// Get the username from the relevant element
const usernameElement = document.querySelector('div.authi a.xw1');
const username = usernameElement ? usernameElement.innerText.trim() : '';
// Determine scores based on the username
if (username === "Juven" || username === "力王ER") {
// Set scores for specific usernames
score1.value = 5;
score2.value = 50;
score4.value = 5;
} else {
// Default score values (you can adjust these as needed)
score1.value = 2;
score2.value = 20;
score4.value = 2;
}
console.log(`Setting scores for user: ${username}`);
console.log('Values set. Attempting to click the correct submit button.');
// Find and click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => {
submitButton.click();
}, 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200); // Retry if the button isn't found
}
}
// Function to close the window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected. Updating inputs...');
setTimeout(updateInputsAndSubmit, 500);
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
// Find all rate buttons and click them one by one
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles, buttonClass) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = buttonClass; // Add class for CSS styling
container.appendChild(autoButton);
}
}
// CSS for button styling and hover effects
const style = document.createElement('style');
style.textContent = `
.main-button {
background-color: #FE9510;
color: white;
border: none; /* No border */
border-radius: 4px;
padding: 7px 10px;
font-size: 14.5px;
cursor: pointer;
margin: 0px;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.main-button:hover {
background-color: #E78F1B; /* Change background color on hover */
color: #FFF; /* Change text color on hover */
}
.scroll-top-button {
background-color: #F4F4F4;
color: #BBBBBB;
border-top: 1px solid #BBBBBB; /* Only add border to the top */
border-left: none; /* No border on left */
border-bottom: none; /* No border on bottom */
border-right: none; /* No border on right */
border-radius: 0px;
padding: 7px 7px;
font-size: 11px;
font-weight: bold;
cursor: pointer;
margin: 0px;
writing-mode: horizontal-lr;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.scroll-top-button:hover {
background-color: #F4F4F4; /* Change background color on hover */
color: #7DA0CC; /* Change text color on hover */
}
`;
document.head.appendChild(style);
// Add the automation buttons on page load
window.addEventListener('load', () => {
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', {}, 'main-button');
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', {}, 'scroll-top-button');
// Start observing the document for rate windows
observer.observe(document.body, {
childList: true,
subtree: true
});
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
Key Changes Made:
Username Retrieval: Added code to retrieve the username from the div.authi a.xw1 element. It uses innerText to get the displayed name.
Conditional Score Assignment:
After retrieving the username, it checks if it matches "Juven" or "力王ER".
This modified logic ensures that the rating scores are adjusted based on the user's name whenever the rate form is accessed. You can further customize the usernames and scores as needed. If you have any other questions or further adjustments, feel free to ask!
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.11
// @description Automatically rate posts in a forum thread and handle already rated posts
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to update input values in the rate form and click the correct submit button
function updateInputsAndSubmit(postElement) {
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
// Get the username from the relevant element
const usernameElement = postElement.querySelector('div.authi a.xw1');
const username = usernameElement ? usernameElement.innerText.trim() : '';
// Determine scores based on the username
if (username === "Juven" || username === "力王ER") {
// Set scores for specific usernames
score1.value = 5;
score2.value = 50;
score4.value = 5;
} else {
// Default score values
score1.value = 2;
score2.value = 20;
score4.value = 2;
}
console.log(`Setting scores for user: ${username}`);
console.log('Values set. Attempting to click the correct submit button.');
// Find and click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => {
submitButton.click();
}, 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200); // Retry if the button isn't found
}
}
// Function to close the window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected. Updating inputs...');
setTimeout(() => {
// Call the update function on all post elements
const posts = document.querySelectorAll('#postlist .plhin');
posts.forEach(post => {
updateInputsAndSubmit(post);
});
}, 500);
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
// Find all rate buttons and click them one by one
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles, buttonClass) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = buttonClass; // Add class for CSS styling
container.appendChild(autoButton);
}
}
// CSS for button styling and hover effects
const style = document.createElement('style');
style.textContent = `
.main-button {
background-color: #FE9510;
color: white;
border: none; /* No border */
border-radius: 4px;
padding: 7px 10px;
font-size: 14.5px;
cursor: pointer;
margin: 0px;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.main-button:hover {
background-color: #E78F1B; /* Change background color on hover */
color: #FFF; /* Change text color on hover */
}
.scroll-top-button {
background-color: #F4F4F4;
color: #BBBBBB;
border-top: 1px solid #BBBBBB; /* Only add border to the top */
border-left: none; /* No border on left */
border-bottom: none; /* No border on bottom */
border-right: none; /* No border on right */
border-radius: 0px;
padding: 7px 7px;
font-size: 11px;
font-weight: bold;
cursor: pointer;
margin: 0px;
writing-mode: horizontal-lr;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.scroll-top-button:hover {
background-color: #F4F4F4; /* Change background color on hover */
color: #7DA0CC; /* Change text color on hover */
}
`;
document.head.appendChild(style);
// Add the automation buttons on page load
window.addEventListener('load', () => {
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', {}, 'main-button');
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', {}, 'scroll-top-button');
// Start observing the document for rate windows
observer.observe(document.body, {
childList: true,
subtree: true
});
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
Key Changes Explained:
Username Extraction from Each Post: The updateInputsAndSubmit function is modified to accept a postElement argument. This argument is the individual post element containing the username.
User Score Logic: The function checks the username from the authi div inside each postElement and updates the scores based on whether the username matches "Juven" or "力王ER".
Looping Through Posts: When a rate window is detected, instead of just updating one post, the script loops through all posts (#postlist .plhin) and applies the relevant scoring logic to each post.
This approach ensures that the scores are differentiated based on the specific usernames you are targeting. Let me know if you encounter any issues or need further customization!
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.12
// @description Automatically rate posts in a forum thread and handle already rated posts
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Function to update input values in the rate form and click the correct submit button
function updateInputsAndSubmit() {
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
// Get the username from the relevant element
const usernameElements = document.querySelectorAll('div.authi a.xw1');
let currentUsername = '';
// Find the username of the post being rated
usernameElements.forEach((element) => {
if (element.closest('.plhin')) {
currentUsername = element.innerText.trim();
}
});
// Determine scores based on the username
if (currentUsername === "Juven" || currentUsername === "力王ER") {
// Set scores for specific usernames
score1.value = 5;
score2.value = 50;
score4.value = 5;
} else {
// Default score values
score1.value = 2;
score2.value = 20;
score4.value = 2;
}
console.log(`Setting scores for user: ${currentUsername}`);
console.log('Values set. Attempting to click the correct submit button.');
// Find and click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => {
submitButton.click();
}, 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200); // Retry if the button isn't found
}
}
// Function to close the window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected. Updating inputs...');
setTimeout(updateInputsAndSubmit, 500);
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
// Find all rate buttons and click them one by one
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles, buttonClass) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = buttonClass; // Add class for CSS styling
container.appendChild(autoButton);
}
}
// CSS for button styling and hover effects
const style = document.createElement('style');
style.textContent = `
.main-button {
background-color: #FE9510;
color: white;
border: none; /* No border */
border-radius: 4px;
padding: 7px 10px;
font-size: 14.5px;
cursor: pointer;
margin: 0px;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.main-button:hover {
background-color: #E78F1B; /* Change background color on hover */
color: #FFF; /* Change text color on hover */
}
.scroll-top-button {
background-color: #F4F4F4;
color: #BBBBBB;
border-top: 1px solid #BBBBBB; /* Only add border to the top */
border-left: none; /* No border on left */
border-bottom: none; /* No border on bottom */
border-right: none; /* No border on right */
border-radius: 0px;
padding: 7px 7px;
font-size: 11px;
font-weight: bold;
cursor: pointer;
margin: 0px;
writing-mode: horizontal-lr;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.scroll-top-button:hover {
background-color: #F4F4F4; /* Change background color on hover */
color: #7DA0CC; /* Change text color on hover */
}
`;
document.head.appendChild(style);
// Add the automation buttons on page load
window.addEventListener('load', () => {
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', {}, 'main-button');
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', {}, 'scroll-top-button');
// Start observing the document for rating windows
observer.observe(document.body, {
childList: true,
subtree: true
});
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
Key Changes:
Username Extraction Logic:
Loop Through All Username Elements: The script now loops through all credentials (div.authi a.xw1) at the time of each rating session.
Store Current Username: It retrieves the username of the post that is being rated when the rating window opens, ensuring scores are set correctly based on the identified user.
Triggering Score Assignment: The assignment of score values now happens based on the current user in the context of the post being rated.
How It Works:
When the rate window opens, the script extracts the username and checks if it matches either "Juven" or "力王ER".
Depending on the username, it assigns the respective scores.
The function iterates through the required elements and ensures the correct user context is used.
Debugging:
If it still doesn't work, consider adding console log statements to debug and ensure the username is being extracted correctly at different points in the script.
You should check the console in your web browser (typically opened with F12) for any logs and errors that can help pinpoint where the flow might be breaking down.
Let me know if you need further adjustments or more detailed troubleshooting!
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.13
// @description Automatically rate posts in a forum thread and handle already rated posts
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Function to update input values in the rate form based on the username
function updateInputsAndSubmit(postElement) {
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
// Get the username from the relevant element within the specific post
const usernameElement = postElement.querySelector('div.authi a.xw1');
// Ensure we have a username and trim it
const username = usernameElement ? usernameElement.innerText.trim() : '';
// Determine scores based on the username
if (username === "Juven" || username === "力王ER") {
// Set scores for specific usernames
score1.value = 5;
score2.value = 50;
score4.value = 5;
} else {
// Default score values
score1.value = 2;
score2.value = 20;
score4.value = 2;
}
console.log(`Setting scores for user: ${username}`);
console.log('Values set. Attempting to click the correct submit button.');
// Find and click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => {
submitButton.click();
}, 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(updateInputsAndSubmit, 200); // Retry if the button isn't found
}
}
// Function to close the rating window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected. Updating inputs...');
const postElement = document.querySelector('.plhin .authi'); // Get the first post that opened the rate window
if (postElement) {
updateInputsAndSubmit(postElement.closest('.plhin')); // Pass the containing element to the function
}
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
// Find all rate buttons and click them one by one
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles, buttonClass) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = buttonClass; // Add class for CSS styling
container.appendChild(autoButton);
}
}
// CSS for button styling and hover effects
const style = document.createElement('style');
style.textContent = `
.main-button {
background-color: #FE9510;
color: white;
border: none; /* No border */
border-radius: 4px;
padding: 7px 10px;
font-size: 14.5px;
cursor: pointer;
margin: 0px;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.main-button:hover {
background-color: #E78F1B; /* Change background color on hover */
color: #FFF; /* Change text color on hover */
}
.scroll-top-button {
background-color: #F4F4F4;
color: #BBBBBB;
border-top: 1px solid #BBBBBB; /* Only add border to the top */
border-left: none; /* No border on left */
border-bottom: none; /* No border on bottom */
border-right: none; /* No border on right */
border-radius: 0px;
padding: 7px 7px;
font-size: 11px;
font-weight: bold;
cursor: pointer;
margin: 0px;
writing-mode: horizontal-lr;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.scroll-top-button:hover {
background-color: #F4F4F4; /* Change background color on hover */
color: #7DA0CC; /* Change text color on hover */
}
`;
document.head.appendChild(style);
// Add the automation buttons on page load
window.addEventListener('load', () => {
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', {}, 'main-button');
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', {}, 'scroll-top-button');
// Start observing the document for rating windows
observer.observe(document.body, {
childList: true,
subtree: true
});
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
Key Changes and Explanation:
Correct Username Extraction: The username is fetched from the post that has opened the rating window. This ensures the score gets set based on the post's author, i.e., "Juven" or "力王ER".
Post Element Identification During Rating: When the rating window opens, the script identifies the specific post it is associated with to assign the score correctly.
Use of Closest Element: The method closest('.plhin') is used to find the parent post element where the rate button was clicked. This ensures you are pulling from the correct context for each post.
Testing:
Ensure that the script runs after the webpage has fully loaded.
Click the rate button for a post authored by "Juven" or "力王ER".
Check the console for any log outputs related to score setting to ensure it is being processed correctly.
This structure should ensure the scores get correctly assigned per user context as intended. If you still encounter issues, please triple-check the exact structure of the HTML being loaded by the webpage to confirm that your script matches the expected document structure. Let me know if there are any further issues or adjustments needed!
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.14
// @description Automatically rate posts in a forum thread and handle already rated posts
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Function to update input values in the rate form based on the username from the post element
function updateInputsAndSubmit(button) {
// Get the post element associated with the clicked rate button
const postElement = button.closest('.plhin');
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
if (!postElement) {
console.error('Could not find the post element associated with this button.');
return;
}
// Get the username from the relevant element within the specific post
const usernameElement = postElement.querySelector('div.authi a.xw1');
// Ensure we have a username and trim it
const username = usernameElement ? usernameElement.innerText.trim() : '';
// Determine scores based on the username
if (username === "Juven" || username === "力王ER") {
// Set scores for specific usernames
score1.value = 5;
score2.value = 50;
score4.value = 5;
} else {
// Default score values
score1.value = 2;
score2.value = 20;
score4.value = 2;
}
console.log(`Setting scores for user: ${username}`);
console.log('Values set. Attempting to click the correct submit button.');
// Find and click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => {
submitButton.click();
}, 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(() => updateInputsAndSubmit(button), 200); // Retry if the button isn't found
}
}
// Function to handle the opening of the rating window
function handleRateButtonClick(event) {
const button = event.currentTarget; // The rate button that was clicked
setTimeout(() => updateInputsAndSubmit(button), 500); // Ensure we wait for the rating window to appear
}
// Function to close the rating window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected.');
// Might want to trigger updateInputsAndSubmit when the rate form is shown (handled inside handleRateButtonClick)
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
// Find all rate buttons and click them one by one
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles, buttonClass) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = buttonClass; // Add class for CSS styling
container.appendChild(autoButton);
}
}
// CSS for button styling and hover effects
const style = document.createElement('style');
style.textContent = `
.main-button {
background-color: #FE9510;
color: white;
border: none; /* No border */
border-radius: 4px;
padding: 7px 10px;
font-size: 14.5px;
cursor: pointer;
margin: 0px;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.main-button:hover {
background-color: #E78F1B; /* Change background color on hover */
color: #FFF; /* Change text color on hover */
}
.scroll-top-button {
background-color: #F4F4F4;
color: #BBBBBB;
border-top: 1px solid #BBBBBB; /* Only add border to the top */
border-left: none; /* No border on left */
border-bottom: none; /* No border on bottom */
border-right: none; /* No border on right */
border-radius: 0px;
padding: 7px 7px;
font-size: 11px;
font-weight: bold;
cursor: pointer;
margin: 0px;
writing-mode: horizontal-lr;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.scroll-top-button:hover {
background-color: #F4F4F4; /* Change background color on hover */
color: #7DA0CC; /* Change text color on hover */
}
`;
document.head.appendChild(style);
// Add the automation buttons on page load
window.addEventListener('load', () => {
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', {}, 'main-button');
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', {}, 'scroll-top-button');
// Start observing the document for rating windows
observer.observe(document.body, {
childList: true,
subtree: true
});
// Attach rate button click event listeners
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach(button => {
button.addEventListener('click', handleRateButtonClick); // Add click event for each rate button
});
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
Explanation of Changes:
Passing the Button Context: In the handleRateButtonClick function, we pass the clicked button to the updateInputsAndSubmit function to ensure the correct username is fetched from the associated post element.
Fetching the Correct Post: The script now retrieves the closest .plhin element associated with the clicked rate button, ensuring that we look for the username in the right context.
Event Binding to Rate Buttons: Instead of calling the rating update function at the time of checking for the fwin_rate window, we attach click event listeners to the rate buttons directly. This allows better control over when to retrieve the username and apply scores.
Improved Error Logging: Added console logging to assist in debugging and tracking the user context as the script runs.
Testing:
Test the script on your forum by clicking the rate buttons for posts authored by "Juven" to see if it correctly assigns the scores.
Ensure you're checking the console for any logs that can indicate the current state and any potential errors.
If the rating still does not reflect the expected scores, please verify that the correct post is being evaluated and that there are no discrepancies with how the usernames are represented in the DOM. Let me know how it works or if anything else needs to be adjusted!
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.15
// @description Automatically rate posts in a forum thread and handle already rated posts. Prevent scroll jumping on back navigation.
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Function to save the current scroll position
function saveScrollPosition() {
sessionStorage.setItem('scrollPosition', window.scrollY);
}
// Function to restore the scroll position
function restoreScrollPosition() {
const scrollPosition = sessionStorage.getItem('scrollPosition');
if (scrollPosition) {
window.scrollTo(0, parseInt(scrollPosition));
sessionStorage.removeItem('scrollPosition'); // Clear the saved position after restoring
}
}
// Function to update input values in the rate form based on the username from the post element
function updateInputsAndSubmit(button) {
const postElement = button.closest('.plhin');
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
// Get the username from the relevant element within the specific post
const usernameElement = postElement.querySelector('div.authi a.xw1');
// Ensure we have a username and trim it
const username = usernameElement ? usernameElement.innerText.trim() : '';
// Determine scores based on the username
if (username === "Juven" || username === "力王ER") {
// Set scores for specific usernames
score1.value = 5;
score2.value = 50;
score4.value = 5;
} else {
// Default score values
score1.value = 2;
score2.value = 20;
score4.value = 2;
}
console.log(`Setting scores for user: ${username}`);
console.log('Values set. Attempting to click the correct submit button.');
// Find and click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => {
submitButton.click();
}, 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(() => updateInputsAndSubmit(button), 200); // Retry if the button isn't found
}
}
// Function to handle the opening of the rating window
function handleRateButtonClick(event) {
const button = event.currentTarget; // The rate button that was clicked
setTimeout(() => updateInputsAndSubmit(button), 500); // Ensure we wait for the rating window to appear
}
// Function to close the rating window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected.');
// Could potentially do further actions here
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
// Find all rate buttons and click them one by one
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles, buttonClass) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = buttonClass; // Add class for CSS styling
container.appendChild(autoButton);
}
}
// CSS for button styling and hover effects
const style = document.createElement('style');
style.textContent = `
.main-button {
background-color: #FE9510;
color: white;
border: none; /* No border */
border-radius: 4px;
padding: 7px 10px;
font-size: 14.5px;
cursor: pointer;
margin: 0px;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.main-button:hover {
background-color: #E78F1B; /* Change background color on hover */
color: #FFF; /* Change text color on hover */
}
.scroll-top-button {
background-color: #F4F4F4;
color: #BBBBBB;
border-top: 1px solid #BBBBBB; /* Only add border to the top */
border-left: none; /* No border on left */
border-bottom: none; /* No border on bottom */
border-right: none; /* No border on right */
border-radius: 0px;
padding: 7px 7px;
font-size: 11px;
font-weight: bold;
cursor: pointer;
margin: 0px;
writing-mode: horizontal-lr;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.scroll-top-button:hover {
background-color: #F4F4F4; /* Change background color on hover */
color: #7DA0CC; /* Change text color on hover */
}
`;
document.head.appendChild(style);
// Add the automation buttons on page load
window.addEventListener('load', () => {
// Restore the scroll position if available
restoreScrollPosition();
// Store the scroll position before navigating away
window.addEventListener('beforeunload', saveScrollPosition);
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', {}, 'main-button');
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', {}, 'scroll-top-button');
// Start observing the document for rating windows
observer.observe(document.body, {
childList: true,
subtree: true
});
// Attach rate button click event listeners
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach(button => {
button.addEventListener('click', handleRateButtonClick); // Add click event for each rate button
});
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
// Function to save the current scroll position
function saveScrollPosition() {
sessionStorage.setItem('scrollPosition', window.scrollY);
}
// Function to restore the scroll position
function restoreScrollPosition() {
const scrollPosition = sessionStorage.getItem('scrollPosition');
if (scrollPosition) {
window.scrollTo(0, parseInt(scrollPosition));
sessionStorage.removeItem('scrollPosition'); // Clear the saved position after restoring
}
}
})();
Key Changes Made:
Scroll Position Management:
Saving the Scroll Position: The scroll position is saved by the saveScrollPosition function when a user leaves the page. It stores the current vertical scroll position in sessionStorage.
Restoring the Scroll Position: When the page loads, the restoreScrollPosition function checks sessionStorage for an existing scroll position and scrolls to that point if it exists.
Adding Events for Before Unload: The script listens for the beforeunload event to save the scroll position anytime the user navigates away from the page.
Summary:
With these changes, the userscript will retain the scroll position of the page when the user navigates away or goes back, preventing the page from jumping to the top.
Feel free to test the script, and let me know if you need any further adjustments or additional features!
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.16
// @description Automatically rate posts in a forum thread and handle already rated posts. Prevent scroll jumping on back navigation.
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Function to save the current scroll position to sessionStorage
function saveScrollPosition() {
sessionStorage.setItem('scrollPosition', window.scrollY);
console.log('Scroll position saved:', window.scrollY);
}
// Function to restore the scroll position from sessionStorage
function restoreScrollPosition() {
const scrollPosition = sessionStorage.getItem('scrollPosition');
if (scrollPosition) {
window.scrollTo(0, parseInt(scrollPosition));
console.log('Restoring scroll position to:', scrollPosition);
sessionStorage.removeItem('scrollPosition'); // Clear the position after restoring
}
}
// Function to update input values in the rate form based on the username from the post element
function updateInputsAndSubmit(button) {
const postElement = button.closest('.plhin'); // Get the closest post element
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
// Get the username from the relevant element within the post
const usernameElement = postElement.querySelector('div.authi a.xw1');
const username = usernameElement ? usernameElement.innerText.trim() : '';
// Determine scores based on the username
if (username === "Juven" || username === "力王ER") {
score1.value = 5;
score2.value = 50;
score4.value = 5;
} else {
score1.value = 2;
score2.value = 20;
score4.value = 2;
}
console.log(`Setting scores for user: ${username}`);
console.log('Values set. Attempting to click the correct submit button.');
// Click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => submitButton.click(), 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(() => updateInputsAndSubmit(button), 200); // Retry if the button isn't found
}
}
// Function to handle the opening of the rating window
function handleRateButtonClick(event) {
const button = event.currentTarget;
setTimeout(() => updateInputsAndSubmit(button), 500); // Wait for the window to fully open
}
// Function to close the rating window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected.');
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles, buttonClass) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = buttonClass; // Add class for CSS styling
container.appendChild(autoButton);
}
}
// CSS for button styling and hover effects
const style = document.createElement('style');
style.textContent = `
.main-button {
background-color: #FE9510;
color: white;
border: none; /* No border */
border-radius: 4px;
padding: 7px 10px;
font-size: 14.5px;
cursor: pointer;
margin: 0px;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.main-button:hover {
background-color: #E78F1B; /* Change background color on hover */
color: #FFF; /* Change text color on hover */
}
.scroll-top-button {
background-color: #F4F4F4;
color: #BBBBBB;
border-top: 1px solid #BBBBBB; /* Only add border to the top */
border-left: none; /* No border on left */
border-bottom: none; /* No border on bottom */
border-right: none; /* No border on right */
border-radius: 0px;
padding: 7px 7px;
font-size: 11px;
font-weight: bold;
cursor: pointer;
margin: 0px;
writing-mode: horizontal-lr;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.scroll-top-button:hover {
background-color: #F4F4F4; /* Change background color on hover */
color: #7DA0CC; /* Change text color on hover */
}
`;
document.head.appendChild(style);
// Add the automation buttons on page load
window.addEventListener('load', () => {
restoreScrollPosition(); // Restore scroll position
window.addEventListener('beforeunload', saveScrollPosition); // Save scroll position before navigation
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', {}, 'main-button');
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', {}, 'scroll-top-button');
// Start observing the document for rating windows
observer.observe(document.body, {
childList: true,
subtree: true
});
// Attach rate button click event listeners
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach(button => {
button.addEventListener('click', handleRateButtonClick); // Add click event for each rate button
});
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
Key Functionalities Explained:
Saving Scroll Position:
The saveScrollPosition function stores the current vertical scroll position in sessionStorage right before the page unloads (using the beforeunload event).
Restoring Scroll Position:
On page load, the restoreScrollPosition function retrieves the stored position from sessionStorage and scrolls to that specific location.
Logging Scroll Actions:
Console logs have been added to help track when the scroll position is saved and restored. You can monitor these logs in your browser's console (F12) to ensure it's functioning correctly.
Testing:
Navigate away from the page and then come back to it to see if the scroll position is retained as you expect.
Use the script in a thread that you’ve already interacted with and check the console logs for confirmation that the scroll position is stored and restored correctly.
This should work correctly for retaining the scroll position when you refresh or navigate back. Let me know if this resolves your issue or if you need further assistance!
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.17
// @description Automatically rate posts in a forum thread and handle already rated posts. Prevent scroll jumping on back navigation.
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Toggle variable for scroll locking functionality
let enableScrollLock = true; // Set to true to enable, or false to disable
// Function to save the current scroll position to sessionStorage
function saveScrollPosition() {
if (enableScrollLock) {
sessionStorage.setItem('scrollPosition', window.scrollY);
console.log('Scroll position saved:', window.scrollY);
}
}
// Function to restore the scroll position from sessionStorage
function restoreScrollPosition() {
if (enableScrollLock) {
const scrollPosition = sessionStorage.getItem('scrollPosition');
if (scrollPosition) {
window.scrollTo(0, parseInt(scrollPosition));
console.log('Restoring scroll position to:', scrollPosition);
sessionStorage.removeItem('scrollPosition'); // Clear the position after restoring
}
}
}
// Function to update input values in the rate form based on the username from the post element
function updateInputsAndSubmit(button) {
const postElement = button.closest('.plhin'); // Get the closest post element
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
// Get the username from the relevant element within the specific post
const usernameElement = postElement.querySelector('div.authi a.xw1');
const username = usernameElement ? usernameElement.innerText.trim() : '';
// Determine scores based on the username
if (username === "Juven" || username === "力王ER") {
score1.value = 5;
score2.value = 50;
score4.value = 5;
} else {
// Default score values
score1.value = 2;
score2.value = 20;
score4.value = 2;
}
console.log(`Setting scores for user: ${username}`);
console.log('Values set. Attempting to click the correct submit button.');
// Click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => submitButton.click(), 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(() => updateInputsAndSubmit(button), 200); // Retry if the button isn't found
}
}
// Function to handle rate button click
function handleRateButtonClick(event) {
const button = event.currentTarget; // The rate button that was clicked
setTimeout(() => updateInputsAndSubmit(button), 500); // Wait for the rating window to appear
}
// Function to close the rating window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected.');
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles, buttonClass) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = buttonClass; // Add class for CSS styling
container.appendChild(autoButton);
}
}
// Add a toggle button for enabling/disabling scroll lock
function addToggleScrollLockButton() {
const toggleButton = document.createElement('button');
toggleButton.innerText = `Toggle Scroll Lock (${enableScrollLock ? 'On' : 'Off'})`;
toggleButton.style.position = 'fixed'; // Positioning
toggleButton.style.top = '10px';
toggleButton.style.right = '10px';
toggleButton.style.zIndex = '9999'; // Over other elements
toggleButton.onclick = () => {
enableScrollLock = !enableScrollLock; // Toggle the state
toggleButton.innerText = `Toggle Scroll Lock (${enableScrollLock ? 'On' : 'Off'})`;
console.log(`Scroll Lock is now ${enableScrollLock ? 'enabled' : 'disabled'}`);
};
document.body.appendChild(toggleButton);
}
// CSS for button styling and hover effects
const style = document.createElement('style');
style.textContent = `
.main-button {
background-color: #FE9510;
color: white;
border: none; /* No border */
border-radius: 4px;
padding: 7px 10px;
font-size: 14.5px;
cursor: pointer;
margin: 0px;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.main-button:hover {
background-color: #E78F1B; /* Change background color on hover */
color: #FFF; /* Change text color on hover */
}
.scroll-top-button {
background-color: #F4F4F4;
color: #BBBBBB;
border-top: 1px solid #BBBBBB; /* Only add border to the top */
border-left: none; /* No border on left */
border-bottom: none; /* No border on bottom */
border-right: none; /* No border on right */
border-radius: 0px;
padding: 7px 7px;
font-size: 11px;
font-weight: bold;
cursor: pointer;
margin: 0px;
writing-mode: horizontal-lr;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.scroll-top-button:hover {
background-color: #F4F4F4; /* Change background color on hover */
color: #7DA0CC; /* Change text color on hover */
}
`;
document.head.appendChild(style);
// Add the automation buttons on page load
window.addEventListener('load', () => {
restoreScrollPosition(); // Restore scroll position if enabled
window.addEventListener('beforeunload', saveScrollPosition); // Save scroll position before navigation
// Check if the URL contains the word "thread"
if (window.location.href.includes("thread")) {
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', {}, 'main-button');
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', {}, 'scroll-top-button');
// Start observing the document for rating windows
observer.observe(document.body, {
childList: true,
subtree: true
});
// Attach rate button click event listeners
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach(button => {
button.addEventListener('click', handleRateButtonClick); // Add click event for each rate button
});
// Add toggle scroll lock button
addToggleScrollLockButton();
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
Key Changes:
Toggle Variable:
A boolean variable enableScrollLock controls whether the scroll locking feature is active.
Save and Restore Functions With Conditions:
In the saveScrollPosition and restoreScrollPosition, we check if enableScrollLock is true before executing the respective logic.
Toggle Button Functionality:
A button to enable or disable the scroll lock functionality is added to the DOM. Clicking this button toggles the enableScrollLock variable and updates the button text accordingly.
Summary:
This setup provides an easy way for you to control whether the scroll locking feature is active without modifying the core functionality of the script. Just click the toggle button to enable or disable the behavior whenever you wish.
Let me know if you need any more features or adjustments!
// ==UserScript==
// @name Auto-Rate Posts with Close Handling
// @namespace http://tampermonkey.net/
// @version 1.18
// @description Automatically rate posts in a forum thread and handle already rated posts. Prevent scroll jumping on back navigation.
// @author Your Name
// @match https://141love.net/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Toggle variable for scroll locking functionality
let enableScrollLock = true; // Set to true to enable, or false to disable
// Function to save the current scroll position to sessionStorage
function saveScrollPosition() {
if (enableScrollLock) {
sessionStorage.setItem('scrollPosition', window.scrollY);
console.log('Scroll position saved:', window.scrollY);
}
}
// Function to restore the scroll position from sessionStorage
function restoreScrollPosition() {
if (enableScrollLock) {
const scrollPosition = sessionStorage.getItem('scrollPosition');
if (scrollPosition) {
window.scrollTo(0, parseInt(scrollPosition));
console.log('Restoring scroll position to:', scrollPosition);
sessionStorage.removeItem('scrollPosition'); // Clear the position after restoring
}
}
}
// Function to create and append the toggle button for scroll lock
function addToggleScrollLockButton() {
const toggleButton = document.createElement('button');
toggleButton.innerText = `Toggle Scroll Lock (${enableScrollLock ? 'On' : 'Off'})`;
toggleButton.style.position = 'fixed'; // Positioning
toggleButton.style.top = '10px';
toggleButton.style.right = '10px';
toggleButton.style.zIndex = '9999'; // Ensure it appears above other content
toggleButton.style.padding = '5px 10px'; // Add some padding
toggleButton.style.cursor = 'pointer';
// Toggle functionality
toggleButton.onclick = () => {
enableScrollLock = !enableScrollLock; // Toggle the state
toggleButton.innerText = `Toggle Scroll Lock (${enableScrollLock ? 'On' : 'Off'})`;
console.log(`Scroll Lock is now ${enableScrollLock ? 'enabled' : 'disabled'}`);
};
document.body.appendChild(toggleButton);
}
// Function to update input values in the rate form based on the username from the post element
function updateInputsAndSubmit(button) {
const postElement = button.closest('.plhin'); // Get the closest post element
const score1 = document.querySelector('input#score1.px.z');
const score2 = document.querySelector('input#score2.px.z');
const score4 = document.querySelector('input#score4.px.z');
// Get the username from the relevant element within the specific post
const usernameElement = postElement.querySelector('div.authi a.xw1');
const username = usernameElement ? usernameElement.innerText.trim() : '';
// Determine scores based on the username
if (username === "Juven" || username === "力王ER") {
score1.value = 5;
score2.value = 50;
score4.value = 5;
} else {
// Default score values
score1.value = 2;
score2.value = 20;
score4.value = 2;
}
console.log(`Setting scores for user: ${username}`);
console.log('Values set. Attempting to click the correct submit button.');
// Click the correct submit button
const submitButton = document.querySelector('button.pn.pnc:not(.z)');
if (submitButton) {
setTimeout(() => submitButton.click(), 200);
} else {
console.log('Submit button not found. Retrying...');
setTimeout(() => updateInputsAndSubmit(button), 200); // Retry if the button isn't found
}
}
// Function to handle rate button click
function handleRateButtonClick(event) {
const button = event.currentTarget; // The rate button that was clicked
setTimeout(() => updateInputsAndSubmit(button), 500); // Wait for the rating window to appear
}
// Function to close the rating window if already rated
function closeFwinWindow() {
const closeButton = document.querySelector('button#closebtn.pn.pnc');
if (closeButton) {
console.log('Already rated window detected. Closing it.');
closeButton.click();
}
}
// Observe changes to detect when a rate form or already rated window is opened
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (node.id === 'fwin_rate') {
console.log('Rate window detected.');
}
if (node.tagName === 'TABLE' && node.classList.contains('fwin')) {
console.log('Already rated window detected. Attempting to close.');
setTimeout(closeFwinWindow, 500);
}
});
}
});
});
function automateRating() {
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach((button, index) => {
setTimeout(() => {
console.log(`Clicking rate button for post index ${index}`);
button.click();
}, index * 1000); // Delay each click by 1 second
});
}
// Create and append a button for starting the automation
function addAutomationButton(container, buttonText, styles, buttonClass) {
if (container) {
const autoButton = document.createElement('button');
autoButton.innerText = buttonText;
autoButton.onclick = automateRating;
Object.assign(autoButton.style, styles);
autoButton.className = buttonClass; // Add class for CSS styling
container.appendChild(autoButton);
}
}
// CSS for button styling and hover effects
const style = document.createElement('style');
style.textContent = `
.main-button {
background-color: #FE9510;
color: white;
border: none; /* No border */
border-radius: 4px;
padding: 7px 10px;
font-size: 14.5px;
cursor: pointer;
margin: 0px;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.main-button:hover {
background-color: #E78F1B; /* Change background color on hover */
color: #FFF; /* Change text color on hover */
}
.scroll-top-button {
background-color: #F4F4F4;
color: #BBBBBB;
border-top: 1px solid #BBBBBB; /* Only add border to the top */
border-left: none; /* No border on left */
border-bottom: none; /* No border on bottom */
border-right: none; /* No border on right */
border-radius: 0px;
padding: 7px 7px;
font-size: 11px;
font-weight: bold;
cursor: pointer;
margin: 0px;
writing-mode: horizontal-lr;
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
.scroll-top-button:hover {
background-color: #F4F4F4; /* Change background color on hover */
color: #7DA0CC; /* Change text color on hover */
}
`;
document.head.appendChild(style);
// Add the automation buttons on page load
window.addEventListener('load', () => {
restoreScrollPosition(); // Restore scroll position if enabled
window.addEventListener('beforeunload', saveScrollPosition); // Save scroll position before navigation
addToggleScrollLockButton(); // Add the toggle button for scroll lock
// Check if the URL contains the word "thread" to add specific buttons
if (window.location.href.includes("thread")) {
addAutomationButton(document.querySelector('div#pgt.pgs.cl'), '自動評分', {}, 'main-button');
addAutomationButton(document.querySelector('div#scrolltop'), '自動評分', {}, 'scroll-top-button');
// Start observing the document for rating windows
observer.observe(document.body, {
childList: true,
subtree: true
});
// Attach rate button click event listeners
const rateButtons = document.querySelectorAll('a.rateb');
rateButtons.forEach(button => {
button.addEventListener('click', handleRateButtonClick); // Add click event for each rate button
});
} else {
console.log('Not a thread page. Automation buttons will not be added.');
}
});
})();
Key Changes:
Toggle Button on Every Page:
The addToggleScrollLockButton() function is called in the page load event handler, which ensures it is added to every page of the website.
Button Functionality:
The toggle button is always present and can be used regardless of whether you're on a thread page.
Summary:
Now, this userscript will show the toggle button for enabling or disabling the scroll locking feature on every page of the forum, making it easily accessible for you to control the functionality.
You can test it by visiting various pages of https://141love.net/ to confirm that the button appears consistently. Let me know if you need further changes or additional features!