It is working great now! Thanks!
I would like to add another @match website, https://www.xinggan5.top/* . The website look similar to others in the @match list but the name of the div elements are different. Could you help me put these option into the program?
div.title = div.node p a b
div.jianjie = div.article-meta
div.item_info = div.toptip
div.page = div.pagination p
Assistant
I'm glad to hear that things are working great for you! I'd be happy to help you integrate the new website https://www.xinggan5.top/* into your program. Based on your description, it looks like you'll need to handle different DOM structures for this new site compared to the existing ones. Here's a step-by-step guide to help you add the new site and handle its unique div elements.
1. Update the @match Directive
First, ensure that your userscript includes the new website in its @match directives. This allows the script to execute on the specified website.
javascript
// ==UserScript==
// @name Your Script Name
// @namespace http://your.namespace/
// @version 1.0
// @description Describe what your script does
// @author Your Name
// @match https://www.existingwebsite1.com/*
// @match https://www.existingwebsite2.com/*
// @match https://www.xinggan5.top/*
// @grant none
// ==/UserScript==
2. Define Selectors for Each Website
Since the new website has different div element names, you'll need to define selectors specific to each site. You can achieve this by detecting the current hostname and setting the selectors accordingly.
javascript
(function() {
'use strict';
// Define an object to hold selectors for different websites
const selectors = {};
// Get the current hostname
const hostname = window.location.hostname;
if (hostname === 'www.existingwebsite1.com' || hostname === 'www.existingwebsite2.com') {
// Selectors for existing websites
selectors.title = 'div.someClass p a b';
selectors.jianjie = 'div.anotherClass';
selectors.item_info = 'div.yetAnotherClass';
selectors.page = 'div.somePaginationClass p';
} else if (hostname === 'www.xinggan5.top') {
// Selectors for xinggan5.top
selectors.title = 'div.node p a b';
selectors.jianjie = 'div.article-meta';
selectors.item_info = 'div.toptip';
selectors.page = 'div.pagination p';
} else {
// Default selectors or handle unknown sites
console.warn('No selectors defined for this site.');
}
// Function to process the page using the defined selectors
function processPage() {
if (!selectors.title || !selectors.jianjie || !selectors.item_info || !selectors.page) {
console.warn('Selectors are not properly defined for this site.');
return;
}
// Example: Extract and log the title
const titles = document.querySelectorAll(selectors.title);
titles.forEach(title => {
console.log('Title:', title.textContent.trim());
});
// Example: Extract and log the 'jianjie'
const jianjies = document.querySelectorAll(selectors.jianjie);
jianjies.forEach(jianjie => {
console.log('Jianjie:', jianjie.textContent.trim());
});
// Example: Extract and log the 'item_info'
const itemInfos = document.querySelectorAll(selectors.item_info);
itemInfos.forEach(itemInfo => {
console.log('Item Info:', itemInfo.textContent.trim());
});
// Example: Extract and log the pagination info
const pages = document.querySelectorAll(selectors.page);
pages.forEach(page => {
console.log('Page Info:', page.textContent.trim());
});
// Add more processing logic as needed
}
// Initialize the processing after the DOM is fully loaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', processPage);
} else {
processPage();
}
})();
3. Explanation of the Script
@match Directive: Ensure that the @match includes the new website https://www.xinggan5.top/*.
Selectors Object: Create an object to store selectors for different websites. This makes it easy to manage and extend in the future.
Hostname Detection: Use window.location.hostname to determine which website the script is running on and set the appropriate selectors.
Processing Function: A processPage function uses the defined selectors to perform actions like extracting and logging text content. You can customize this function based on what your script needs to do with these elements.
DOM Ready Handling: Ensure that the script runs after the DOM is fully loaded to avoid issues with elements not being available yet.
4. Customizing Further
Error Handling: The script includes warnings if selectors are not defined, which helps in debugging.
Extensibility: If you add more websites in the future, you can simply extend the if-else block with new hostnames and their respective selectors.
Functionality Enhancements: Depending on what your script does (e.g., modifying the DOM, adding events), you can expand the processPage function accordingly.
5. Testing
After updating your script:
Install/Update the Userscript: Make sure to save and install/update your userscript in your userscript manager (e.g., Tampermonkey, Greasemonkey).
Navigate to the New Site: Visit https://www.xinggan5.top/ and check the browser console (F12 or Ctrl+Shift+I) to see if the selectors are working as expected.
Debugging: If elements are not being selected correctly:
Inspect the page's HTML to ensure the selectors match the actual structure.
Adjust the selectors in the script as needed.
Use more specific selectors if necessary to target the correct elements.
6. Example: Enhanced Selector Matching
If you find that the selectors are too broad or not unique enough, you can enhance them. For example:
javascript
selectors.title = 'div.node > p > a > b';
selectors.jianjie = 'div.article-meta';
selectors.item_info = 'div.toptip';
selectors.page = 'div.pagination > p';
Using the > combinator ensures that you're selecting direct child elements, which can improve the accuracy of your selections.
7. Additional Tips
Use @include if Necessary: Sometimes @include is more flexible than @match depending on your needs.
Dynamic Content Handling: If the website loads content dynamically (e.g., via AJAX), you might need to use MutationObserver to watch for changes in the DOM and process elements as they appear.
Modular Code: Consider modularizing your code by creating separate functions or even separate script files for each website if it grows in complexity.
8. Final Thoughts
By following the above steps, you should be able to successfully add https://www.xinggan5.top/* to your script and handle its unique div elements. Remember to thoroughly test the script on the new site to ensure everything works as intended. If you encounter any specific issues or need further customization, feel free to ask!