// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace
// @version 1.1
// @description Adds a button to activate the showWindow function on JK Forum pages
// @author Your Name
// @match *
// @grant none
// ==/UserScript==
// Function to extract TID from the URL
function getTID() {
const url = window.location.href;
const match = url.match(/thread-(\d{8})/);
return match ? match[1] : null;
}
// Function to get the image URL from the <meta property="og:image">
function getImgURL() {
const metaTag = document.querySelector('meta[property="og:image"]');
return metaTag ? metaTag.getAttribute('content') : null;
}
// Function to create and add the button
function addButton() {
const button = document.createElement('button');
button.innerText = 'Set as Cover';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
// Button click event
button.onclick = function() {
const tid = getTID();
const imgURL = getImgURL();
if (tid && imgURL) {
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
};
document.body.appendChild(button);
}
// Add button when the DOM is fully loaded
window.onload = addButton;
})();
Please help me add a function to change the fid to 234
this is an example of the element where the buttons are located.
Assistant
To modify the script you provided so that it changes the fid (forum ID) to 234 when the button is clicked, we can update the JavaScript to first locate the form or button that handles the submission and then adjust its action parameter to include the new fid.
Assuming that the button in the example you've given is part of a form where submissions happen, we can modify the onclick function to change the fid. Below is your updated UserScript:
javascript
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 1.2
// @description Adds a button to activate the showWindow function on JK Forum pages
// @author Your Name
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to extract TID from the URL
function getTID() {
const url = window.location.href;
const match = url.match(/thread-(\d{8})/);
return match ? match[1] : null;
}
// Function to get the image URL from the <meta property="og:image">
function getImgURL() {
const metaTag = document.querySelector('meta[property="og:image"]');
return metaTag ? metaTag.getAttribute('content') : null;
}
// Function to create and add the button
function addButton() {
const button = document.createElement('button');
button.innerText = 'Set as Cover';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
// Button click event
button.onclick = function() {
const tid = getTID();
const imgURL = getImgURL();
if (tid && imgURL) {
const newFid = 234; // New fid to set
const form = document.querySelector('form'); // Assuming there is a <form> to submit to
if (form) {
// Change the 'fid' in the action of the form or any hidden input within it
const fidInput = form.querySelector('input[name="fid"]');
if (fidInput) {
fidInput.value = newFid; // Change the fid input value
}
}
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
};
document.body.appendChild(button);
}
// Add button when the DOM is fully loaded
window.onload = addButton;
})();
User
how do i check if fid has changed to 234?
Assistant
To verify whether the fid has changed to 234 after clicking the button in your UserScript, you can use a few different methods. Here are a few options:
1. Use the Console
You can easily check the value of fid in your browser's JavaScript console. Here's how to do that:
Open Developer Tools:
Right-click on the page and select "Inspect" or "Inspect Element" (this varies by browser).
Go to the "Console" tab.
Check the Value:
In the console, you would run a command to check the value of the fid input. Assuming that fid is an input element named fid, you would run this command:
javascript
document.querySelector('input[name="fid"]').value
If the script has successfully changed it, this should return 234.
2. Add a Debug Log in the Script
You can modify your script to log the new value of fid to the console after it has been changed. Here’s how you can do that:
Update the onclick function in your script as follows:
javascript
button.onclick = function() {
const tid = getTID();
const imgURL = getImgURL();
if (tid && imgURL) {
const newFid = 234; // New fid to set
const form = document.querySelector('form'); // Assuming there is a <form> to submit to
if (form) {
// Change the 'fid' in the action of the form or any hidden input within it
const fidInput = form.querySelector('input[name="fid"]');
if (fidInput) {
fidInput.value = newFid; // Change the fid input value
console.log(`fid changed to: ${fidInput.value}`); // Log the new fid value
}
}
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
};
User
can you rewrite the script with option 3?
Assistant
Certainly! Below is your updated UserScript that includes an alert to show the new value of fid right after it’s changed to 234.
javascript
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 1.2
// @description Adds a button to activate the showWindow function on JK Forum pages
// @author Your Name
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to extract TID from the URL
function getTID() {
const url = window.location.href;
const match = url.match(/thread-(\d{8})/);
return match ? match[1] : null;
}
// Function to get the image URL from the <meta property="og:image">
function getImgURL() {
const metaTag = document.querySelector('meta[property="og:image"]');
return metaTag ? metaTag.getAttribute('content') : null;
}
// Function to create and add the button
function addButton() {
const button = document.createElement('button');
button.innerText = 'Set as Cover';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
// Button click event
button.onclick = function() {
const tid = getTID();
const imgURL = getImgURL();
if (tid && imgURL) {
const newFid = 234; // New fid to set
const form = document.querySelector('form'); // Assuming there is a <form> to submit to
if (form) {
// Change the 'fid' in the action of the form or any hidden input within it
const fidInput = form.querySelector('input[name="fid"]');
if (fidInput) {
fidInput.value = newFid; // Change the fid input value
alert(`fid changed to: ${fidInput.value}`); // Alert the new fid value
}
}
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
};
document.body.appendChild(button);
}
// Add button when the DOM is fully loaded
window.onload = addButton;
})();
User
I don't see the alert message.
Assistant
If you're not seeing the alert message when you click the "Set as Cover" button, there could be a couple of reasons for it. Let's troubleshoot and ensure that the script works as intended:
1. Check If the Button Is Clicked
First, ensure that the button is being clicked and that the script is executing. You can do this by adding some console.log statements to give visibility into what happens when the button is clicked.
Here's an updated version of your script with additional debugging information:
javascript
User
I understand why now. I don't want the fid change when the Set as Cover button is pressed. I want it happens when either the topicsubmit or postsave button is pressed.
Here is the element of where the buttons are located.
Assistant
Thank you for clarifying! If you want to change the fid value to 234 when either the "發表文章" (submit topic) button or the "儲存草稿" (save draft) button is pressed, we will modify the script to add event listeners to these buttons instead of when the "Set as Cover" button is clicked.
Updated UserScript
Here is your revised UserScript that will set the fid to 234 whenever either the topicsubmit button (for submitting a post) or the postsave button (for saving a draft) is clicked.
javascript
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 1.4
// @description Adds functionality to change fid to 234 when submitting or saving drafts on JK Forum pages
// @author Your Name
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to change the fid value
function setFidValueTo234() {
const fidInput = document.querySelector('input[name="fid"]');
if (fidInput) {
fidInput.value = 234; // Set fid to 234
alert(`fid changed to: ${fidInput.value}`); // Alert the new fid value
} else {
console.log("fid input element not found");
}
}
// Function to add event listeners to the submit and save buttons
function addButtonListeners() {
const submitButton = document.querySelector('button[name="topicsubmit"]');
const saveButton = document.querySelector('button[onclick*="postsave"]'); // Matching the button by its onclick
if (submitButton) {
submitButton.addEventListener('click', function() {
setFidValueTo234();
});
}
if (saveButton) {
saveButton.addEventListener('click', function() {
setFidValueTo234();
});
}
}
// Add listeners when the DOM is fully loaded
window.onload = addButtonListeners;
})();
User
I want to keep the Set as Cover button and its function.
Assistant
Sure! We can keep the "Set as Cover" button along with its functionality while also adding the ability to change the fid when either the "發表文章" (submit topic) button or the "儲存草稿" (save draft) button is pressed.
The updated script below includes both the "Set as Cover" functionality and the new behavior to change the fid when submitting or saving drafts.
Complete UserScript
javascript
User
I would like to remove the change fid function. However, I would like to run the Set as Cover function immedately after either the "發表文章" (submit topic) button or the "儲存草稿" (save draft) button is pressed.
Assistant
Certainly! You can modify the script so that the "Set as Cover" function runs immediately after either the "發表文章" (submit topic) button or the "儲存草稿" (save draft) button is pressed. We will remove the part that changes the fid and directly call the Set as Cover functionality from the respective button event handlers.
Updated UserScript
Here’s the modified script:
javascript
User
it seems like the TID and Image URL are not located when the button is pressed. Could you change if so that the function will run after the button is pressed, and the page is completely loaded?
Assistant
To ensure that the Set as Cover functionality executes only after the button is pressed and the page has fully loaded (which should happen after the form submission), we can utilize the MutationObserver API. This will allow us to observe changes to the DOM and react accordingly after the page is updated.
Here's how you can modify your script:
Updated UserScript
javascript
User
wait..I meant to say is when either the "發表文章" or the "儲存草稿" button is pressed, the new page will be loaded. When that happens, execute the setAsCover function.
Assistant
Thank you for the clarification! To ensure that the setAsCover function runs immediately after either the "發表文章" (Submit Topic) or "儲存草稿" (Save Draft) button is pressed—specifically on the newly loaded page—we can set up a mechanism to detect when the page is fully loaded.
We can use the window.onload event to trigger the setAsCover function once the page has fully loaded after the submission or save action. Since the buttons trigger page changes, we can use a setTimeout to check for the presence of the expected elements and call the setAsCover function accordingly.
Here's how to implement it:
Updated UserScript
javascript
User
It did not activate the setAsCover after the page is loaded.
Assistant
To ensure that the setAsCover function executes after the page has fully loaded following a form submission or button click, we need to slightly adjust our approach. Instead of waiting for the window to load again and using setTimeout, we can directly invoke the setAsCover function right after the typical page transition for form submissions.
Here's how we can implement this:
Each time the user clicks the submit or save button, we will rely on the normal behavior of form submissions that the browser implements, which automatically reloads or navigates to the next page.
We will add a check for the URL that ensures the right page is loaded and then call setAsCover.
Updated UserScript Implementation
Below is the revised script, which ensures that setAsCover is executed once the new page is loaded:
javascript
User
it tries to get TID and Image URL as soon as the 發表文章 button is pressed which are not available until it jumps to a new page.
Assistant
To ensure that the setAsCover function executes after the page has fully loaded (and after the browser navigates to the new URL from pressing the "發表文章" or "儲存草稿" buttons), you can directly tie the execution of the setAsCover function to the window.onload event. This means you'd want to keep a toggle or flag indicating if the last action was a form submission.
Here’s how to update the UserScript:
Listen for the button click events and set a flag.
Run the setAsCover function when the page is fully loaded, but only if the flag indicates it was a result of pressing the submit/save buttons.
Updated UserScript
javascript
User
it did not execute the setAsCover function.
Please note that when the 發表文章 is pressed, it will go to a new page. However, a window will pop up saying you have earn a certain point for a second. Is it affecting the execution of the setAsCover function?
Assistant
Yes, if there is an intermediate pop-up (like a modal or an alert) that appears right after pressing the "發表文章" (submit) button and before the new page fully loads, that could be interfering with the execution of the setAsCover function. This might prevent the script from executing as expected.
In scenarios where a pop-up blocks the loading of the new page, we can't directly rely on the window.onload event to execute our function immediately since it won't trigger until the pop-up is closed or dismissed.
Updated Approach
To handle this situation, we can use a combination of setInterval or setTimeout with a clear condition to check periodically for the presence of the necessary elements and run the setAsCover when they become available.
Here's the modified version of the UserScript:
javascript
User
it did not run the function after the pop-ups disappears.
Assistant
Given the issue you're facing where the setAsCover function is not executing after the pop-up disappears following button clicks—especially when navigating to a new page—we can approach the problem by checking for the correct element on the page after some time has passed.
Since the pop-up is likely interrupting the process, it's essential to adjust our approach to wait until the pop-up disappears and the new page loads before executing the script.
To implement this:
We'll wait for a few seconds after clicking either the "發表文章" (submit) or "儲存草稿" (save) buttons.
After that, we will check for the presence of specific elements that would indicate that the new page has loaded.
We will then call the setAsCover function accordingly.
Updated UserScript
Here's the revised version of the script that incorporates this approach:
javascript
User
it still didn't execute.
Assistant
I apologize for the continued issues. Let's refine the approach to ensure that the setAsCover function executes reliably after the new page has loaded, especially given that a pop-up message may be interfering.
Below, I'll outline a strategy to wait for the new page to load, with a focus on checking for the presence of elements that indicate the new content is fully rendered.
New Approach
Use a MutationObserver to detect changes in the DOM after clicking the "發表文章" or "儲存草稿" buttons. This way, we can respond immediately when the new page content replaces the old one.
Specifically look for a unique element on the new page that indicates the content has loaded.
Revised UserScript
Here's the updated script:
javascript
Not yet included in a published release.
Changes Made:
Change fid to 234: In the onclick function, we searched for an input field with the name fid. If it exists, we set its value to 234.
Assumption: I assumed that you have a <form> element and that the fid is either part of that form as a hidden input or directly modified. This could require adjusting to fit your specific HTML structure, as the exact location and method of changing the fid may differ based on the exact context.
Make sure to test the script on the JK Forum pages. You might need to check how the form and its inputs are structured to ensure the fid change works appropriately.
3. Use alert() for Immediate Feedback
If you prefer not to open the console every time and want immediate feedback, you can use an alert to show the value:
Replace your logging line with:
javascript
alert(`fid changed to: ${fidInput.value}`);
4. Check the Network Request (Advanced)
If the fid value is sent to the server in a network request (like a form submission), you can monitor that request:
In the Network tab of Developer Tools, perform the action that would include fid in the request.
Look for the request that is sent (it could be a POST or GET request).
Inspect the request payload or the query parameters to see if fid is included and check its value.
Conclusion
Choose the method that best fits your workflow. If you're debugging your script, using console.log() or alert() will likely be the quickest and easiest way to ensure that your changes take effect.
Key Additions:
An alert function was added after setting the fid value to 234. This will display a pop-up alert confirming the change to the user.
Testing the Script:
Install the script using a browser extension such as Tampermonkey.
Navigate to a compatible page on JK Forum.
Click the "Set as Cover" button.
You should see an alert pop up indicating that the fid has been changed to 234.
Just remember that using alert() for debugging is a straightforward but temporary measure. In a production environment, you would typically want to use logging instead of alerts for a better user experience.
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 1.3
// @description Adds a button to activate the showWindow function on JK Forum pages
// @author Your Name
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to extract TID from the URL
function getTID() {
const url = window.location.href;
const match = url.match(/thread-(\d{8})/);
return match ? match[1] : null;
}
// Function to get the image URL from the <meta property="og:image">
function getImgURL() {
const metaTag = document.querySelector('meta[property="og:image"]');
return metaTag ? metaTag.getAttribute('content') : null;
}
// Function to create and add the button
function addButton() {
const button = document.createElement('button');
button.innerText = 'Set as Cover';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
// Button click event
button.onclick = function() {
console.log("Button clicked"); // Debugging log
const tid = getTID();
const imgURL = getImgURL();
console.log(`TID: ${tid}, Image URL: ${imgURL}`); // Debugging log
if (tid && imgURL) {
const newFid = 234; // New fid to set
const form = document.querySelector('form'); // Assuming there is a <form> to submit to
if (form) {
// Change the 'fid' in the action of the form or any hidden input within it
const fidInput = form.querySelector('input[name="fid"]');
if (fidInput) {
fidInput.value = newFid; // Change the fid input value
alert(`fid changed to: ${fidInput.value}`); // Alert the new fid value
} else {
console.log("fid input element not found"); // Debugging log
}
} else {
console.log("Form element not found"); // Debugging log
}
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
};
document.body.appendChild(button);
}
// Add button when the DOM is fully loaded
window.onload = addButton;
})();
Troubleshooting Steps:
Console Logs:
Open the developer console in your browser (F12 or right-click and select "Inspect", then go to the "Console" tab).
Look for the message "Button clicked" when you click the button. If you see this, we know the click event is being triggered.
Check for TID and Image URL:
The log messages will show you the extracted TID and Image URL. If either of them is null, the alert won’t be triggered. Make sure these values are being correctly parsed from the URL and the <meta> tag.
Ensure the Form and fid Field Exist:
The script checks for the existence of a form and the fid input field. The console log messages will indicate whether these elements were found.
If you see "Form element not found" or "fid input element not found", your selector may need to be adjusted based on the actual structure of the webpage.
Adjust Selectors If Necessary:
If you've confirmed that the button click is registering but you're still not seeing the alert, ensure the selectors for the form and the fid input are correct based on the structure of the page. You may need to adjust them.
You can do this by inspecting the HTML structure directly in the developer tools to find the exact selectors to use.
After making any changes or performing the checks outlined above, try clicking the button again and see if the alert appears.
Key Changes Made:
Function to Change fid: I created a function setFidValueTo234 that changes the fid input to 234 and shows an alert.
Event Listeners for Buttons:
The script grabs the submit button (with name="topicsubmit") and the save button (with a dynamic onclick for saving drafts).
Each button is associated with an event listener that executes the setFidValueTo234 function when the buttons are clicked.
How to Test the Script:
Install the script using a browser extension like Tampermonkey.
Navigate to a page on JK Forum that contains the post submit and save buttons.
Click the "發表文章" button or the "儲存草稿" button.
You should see an alert indicating that the fid has been changed to 234.
This will ensure that the fid gets updated accordingly just before the form submission or draft saving occurs. If you still need further adjustments, feel free to ask!
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 1.5
// @description Adds a button to activate the showWindow function and changes fid to 234 on submit/save actions on JK Forum pages
// @author Your Name
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to extract TID from the URL
function getTID() {
const url = window.location.href;
const match = url.match(/thread-(\d{8})/);
return match ? match[1] : null;
}
// Function to get the image URL from the <meta property="og:image">
function getImgURL() {
const metaTag = document.querySelector('meta[property="og:image"]');
return metaTag ? metaTag.getAttribute('content') : null;
}
// Function to change the fid value
function setFidValueTo234() {
const fidInput = document.querySelector('input[name="fid"]');
if (fidInput) {
fidInput.value = 234; // Set fid to 234
alert(`fid changed to: ${fidInput.value}`); // Alert the new fid value
} else {
console.log("fid input element not found");
}
}
// Function to create and add the Set as Cover button
function addSetAsCoverButton() {
const button = document.createElement('button');
button.innerText = 'Set as Cover';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
// Button click event
button.onclick = function() {
const tid = getTID();
const imgURL = getImgURL();
if (tid && imgURL) {
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
};
document.body.appendChild(button);
}
// Function to add event listeners to the submit and save buttons
function addButtonListeners() {
const submitButton = document.querySelector('button[name="topicsubmit"]');
const saveButton = document.querySelector('button[onclick*="postsave"]'); // Matching the button by its onclick
if (submitButton) {
submitButton.addEventListener('click', function() {
setFidValueTo234();
});
}
if (saveButton) {
saveButton.addEventListener('click', function() {
setFidValueTo234();
});
}
}
// Add button and listeners when the DOM is fully loaded
window.onload = function() {
addSetAsCoverButton();
addButtonListeners();
};
})();
Explanation of the Script:
Set as Cover Button: The functionality for the "Set as Cover" button remains intact. It retrieves the TID and image URL, then calls the original showWindow() function.
Changing fid: The setFidValueTo234() function is called when either the "發表文章" button or the "儲存草稿" button is clicked. This updates the hidden fid input value to 234 and alerts the user.
Event Listeners: The script now listens for clicks on both the submit and save buttons and modifies the fid just before the actions occur.
Testing the Script:
Install the script using a browser extension like Tampermonkey.
Navigate to a JK Forum page with the available buttons.
Click the "Set as Cover" button to ensure it works as before.
Click the "發表文章" button and the "儲存草稿" button to confirm that the fid is successfully set to 234 and that an alert appears.
If you have any other modifications or need more assistance, feel free to ask!
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 1.6
// @description Adds a button to activate the showWindow function when submitting or saving on JK Forum pages
// @author Your Name
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to extract TID from the URL
function getTID() {
const url = window.location.href;
const match = url.match(/thread-(\d{8})/);
return match ? match[1] : null;
}
// Function to get the image URL from the <meta property="og:image">
function getImgURL() {
const metaTag = document.querySelector('meta[property="og:image"]');
return metaTag ? metaTag.getAttribute('content') : null;
}
// Function to invoke the Set as Cover functionality
function setAsCover() {
const tid = getTID();
const imgURL = getImgURL();
if (tid && imgURL) {
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
}
// Function to create and add the Set as Cover button
function addSetAsCoverButton() {
const button = document.createElement('button');
button.innerText = 'Set as Cover';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
// Button click event
button.onclick = setAsCover; // Set as Cover button functionality
document.body.appendChild(button);
}
// Function to add event listeners to the submit and save buttons
function addButtonListeners() {
const submitButton = document.querySelector('button[name="topicsubmit"]');
const saveButton = document.querySelector('button[onclick*="postsave"]'); // Matching the button by its onclick
const wrappedSetAsCover = function(event) {
setAsCover(); // Call Set as Cover function
// Keep the behavior of the original button (submit or save)
setTimeout(function() {
event.target.click();
}, 0);
};
if (submitButton) {
submitButton.addEventListener('click', wrappedSetAsCover);
}
if (saveButton) {
saveButton.addEventListener('click', wrappedSetAsCover);
}
}
// Add button and listeners when the DOM is fully loaded
window.onload = function() {
addSetAsCoverButton();
addButtonListeners();
};
})();
Key Changes Made:
Set as Cover Functionality: The setAsCover function now encapsulates the logic to extract the TID and image URL and call the desired showWindow function.
Immediate Invocation: When either the "發表文章" button or the "儲存草稿" button is clicked, the setAsCover function is called immediately.
Preserving Button Behavior: The original button behavior (submitting or saving) is preserved by using a setTimeout function. This ensures that the setAsCover function runs first, and then the click action of the button proceeds as usual.
Testing the Script:
Install the script using a browser extension like Tampermonkey.
Navigate to a JK Forum page with the available buttons.
Click the "Set as Cover" button to ensure it works as before.
Click the "發表文章" button and the "儲存草稿" button to confirm that the Set as Cover function is executed immediately after respective button clicks.
This will ensure that the Set as Cover functionality runs while maintaining the original functionality of submitting or saving drafts. If you need further adjustments, feel free to ask!
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 1.7
// @description Adds a button to activate the showWindow function when submitting or saving on JK Forum pages
// @author Your Name
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to extract TID from the URL
function getTID() {
const url = window.location.href;
const match = url.match(/thread-(\d{8})/);
return match ? match[1] : null;
}
// Function to get the image URL from the <meta property="og:image">
function getImgURL() {
const metaTag = document.querySelector('meta[property="og:image"]');
return metaTag ? metaTag.getAttribute('content') : null;
}
// Function to invoke the Set as Cover functionality
function setAsCover() {
const tid = getTID();
const imgURL = getImgURL();
if (tid && imgURL) {
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
}
// Function to create and add the Set as Cover button
function addSetAsCoverButton() {
const button = document.createElement('button');
button.innerText = 'Set as Cover';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
// Button click event
button.onclick = setAsCover; // Set as Cover button functionality
document.body.appendChild(button);
}
// Function to observe DOM changes and execute setAsCover if the URL changes
function observeDOMChanges() {
const observer = new MutationObserver(() => {
const tid = getTID(); // Get updated TID after form submission
const imgURL = getImgURL(); // Get updated image URL
if (tid && imgURL) {
// Call setAsCover function to run immediately after the page has loaded
setAsCover();
}
});
// Configure the observer to watch for changes in the body
observer.observe(document.body, { childList: true, subtree: true });
}
// Function to add event listeners to the submit and save buttons
function addButtonListeners() {
const submitButton = document.querySelector('button[name="topicsubmit"]');
const saveButton = document.querySelector('button[onclick*="postsave"]'); // Matching the button by its onclick
if (submitButton) {
submitButton.addEventListener('click', observeDOMChanges); // Start observing when the submit button is clicked
}
if (saveButton) {
saveButton.addEventListener('click', observeDOMChanges); // Start observing when the save button is clicked
}
}
// Add button and listeners when the DOM is fully loaded
window.onload = function() {
addSetAsCoverButton();
addButtonListeners();
};
})();
Changes Made:
MutationObserver: The script uses MutationObserver to watch for changes in the DOM. This way, the script can run the setAsCover function once the page reloads after submitting a post or saving a draft.
Event Listeners: The click event handlers for the submit and save buttons now call the observeDOMChanges() function, which sets up the MutationObserver.
Immediate Function Execution: When changes are detected, it retrieves the TID and image URL and subsequently runs the setAsCover function if valid values are found.
Testing the Script:
Install the script using a browser extension like Tampermonkey.
Navigate to a JK Forum page with the available buttons.
Click the "Set as Cover" button to ensure it works as before.
Click the "發表文章" button and the "儲存草稿" button, and verify that the Set as Cover function runs after the page has fully loaded.
With this setup, it will ensure that the Set as Cover functionality runs only after the page reloads due to form submission or draft saving. Let me know if you need any further modifications or assistance!
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 1.8
// @description Adds a button to activate the showWindow function when submitting or saving on JK Forum pages
// @author Your Name
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to extract TID from the URL
function getTID() {
const url = window.location.href;
const match = url.match(/thread-(\d{8})/);
return match ? match[1] : null;
}
// Function to get the image URL from the <meta property="og:image">
function getImgURL() {
const metaTag = document.querySelector('meta[property="og:image"]');
return metaTag ? metaTag.getAttribute('content') : null;
}
// Function to invoke the Set as Cover functionality
function setAsCover() {
const tid = getTID();
const imgURL = getImgURL();
if (tid && imgURL) {
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
}
// Function to create and add the Set as Cover button
function addSetAsCoverButton() {
const button = document.createElement('button');
button.innerText = 'Set as Cover';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
// Button click event
button.onclick = setAsCover; // Set as Cover button functionality
document.body.appendChild(button);
}
// Function to listen for the page load after button click
function startListening() {
window.addEventListener('load', function() {
setTimeout(setAsCover, 1000); // Delay to ensure the page is fully loaded
});
}
// Function to add event listeners to the submit and save buttons
function addButtonListeners() {
const submitButton = document.querySelector('button[name="topicsubmit"]');
const saveButton = document.querySelector('button[onclick*="postsave"]'); // Matching the button by its onclick
if (submitButton) {
submitButton.addEventListener('click', startListening); // Start listening when the submit button is clicked
}
if (saveButton) {
saveButton.addEventListener('click', startListening); // Start listening when the save button is clicked
}
}
// Add button and listeners when the DOM is fully loaded
window.onload = function() {
addSetAsCoverButton();
addButtonListeners();
};
})();
Key Changes:
setTimeout After load Event: The startListening function now uses the window.addEventListener('load', ...) method to wait until the page has fully loaded before executing the setAsCover function. The delay ensures that the script waits long enough for the content to be available.
Event Listeners: The event listeners are set to call the startListening function when the "發表文章" or "儲存草稿" button is pressed.
Delay for Full Loading: I included a setTimeout of 1000 milliseconds (1 second) after the load event. This gives the page adequate time to completely load any dynamic content before executing the setAsCover function.
How to Test the Script:
Install the script using a browser extension like Tampermonkey.
Navigate to a JK Forum page with the available buttons.
Click the "Set as Cover" button to ensure it works as expected.
Click either the "發表文章" or "儲存草稿" buttons to verify that the setAsCover function is executed after the page has fully loaded.
This should now work as intended! Let me know if you need any more adjustments or further assistance.
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 1.9
// @description Adds a button to activate the showWindow function after submitting or saving on JK Forum pages
// @author Your Name
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to extract TID from the URL
function getTID() {
const url = window.location.href;
const match = url.match(/thread-(\d{8})/);
return match ? match[1] : null;
}
// Function to get the image URL from the <meta property="og:image">
function getImgURL() {
const metaTag = document.querySelector('meta[property="og:image"]');
return metaTag ? metaTag.getAttribute('content') : null;
}
// Function to invoke the Set as Cover functionality
function setAsCover() {
const tid = getTID();
const imgURL = getImgURL();
if (tid && imgURL) {
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
}
// Function to create and add the Set as Cover button
function addSetAsCoverButton() {
const button = document.createElement('button');
button.innerText = 'Set as Cover';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
// Button click event
button.onclick = setAsCover; // Set as Cover button functionality
document.body.appendChild(button);
}
// Function to listen for the page load after button click
function listenForLoad() {
const observer = new MutationObserver(() => {
// Call setAsCover every time the URL changes
setAsCover();
});
// Watch for changes in the document body
observer.observe(document.body, { childList: true, subtree: true });
}
// Function to add event listeners to the submit and save buttons
function addButtonListeners() {
const submitButton = document.querySelector('button[name="topicsubmit"]');
const saveButton = document.querySelector('button[onclick*="postsave"]'); // Matching the button by its onclick
if (submitButton) {
submitButton.addEventListener('click', function() {
// Start observing the DOM changes after clicking the submit button
listenForLoad();
});
}
if (saveButton) {
saveButton.addEventListener('click', function() {
// Start observing the DOM changes after clicking the save button
listenForLoad();
});
}
}
// Add button and listeners when the DOM is fully loaded
window.onload = function() {
addSetAsCoverButton();
addButtonListeners();
};
})();
Key Adjustments:
MutationObserver: The listenForLoad function uses MutationObserver to detect changes within the document body, which allows it to trigger setAsCover whenever the page changes after a button click. This method will ensure we react every single time the content is modified in the document.
Immediate Function Call: Whenever one of the buttons is clicked (submit or save), the script starts observing for DOM changes directly.
Remove Delays: There are no artificial delays (like setTimeout), allowing it to execute as soon as the relevant page structure is updated.
How to Test the Script:
Install the script using a browser extension like Tampermonkey.
Navigate to a JK Forum page with the available buttons.
Click the "Set as Cover" button to confirm it works.
Click the "發表文章" or "儲存草稿" buttons, and verify that the setAsCover function executes after the page loads.
Conclusion
With this script, setAsCover should be invoked correctly whenever the specified buttons are pressed, activating the function on the newly loaded content of the page. If you have any questions or need further adjustments, feel free to ask!
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 2.0
// @description Executes setAsCover after submitting or saving on JK Forum pages
// @author Your Name
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
let sourceButtonClicked = false; // Flag to check if form submission occurred
// Function to extract TID from the URL
function getTID() {
const url = window.location.href;
const match = url.match(/thread-(\d{8})/);
return match ? match[1] : null;
}
// Function to get the image URL from the <meta property="og:image">
function getImgURL() {
const metaTag = document.querySelector('meta[property="og:image"]');
return metaTag ? metaTag.getAttribute('content') : null;
}
// Function to invoke the Set as Cover functionality
function setAsCover() {
const tid = getTID();
const imgURL = getImgURL();
if (tid && imgURL) {
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
}
// Function to create and add the Set as Cover button
function addSetAsCoverButton() {
const button = document.createElement('button');
button.innerText = 'Set as Cover';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
// Button click event
button.onclick = setAsCover; // Set as Cover button functionality
document.body.appendChild(button);
}
// Function to add event listeners to the submit and save buttons
function addButtonListeners() {
const submitButton = document.querySelector('button[name="topicsubmit"]');
const saveButton = document.querySelector('button[onclick*="postsave"]'); // Matching the button by its onclick
if (submitButton) {
submitButton.addEventListener('click', function() {
sourceButtonClicked = true; // Set the flag when submit is clicked
});
}
if (saveButton) {
saveButton.addEventListener('click', function() {
sourceButtonClicked = true; // Set the flag when save is clicked
});
}
}
// Add button and listeners when the DOM is fully loaded
window.onload = function() {
if (sourceButtonClicked) {
setAsCover(); // Run the Set as Cover function after the page loads if the flag is set
sourceButtonClicked = false; // Reset the flag
}
addSetAsCoverButton(); // Always add the Set as Cover button on load
addButtonListeners(); // Always add button listeners on load
};
})();
Explanation of Changes:
Flag (sourceButtonClicked): A simple boolean flag tracks if either the submit or save button was pressed.
Event Listeners: When one of the buttons is clicked, the flag is set to true.
Page Load Check: When the page finishes loading, it checks the flag:
If the flag is true, it runs the setAsCover() function and then resets the flag.
This ensures that setAsCover runs after the new page has loaded its content.
How to Test the Script:
Install the script using a browser extension like Tampermonkey.
Navigate to a JK Forum page with the available buttons.
Click the "Set as Cover" button to ensure it works as before.
Click the "發表文章" button and verify that the setAsCover function executes correctly after the new page loads.
Click the "儲存草稿" button and check that the setAsCover function runs as expected.
Conclusion
This updated structure should now work as intended, executing the setAsCover method only after the page loads following a button press. If you need further adjustments or have any other questions, feel free to ask!
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 2.1
// @description Executes setAsCover after submitting or saving on JK Forum pages
// @author Your Name
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
let sourceButtonClicked = false; // Flag to check if form submission occurred
// Function to extract TID from the URL
function getTID() {
const url = window.location.href;
const match = url.match(/thread-(\d{8})/);
return match ? match[1] : null;
}
// Function to get the image URL from the <meta property="og:image">
function getImgURL() {
const metaTag = document.querySelector('meta[property="og:image"]');
return metaTag ? metaTag.getAttribute('content') : null;
}
// Function to invoke the Set as Cover functionality
function setAsCover() {
const tid = getTID();
const imgURL = getImgURL();
if (tid && imgURL) {
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
}
// Function to create and add the Set as Cover button
function addSetAsCoverButton() {
const button = document.createElement('button');
button.innerText = 'Set as Cover';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
// Button click event
button.onclick = setAsCover; // Set as Cover button functionality
document.body.appendChild(button);
}
// Function to add event listeners to the submit and save buttons
function addButtonListeners() {
const submitButton = document.querySelector('button[name="topicsubmit"]');
const saveButton = document.querySelector('button[onclick*="postsave"]');
if (submitButton) {
submitButton.addEventListener('click', function () {
sourceButtonClicked = true; // Set the flag when submit is clicked
checkNewPageLoad(); // Start checking for the new page load immediately
});
}
if (saveButton) {
saveButton.addEventListener('click', function () {
sourceButtonClicked = true; // Set the flag when save is clicked
checkNewPageLoad(); // Start checking for the new page load immediately
});
}
}
// Function to check for page load after a button click
function checkNewPageLoad() {
const checkInterval = setInterval(() => {
const tid = getTID();
const imgURL = getImgURL();
// If TID and Image URL are found, execute setAsCover
if (tid && imgURL) {
clearInterval(checkInterval); // Stop checking
setAsCover(); // Call the function
sourceButtonClicked = false; // Reset the flag
}
}, 1000); // Check every 1 second
}
// Add button and listeners when the DOM is fully loaded
window.onload = function () {
addSetAsCoverButton(); // Always add the Set as Cover button on load
addButtonListeners(); // Always add button listeners on load
};
})();
How This Script Works:
Check on Interval: A setInterval function continuously checks every second if the new page has loaded by looking for the TID and image URL. If they are found, it calls setAsCover() and clears the interval.
Immediate Checking: The interval starts immediately after either the "發表文章" (Submit Topic) or "儲存草稿" (Save Draft) button is clicked.
Flag Reset: The flag indicating that a source button was clicked is reset after setAsCover() is executed to prevent multiple triggers in case of repeated submissions.
How to Test the Script:
Install the script using a browser extension like Tampermonkey.
Navigate to a JK Forum page with the available buttons.
Click the "Set as Cover" button to ensure it operates correctly.
Click the "發表文章" button, then verify that the setAsCover function executes after the new page loads, even with the pop-up appearing.
Click the "儲存草稿" button and check that the setAsCover function runs as expected.
This implementation should handle page loads and the presence of pop-ups effectively. If you have further questions or need adjustments, feel free to let me know!
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 2.2
// @description Executes setAsCover after submitting or saving on JK Forum pages
// @author Your Name
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Function to extract TID from the URL
function getTID() {
const url = window.location.href;
const match = url.match(/thread-(\d{8})/);
return match ? match[1] : null;
}
// Function to get the image URL from the <meta property="og:image">
function getImgURL() {
const metaTag = document.querySelector('meta[property="og:image"]');
return metaTag ? metaTag.getAttribute('content') : null;
}
// Function to invoke the Set as Cover functionality
function setAsCover() {
const tid = getTID();
const imgURL = getImgURL();
if (tid && imgURL) {
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
}
// Function to create and add the Set as Cover button
function addSetAsCoverButton() {
const button = document.createElement('button');
button.innerText = 'Set as Cover';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
// Button click event
button.onclick = setAsCover; // Set as Cover button functionality
document.body.appendChild(button);
}
// Function to add event listeners to the submit and save buttons
function addButtonListeners() {
const submitButton = document.querySelector('button[name="topicsubmit"]');
const saveButton = document.querySelector('button[onclick*="postsave"]');
if (submitButton) {
submitButton.addEventListener('click', function () {
setTimeout(checkForNewPage, 2000); // Wait for 2 seconds after clicking submit
});
}
if (saveButton) {
saveButton.addEventListener('click', function () {
setTimeout(checkForNewPage, 2000); // Wait for 2 seconds after clicking save
});
}
}
// Function to check for the presence of TID and Image URL after clicking
function checkForNewPage() {
const tid = getTID();
const imgURL = getImgURL();
// If TID and Image URL are found, execute setAsCover
if (tid && imgURL) {
setAsCover(); // Call the function
} else {
// Recheck again after 1 second for a set number of times
let attempts = 0;
const maxAttempts = 5;
const retryInterval = setInterval(() => {
const tidRetry = getTID();
const imgURLRetry = getImgURL();
attempts++;
if (tidRetry && imgURLRetry) {
clearInterval(retryInterval);
setAsCover();
}
// Stop after maximum attempts
if (attempts >= maxAttempts) {
clearInterval(retryInterval);
}
}, 1000); // Recheck every second
}
}
// Add button and listeners when the DOM is fully loaded
window.onload = function () {
addSetAsCoverButton(); // Always add the Set as Cover button on load
addButtonListeners(); // Always add button listeners on load
};
})();
Key Changes:
Improve Timing Logic: The function checkForNewPage now waits for 2 seconds after the button press to give time for the pop-up to disappear and the new page to load.
Retry Mechanism: If TID and the Image URL are not immediately available, the script will retry checking up to a maximum of 5 times (1-second intervals). This accounts for the scenario where the page may still be loading.
Execution After Button Press: This updated logic ensures that setAsCover executes only after confirming the new page has loaded and the necessary elements are present.
How to Test the Script:
Install the script using a browser extension like Tampermonkey.
Open a JK Forum page with the relevant buttons.
Click the "Set as Cover" button to ensure it operates correctly.
Click the "發表文章" button and ensure that the setAsCover function executes after the new page loads and the pop-up disappears.
Do the same with the "儲存草稿" button and verify functionality.
With this implementation, the setAsCover function should fire correctly after the new page loads and after the pop-up has finished displaying. If you face any further issues or need enhancements, please let me know!
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 2.3
// @description Executes setAsCover after submitting or saving on JK Forum pages
// @author Your Name
// @match https://www.jkforum.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to extract TID from the URL
function getTID() {
const url = window.location.href;
const match = url.match(/thread-(\d{8})/);
return match ? match[1] : null;
}
// Function to get the Image URL from the <meta property="og:image">
function getImgURL() {
const metaTag = document.querySelector('meta[property="og:image"]');
return metaTag ? metaTag.getAttribute('content') : null;
}
// Function to invoke the Set as Cover functionality
function setAsCover() {
const tid = getTID();
const imgURL = getImgURL();
if (tid && imgURL) {
const script = `showWindow('xx', 'plugin/?id=threadicon:set&tid=${tid}&imgurl=' + encodeURIComponent('${imgURL}'), 'get', 0);`;
eval(script);
} else {
alert('TID or Image URL not found!');
}
}
// Function to create and add the Set as Cover button
function addSetAsCoverButton() {
const button = document.createElement('button');
button.innerText = 'Set as Cover';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
// Button click event
button.onclick = setAsCover; // Set as Cover button functionality
document.body.appendChild(button);
}
// Function to monitor DOM changes
function monitorDOMChanges() {
const observer = new MutationObserver(() => {
const tid = getTID();
const imgURL = getImgURL();
// If TID and Image URL are found, execute setAsCover
if (tid && imgURL) {
observer.disconnect(); // Stop observing once we've found what we need
setAsCover(); // Call the function
}
});
// Start observing the body for changes
observer.observe(document.body, { childList: true, subtree: true });
}
// Function to add event listeners to the submit and save buttons
function addButtonListeners() {
const submitButton = document.querySelector('button[name="topicsubmit"]');
const saveButton = document.querySelector('button[onclick*="postsave"]');
if (submitButton) {
submitButton.addEventListener('click', function() {
monitorDOMChanges(); // Start monitoring for changes after submit is clicked
});
}
if (saveButton) {
saveButton.addEventListener('click', function() {
monitorDOMChanges(); // Start monitoring for changes after save is clicked
});
}
}
// Add button and listeners when the DOM is fully loaded
window.onload = function() {
addSetAsCoverButton(); // Always add the Set as Cover button on load
addButtonListeners(); // Always add button listeners on load
};
})();
Key Changes:
Mutating Observer: The monitorDOMChanges function uses a MutationObserver to listen for changes in the DOM, specifically looking for when the TID and Image URL become available.
Listener for Submit/Save Button: When you click the "發表文章" or "儲存草稿" button, the observer starts, ready to react to the DOM changes of the newly loaded page content.
Disconnection of Observer: Once the script successfully detects the TID and Image URL, it disconnects the observer to avoid unnecessary checks.
Testing the Script:
Install the script using a browser extension like Tampermonkey.
Open a JK Forum page with the relevant buttons.
Click the "Set as Cover" button to ensure it operates correctly.
Click the "發表文章" button and verify that the setAsCover function executes after the new page loads, even if there’s a pop-up.
Repeat the process with the "儲存草稿" button to ensure the expected results.
This modified approach should effectively handle the timing issues by directly observing the relevant changes in the DOM, ensuring that your setAsCover function runs as intended. Let me know if you run into any other issues or need further modifications!