The HTML code you've shared is a hyperlink (an anchor tag) that, when clicked, triggers a JavaScript function. Here’s a breakdown of its components:
<a href="javascript:;">: This creates a link that does not navigate to a new page when clicked, as href="javascript:;" effectively prevents any default action.
onclick="showWindow(...);": This specifies a JavaScript function named showWindow that will be executed when the link is clicked.
showWindow('xx', 'plugin/?id=threadicon:set&tid=19596987&imgurl='+escape('https://i3.anysex.com/videos_screenshots/400000/400042/preview.jpg'),'get',0);: This part is calling the showWindow function with several parameters:
The first parameter 'xx' is likely an identifier or type for the window being shown.
The second parameter is a URL string that includes several query parameters (id, tid, and imgurl). The imgurl parameter contains a URL to an image, which is being escaped to ensure safe handling in a URL context.
The third parameter specifies the request method, which is 'get' in this case.
The last parameter is probably a setting or a numerical option, set to 0.
return false;: This prevents the default action of the link (which is navigating to a new page).
設為封面: This is Chinese text that translates to "Set as Cover" in English, suggesting that clicking the link may set the referenced image as a cover image for something, possibly a thread or a post in a forum.
Overall, this code is likely part of a web application, possibly a forum or content management system, allowing users to set an image as a cover for a particular thread or post.
User
is it possible to run this showWindow javascript on same website on a different page without this button?
Assistant
Yes, it is possible to run the showWindow JavaScript function on a different page of the same website without using the button. However, there are a few things to consider:
Javascript Availability: Ensure that the showWindow function is included in the JavaScript files that are loaded on the new page. If it is available globally (i.e., it is part of the same website’s JavaScript), you can call it directly from the console or within another script on that page.
Calling the Function: You can execute the function in a <script> tag in the new page’s HTML or directly from the browser's JavaScript console. Here is an example of how you could call it programmatically:
Note: I replaced escape with encodeURIComponent, which is a more modern and appropriate way to encode URLs.
CORS and Same-Origin Policy: If the page you're trying to run the function on is on the same domain as the page containing the function, it should work without issues. If there are cross-origin concerns (i.e., dealing with different subdomains or domains), it may affect the ability to access and execute functions.
Function Parameters: Make sure the parameters you provide when calling the function are valid and appropriate for the context in which you are using it.
Permissions: Depending on how the website is designed, certain actions might be restricted based on user roles or permissions, even if you can call the function.
Context: Ensure that the context in which showWindow is meant to operate aligns with the new page. If the page setup or the intended operation of showWindow is different in the new context, it may lead to unexpected behavior.
In summary, as long as the necessary JavaScript functions are available and the context is correct, you can run the showWindow function on a different page on the same website.
The above line is the general syntax where TID is a 8 digit number for the thread and imgURL is the address link of the image.
TID can be collected from the address link of the current webpage. An example of the address link is as followed.
https://www.jkforum.net/p/thread-19597007-1-1.html
The 8 digit number TID is between thread- and -1-1.html
For the imgURL, it can be collected from the src of element img#aimg_M7z98.zoom
Can you help me write a tampermonkey script for adding a button on https://www.jkforum.net/* to activite the showWindow function?
Assistant
Certainly! Below is a Tampermonkey script that adds a button to the pages on https://www.jkforum.net/*. When the button is clicked, it extracts the TID from the current URL and the image URL from the specified image element (img#aimg_M7z98.zoom), then calls the showWindow function with the collected values.
Tampermonkey Script
javascript
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 1.0
// @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 specified img element
function getImgURL() {
const imgElement = document.querySelector('img#aimg_M7z98.zoom');
return imgElement ? imgElement.src : 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.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;
})();
User
could you change it to collect the imgURL from this example element? the imgURL is the content=
Assistant
Certainly! If you want to extract the imgURL from a <meta> tag with the property og:image, you can modify the script accordingly. Below is the revised Tampermonkey script that collects the imgURL from the specified <meta> element.
Updated Tampermonkey Script
javascript
// ==UserScript==
// @name Show Window Button for JK Forum
// @namespace http://tampermonkey.net/
// @version 1.1
// @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 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
Is it possible to add an extra function of running showWindow when either of these buttons inside the following element is pressed?
Assistant
Yes, it's certainly possible to modify the Tampermonkey script to also trigger the showWindow function when either of the specified buttons inside the <div class="mtm mbm pnpost"> is pressed.
Here's how you can extend the script to achieve this:
Extended Tampermonkey Script
javascript
User
it doesn't work, it seems like , by default, the button will jump to another page saying the cover page is not set.
May I ask what is the diferent between these two elements?
first
Assistant
The two HTML code snippets you've provided are almost identical, but they contain one significant difference:
Differences in the Snippets
fid Parameter in the Link:
The first snippet has an fid parameter set to 1087: fid=1087
The second snippet has an fid parameter set to 1400: fid=1400
Contextual Implications
Functionality and Behavior:
The fid (forum ID or similar identifier) is likely used by the backend of the website to determine which forum or topic the actions (like submitting a post or saving a draft) are being conducted in. This can affect the visibility and accessibility of certain features, including whether a cover page has been set or not.
If the backend expects different behavior based on the forum ID referenced in the URL (for example, certain IDs may require a cover image to be set while others may not), trying to submit a post without setting the cover image can lead to the "cover page is not set" warning.
Element IDs:
Both buttons have the same id="postsubmit", which is not valid HTML because IDs should be unique on a page. This may lead to JavaScript issues when trying to select elements by ID since document.getElementById('postsubmit') would return the first element with that ID.
Possible Solutions to Fix the Issue
Debugging the Form Submission:
You could take a closer look at the network requests being made when the "发 表文章" (Submit article) button is clicked. Inspect the form data sent with the request to see if the tid, imgURL, or other required fields are missing.
Ensure that the showWindow function runs and completes successfully before allowing the form submission.
Adjust Event Handling:
Modify the JavaScript to delay the form submission until after the showWindow function has been executed. One method could be to programmatically trigger the form submission after waiting for the showWindow function to perform its task:
Adjusted Script Example
You can use a setTimeout to ensure the showWindow is executed before allowing the form to submit:
User
is it possible to change fid to 234?
Assistant
Yes, it's possible to change the fid to 234 when you submit a form or manipulate the DOM elements using JavaScript. However, it's important to know where the fid is being set and how it affects the form submission.
Understanding the Context
Changing in the URL: If the fid is part of the URL you want to submit (as seen in the example links you provided previously), you need to ensure that the correct fid is set in the form data sent to the server.
JavaScript Manipulation: If the fid is set dynamically via JavaScript, you can simply change its value before the form is submitted.
Example: Changing fid to 234
If fid is an Element in the HTML
If fid is in the form as a hidden input or URL parameter, you can modify it using JavaScript:
javascript
// This function modifies the fid in the input form
function changeFID() {
const fidInput = document.querySelector('input[name="fid"]'); // Adjust the selector if necessary
if (fidInput) {
fidInput.value = '234'; // Set fid to 234
}
}
If fid is in the URL
If you're sending a request that includes fid, such as when the form is submitted, and the URL looks something like this:
code
home.php?mod=spacecp&ac=credit&op=rule&fid=1400
You can modify the submission URL using JavaScript right before the submission:
Adjusted Tampermonkey Script
Here’s how you can modify the existing script to change fid to 234 when either submission button is clicked:
Install Tampermonkey: First, ensure you have the Tampermonkey extension installed in your web browser.
Create a New Script:
Click the Tampermonkey icon in your browser.
Select "Create a new script".
Paste the Script: Replace the default script in the editor with the script provided above.
Save the Script: Click the "File" menu and select "Save".
Visit the Website: Navigate to any page on https://www.jkforum.net/. You should see a "Set as Cover" button positioned at the top-right corner of the webpage.
Usage: When you click the button, it will extract the TID from the URL and the image URL from the specified img element, then call the showWindow function with those parameters.
Notes
Make sure the page structure remains constant; if the image element or the URL format changes, you may need to adjust the script accordingly.
The button style can be modified if desired.
This script uses eval to run the showWindow function, which can raise security concerns in other contexts. However, it is necessary here to execute the function dynamically.
Check your browser's console for any errors if the script does not work as expected.
Explanation of Changes:
Image URL Extraction: The getImgURL function has been updated to look for a <meta> tag with the attribute property="og:image" and retrieve the content attribute value to get the image URL.
Instructions to Use the Script:
Follow the same installation steps for the Tampermonkey extension as mentioned earlier.
Replace the previous script in the Tampermonkey editor with this updated version.
Save the script and navigate to any page on https://www.jkforum.net/.
The "Set as Cover" button will appear at the top-right corner as before, and it will work with the image URL found in the specified meta tag.
This script should now accurately extract the desired image URL from the <meta> tag as specified. If you have any further adjustments or questions, feel free to ask!
// ==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 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 create and add the button to trigger showWindow
function addShowWindowButton() {
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 = runShowWindow;
document.body.appendChild(button);
}
// Function to add event listeners to the existing buttons
function attachShowWindowToExistingButtons() {
const postButtons = document.querySelector('.mtm.mbpnpost');
if (postButtons) {
const submitBtn = postButtons.querySelector('#postsubmit[type=submit]');
const saveDraftBtn = postButtons.querySelector('button[onclick^="$("postsave")"]');
if (submitBtn) {
submitBtn.addEventListener('click', function(event) {
event.preventDefault(); // Prevent the default form submission if needed
runShowWindow();
submitBtn.click(); // Call the original action if desired
});
}
if (saveDraftBtn) {
saveDraftBtn.addEventListener('click', function(event) {
event.preventDefault(); // Prevent the default action if needed
runShowWindow();
saveDraftBtn.onclick(); // Call the original onclick action if desired
});
}
}
}
// Initialize the script when the DOM is fully loaded
window.addEventListener('load', function() {
addShowWindowButton();
attachShowWindowToExistingButtons();
});
})();
Explanation of the Changes:
runShowWindow Function: This new function encapsulates the logic for running the showWindow function. This makes it easy to call it from multiple places.
Event Listeners for Existing Buttons:
Added a function called attachShowWindowToExistingButtons. This function finds the appropriate buttons (#postsubmit for submitting and the button for saving drafts) and adds click event listeners to them.
When either button is clicked, the runShowWindow function is called before triggering the original action.
Event Prevention:
The event.preventDefault() is called to prevent the default submission behavior initially, allowing the showWindow function to run. You may choose to comment out this line if you want the form to submit immediately without waiting for the showWindow execution.
Usage:
Install the updated script in Tampermonkey as before.
Whenever you press the "发表文章" (Submit article) button or the "儲存草稿" (Save draft) button, it will now also execute the showWindow function as specified.
Feel free to adjust the script further according to your needs! If you have any more requests or questions, let me know!
javascript
// Function to attach event listeners to the existing buttons
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(); // Prevent the default form submission
runShowWindow(); // Run showWindow
// Delay the actual submission after showWindow completes (adjust timing if needed)
setTimeout(function() {
submitBtn.click(); // Trigger native button click after showWindow
}, 500); // Adjust the delay as necessary
});
}
if (saveDraftBtn) {
saveDraftBtn.addEventListener('click', function(event) {
event.preventDefault(); // Prevent default action if needed
runShowWindow();
// Delay the save draft action
setTimeout(function() {
saveDraftBtn.onclick(); // Trigger the existing onclick action
}, 500); // Adjust the delay if necessary
});
}
}
}
Notes:
Debugging: Use the browser console to log messages or errors when trying to run these actions. Checking the network tab for any failed requests may give insight into what is missing or leading to the warning.
Consider JavaScript Event Handling: Given that both buttons share the same ID in your example, it might be worth modifying one of the IDs if possible. Using unique IDs for elements ensures proper event handling.
Feel free to test the adjustments outlined, and if you encounter any additional issues or need any more modifications, let me know!
// ==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) {
// Assuming the fid is in the URL
const actionUrl = postForm.action.replace(/fid=\d+/, 'fid=234');
postForm.action = actionUrl; // Set the new action URL with fid=234
}
}
// 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(); // Prevent the default form submission
changeFID(); // Change the fid to 234
runShowWindow(); // Run showWindow
// Trigger submission after running showWindow
setTimeout(function() {
submitBtn.click(); // Trigger native button click after showWindow
}, 500); // Adjust the delay if necessary
});
}
if (saveDraftBtn) {
saveDraftBtn.addEventListener('click', function(event) {
event.preventDefault(); // Prevent default action if needed
changeFID(); // Change the fid to 234
runShowWindow();
// Delay the save draft action
setTimeout(function() {
saveDraftBtn.onclick(); // Trigger the existing onclick action
}, 500); // Adjust the delay if necessary
});
}
}
}
// Initialize the script when the DOM is fully loaded
window.addEventListener('load', function() {
addShowWindowButton(); // Assuming you have this function implemented
attachShowWindowToExistingButtons();
});
})();
Explanation of the Changes:
Changing fid: Added the changeFID function, which modifies the action URL of the form, replacing any current fid value with 234.
Triggering on Button Clicks: Both for the submit and save draft buttons, the changeFID function is called before executing the actual showWindow logic.
Important Note:
Make sure to review the HTML structure and adjust element selectors as necessary (e.g., targeting the correct form). If the fid value is stored differently (like as a hidden input), you'll need to modify the approach accordingly.
Test the script after these adjustments to see if it addresses your requirement. If you encounter any issues or need further refinements, feel free to ask!