您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Extract text from webpage using XPath with GUI
当前为
// ==UserScript== // @name XPath Text Extractor // @namespace http://tampermonkey.net/ // @version 0.1 // @description Extract text from webpage using XPath with GUI // @author oovz // @match https://www.qidian.com/chapter/* // @grant none // @license MIT // ==/UserScript== (function() { 'use strict'; // Configure your XPath here const TITLE_XPATH = '//div[contains(@class, "chapter-wrapper")]//div[contains(@class, "print")]//h1'; // Fill this with your XPath const CONTENT_XPATH = '//div[contains(@class, "chapter-wrapper")]//div[contains(@class, "print")]//main/p/span'; // Fill this with your XPath // Create GUI elements const gui = document.createElement('div'); gui.style.cssText = ` position: fixed; bottom: 20px; right: 20px; background: white; padding: 15px; border: 1px solid #ccc; border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1); z-index: 9999; `; const output = document.createElement('textarea'); output.style.width = '300px'; output.style.height = '150px'; output.style.marginBottom = '10px'; output.readOnly = true; const copyButton = document.createElement('button'); copyButton.textContent = 'Copy Text'; copyButton.style.display = 'block'; // Add elements to GUI gui.appendChild(output); gui.appendChild(copyButton); document.body.appendChild(gui); // Extract text function function getElementsByXpath(xpath) { const results = []; const query = document.evaluate( xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null ); for (let i = 0; i < query.snapshotLength; i++) { const node = query.snapshotItem(i); if (node) { // Get only direct text content and exclude child elements let directTextContent = ''; for (let j = 0; j < node.childNodes.length; j++) { const childNode = node.childNodes[j]; if (childNode.nodeType === Node.TEXT_NODE) { directTextContent += childNode.textContent; } } // Only trim if it's the title (preserve indentation for content) if (xpath === TITLE_XPATH) { directTextContent = directTextContent.trim(); if (directTextContent) { results.push(directTextContent); } } else { // For content, preserve indentation if (directTextContent) { results.push(directTextContent); } } } } return results; } // Initial extraction function updateTitleOutput() { const elements = getElementsByXpath(TITLE_XPATH); return elements.join('\n'); } function updateContentOutput() { const elements = getElementsByXpath(CONTENT_XPATH); return elements.join('\n'); } function updateOutput() { const title = updateTitleOutput(); const content = updateContentOutput(); output.value = title ? title + '\n\n' + content : content; } // Run initial extraction updateOutput(); // Add event listener for copy button copyButton.addEventListener('click', () => { output.select(); document.execCommand('copy'); copyButton.textContent = 'Copied!'; setTimeout(() => { copyButton.textContent = 'Copy Text'; }, 1000); }); // Update when DOM changes const observer = new MutationObserver(updateOutput); observer.observe(document.body, { childList: true, subtree: true }); })();
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址