如何在Ebay页面添加脚本移除含China的li父标签?
Absolutely, this is totally doable! You’ve got the right idea—here’s a step-by-step guide to make this work, including how to insert your script and a ready-to-use example:
First: Is This Feasible?
Yes! eBay’s search results are rendered in your browser’s DOM, so you can manipulate them with JavaScript. The key is targeting the right elements and handling both the initial page load and infinite scroll (since eBay loads more listings as you scroll down).
Step 1: Use a User Script Manager (Easiest Approach)
The simplest way to run your script automatically on eBay’s search pages is with a user script manager like Tampermonkey (works on Chrome, Firefox, Edge, etc.). This lets you inject scripts into specific websites without modifying eBay’s code directly.
Install Tampermonkey
- Grab the extension from your browser’s add-on store (search for "Tampermonkey").
- Once installed, click the Tampermonkey icon and select "Create a new script".
Step 2: Paste the Custom Script
Replace the default template with this script (it’s tailored to eBay’s current DOM structure, but you may need to tweak selectors if eBay updates their layout later):
// ==UserScript== // @name eBay Remove China Listings // @namespace http://tampermonkey.net/ // @version 0.1 // @description Automatically removes eBay listings that originate from China // @author Your Name // @match https://www.ebay.com/sch/* // @match https://www.ebay.co.uk/sch/* // @match https://www.ebay.de/sch/* // @grant none // ==/UserScript== (function() { 'use strict'; // Core function to scan and remove China-based listings function purgeChinaListings() { // Target eBay's listing elements (adjust selector if eBay changes their DOM) const allListings = document.querySelectorAll('li.s-item'); allListings.forEach(listing => { // Look for location text (common classes for location info on eBay) const locationTags = listing.querySelectorAll('.s-item__location, .s-item__itemLocation'); let isChinaListing = false; // Check each location element for "China" (case-insensitive) locationTags.forEach(tag => { if (tag.textContent.toLowerCase().includes('china')) { isChinaListing = true; } }); // Remove the listing if it's from China if (isChinaListing) { listing.remove(); console.log('Removed China listing:', listing); } }); } // Run once when the page first loads purgeChinaListings(); // Handle infinite scroll: watch for new listings being added const scrollObserver = new MutationObserver((mutations) => { mutations.forEach(mutation => { // If new elements are added to the page, re-run the purge if (mutation.addedNodes.length > 0) { purgeChinaListings(); } }); }); // Observe the entire body for new elements (covers eBay's scroll-loaded content) scrollObserver.observe(document.body, { childList: true, subtree: true }); })();
Step 3: Customize and Test
- Update Match URLs: Add or remove eBay country domains (like
https://www.ebay.ca/sch/*) in the@matchlines to cover the regions you use. - Adjust Selectors: If eBay changes their HTML classes (e.g.,
.s-itembecomes.new-listing-class), use your browser’s DevTools (F12) to inspect the listing elements and update the selectors in the script. - Test It: Save the script, go to an eBay search page, and watch as listings from China disappear automatically (check the console with F12 to see which listings were removed).
Alternative: Run Script in Browser Console (Temporary)
If you don’t want to install an extension, you can paste the core purgeChinaListings() function plus the initial call into your browser’s console (F12 > Console tab). This works temporarily, but you’ll have to re-run it every time you load a new page or scroll for more listings.
内容的提问来源于stack exchange,提问作者0_yami_0

