15,028
edits
| Line 21: | Line 21: | ||
</table> | </table> | ||
== Browser | == Browser Console == | ||
=== List matched elements by XPath rule === | |||
# | |||
# | Open the browser console in {{chrome}} or {{Edge}}: | ||
# Menu: Tools → Developer Tools → Console | |||
# To verify elements matching an XPath rule, type the expression using $x(). For example, to find all images, enter {{kbd | key = <nowiki>$x("//img")</nowiki>}} and press Enter. The matched elements will be listed in the console<ref>[http://stackoverflow.com/questions/3030487/is-there-a-way-to-get-the-xpath-in-google-chrome Is there a way to get the xpath in google chrome? - Stack Overflow]</ref><ref>[https://yizeng.me/2014/03/23/evaluate-and-validate-xpath-css-selectors-in-chrome-developer-tools/ Evaluate and validate XPath/CSS selectors in Chrome Developer Tools | Yi Zeng's Blog]</ref>: | |||
https://planetoid.info/images/xpath_result_on_browser_console.jpg | https://planetoid.info/images/xpath_result_on_browser_console.jpg | ||
To extract and display the text content of matched elements — cleaned and separated by newlines — use the snippet below. The example uses XPath rule {{kbd | key = <nowiki>//div[@id="showvender"]/a</nowiki>}}: | |||
<pre> | <pre> | ||
// | // Use document.evaluate to retrieve all matching elements | ||
const result = document.evaluate('//div[@id="showvender"]/a', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); | const result = document.evaluate('//div[@id="showvender"]/a', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); | ||
// | // Collect text content from each element | ||
const textArray = []; | const textArray = []; | ||
for (let i = 0; i < result.snapshotLength; i++) { | for (let i = 0; i < result.snapshotLength; i++) { | ||
const element = result.snapshotItem(i); | const element = result.snapshotItem(i); | ||
// | // Strip newlines and trim whitespace | ||
const cleanedText = element.textContent.replace(/[\n\r]+/g, '').trim(); | const cleanedText = element.textContent.replace(/[\n\r]+/g, '').trim(); | ||
textArray.push(cleanedText); | textArray.push(cleanedText); | ||
} | } | ||
// | // Print all results, one per line | ||
console.log(textArray.join('\n')); | |||
</pre> | |||
=== List matched elements by CSS selector === | |||
To quickly list all elements matching a CSS selector, use {{kbd | key = <nowiki>$$("css-selector")</nowiki>}} in the console. For example, to find all images, enter {{kbd | key = <nowiki>$$("img")</nowiki>}} and press Enter. The matched elements will be listed in the console. | |||
To extract and display the text content of matched elements — cleaned and separated by newlines — use the snippet below. The example uses CSS selector {{kbd | key = <nowiki>div#showvender a</nowiki>}}: | |||
<pre> | |||
// Use querySelectorAll to retrieve all matching elements | |||
const elements = document.querySelectorAll('div#showvender a'); | |||
// Collect text content from each element | |||
const textArray = []; | |||
elements.forEach(el => { | |||
// Strip newlines and trim whitespace | |||
const cleanedText = el.textContent.replace(/[\n\r]+/g, '').trim(); | |||
textArray.push(cleanedText); | |||
}); | |||
// Print all results, one per line | |||
console.log(textArray.join('\n')); | console.log(textArray.join('\n')); | ||
</pre> | |||
=== Highlight matched elements by XPath rule === | |||
By XPath rule: | |||
<pre> | |||
const xpath = '//main[contains(@class, "htmlview") and @data-cat="article"]'; | |||
// Helper: resolve XPath to an array of elements | |||
function $x(xpath) { | |||
const result = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); | |||
return Array.from({ length: result.snapshotLength }, (_, i) => result.snapshotItem(i)); | |||
} | |||
// Scroll to the first matched element | |||
$x(xpath)[0]?.scrollIntoView(); | |||
// Highlight all matched elements with a red outline | |||
$x(xpath).forEach(el => { | |||
el.style.outline = '3px solid red'; | |||
}); | |||
</pre> | |||
=== Highlight matched elements by CSS selector === | |||
By CSS selector: | |||
<pre> | |||
const cssSelector = "main.htmlview[data-cat='article']"; | |||
// Scroll to the first matched element | |||
document.querySelector(cssSelector)?.scrollIntoView(); | |||
// Highlight all matched elements with a red outline | |||
document.querySelectorAll(cssSelector).forEach(el => { | |||
el.style.outline = '3px solid red'; | |||
}); | |||
</pre> | </pre> | ||