If the button is still missing after implementing the function, there may be several reasons for this issue. Here are some troubleshooting steps and adjustments you can try:
Debugging Steps
-
Check the Selector: Ensure that the selector used to find the place where the button should be appended is correct. The line const postButtons = document.querySelector('.mtm.mbpnpost'); may not be matching any elements on the page.
-
Use console.log for Debugging: Add console.log() statements in your script to determine if the function is being executed and whether the button is being created.
For example, modify the addShowWindowButton function as follows:
function addShowWindowButton() {
const postButtons = document.querySelector('.mtm.mbpnpost');
console.log('Post buttons element:', postButtons); // Check if the element is found
const button = document.createElement('button');
button.innerText = 'Show Window';
button.style.marginRight = '10px';
button.style.cursor = 'pointer';
button.style.color = '#ffffff';
button.style.backgroundColor = '#007bff';
button.style.border = 'none';
button.style.borderRadius = '4px';
button.style.padding = '5px 10px';
button.style.fontSize = '14px';
// Attach click event
button.addEventListener('click', function() {
changeFID();
runShowWindow();
});
// Check if postButtons exist before inserting
if (postButtons) {
postButtons.insertBefore(button, postButtons.firstChild);
console.log('Button added successfully'); // Confirm button has been added
} else {
console.log('Post buttons element not found'); // If not found
}
}
-
Ensure Script is Executed After Page Load: Depending on how the website loads content, ensure that your script runs at the right time. Make sure you're listening for the load event, which you've already done. If the page uses AJAX to load content after the initial page load, you might need to use a MutationObserver to detect when new elements are added to the DOM.
Here's how to use a MutationObserver:
const observer = new MutationObserver((mutationsList, observer) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
addShowWindowButton(); // Try to add the button whenever the DOM changes
}
}
});
// Start observing the target node for configured mutations
const targetNode = document.body; // Observe the entire body for changes
observer.observe(targetNode, {
childList: true,
subtree: true
});
-
Browser Extensions/Ad Blockers: Sometimes, other browser extensions can interfere with the execution of user scripts. Temporarily disable any ad blockers or other extensions to see if that affects the visibility of the button.
-
Check Console for Errors: Look for any errors in the browser's console (F12) that may be preventing your script from executing properly.
Complete Updated Script with MutationObserver
Here's how your complete script might look with the MutationObserver added:
// ==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 and also runs it on button clicks within a specific div.
// @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 run the showWindow function
function runShowWindow() {
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 change the fid in the submission
function changeFID() {
const postForm = document.querySelector('form'); // Adjust this to target the parent form, if necessary
if (postForm) {
const actionUrl = postForm.action.replace(/fid=\d+/, 'fid=234');
postForm.action = actionUrl; // Set the new action URL with fid=234
}
}
// Function to add the Show Window button to the page
function addShowWindowButton() {
const postButtons = document.querySelector('.mtm.mbpnpost');
console.log('Post buttons element:', postButtons); // Check if the element is found
const button = document.createElement('button');
button.innerText = 'Show Window';
button.style.marginRight = '10px';
button.style.cursor = 'pointer';
button.style.color = '#ffffff';
button.style.backgroundColor = '#007bff';
button.style.border = 'none';
button.style.borderRadius = '4px';
button.style.padding = '5px 10px';
button.style.fontSize = '14px';
button.addEventListener('click', function() {
changeFID();
runShowWindow();
});
if (postButtons) {
postButtons.insertBefore(button, postButtons.firstChild);
console.log('Button added successfully'); // Confirm button has been added
} else {
console.log('Post buttons element not found'); // If not found
}
}
// Add buttons to existing form
function attachShowWindowToExistingButtons() {
const postButtons = document.querySelector('.mtm.mbpnpost');
if (postButtons) {
const submitBtn = postButtons.querySelector('button[type=submit]');
const saveDraftBtn = postButtons.querySelector('button[onclick^="$("postsave")"]');
if (submitBtn) {
submitBtn.addEventListener('click', function(event) {
event.preventDefault();
changeFID();
runShowWindow();
setTimeout(function() {
submitBtn.click();
}, 500);
});
}
if (saveDraftBtn) {
saveDraftBtn.addEventListener('click', function(event) {
event.preventDefault();
changeFID();
runShowWindow();
setTimeout(function() {
saveDraftBtn.onclick();
}, 500);
});
}
}
}
// Initialize the script when the DOM is fully loaded
window.addEventListener('load', function() {
addShowWindowButton();
attachShowWindowToExistingButtons();
});
// Use MutationObserver to monitor changes to the DOM
const observer = new MutationObserver((mutationsList, observer) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
addShowWindowButton(); // Try to add the button whenever the DOM changes
}
}
});
// Start observing the target node for configured mutations
const targetNode = document.body; // Observe the entire body for changes
observer.observe(targetNode, {
childList: true,
subtree: true
});
})();
Summary:
- Verify your selectors to correctly identify where to insert the button.
- Add debugging logs to gain insight into what the script is doing.
- Use a
MutationObserver to watch for dynamic content changes on the page.
- Check the console for any potential errors that might be causing your button not to be appended.
After applying these suggestions, review the page again to see if the button appears as expected. If there are still problems, please share more details about the structure of the page, and I can assist you further.