~/Tampermonkey Script to Generate Tags from HTML IDs
Jan 7, 2025
Tampermonkey is a userscript manager that lets users inject JavaScript into web pages. A common automation task is extracting HTML element ID attributes and generating tags or categorizations from them. This article details how to create a Tampermonkey script that automatically collects all element IDs from the DOM and generates tags.
Tampermonkey scripts execute in the browser and can modify the page or export data. You can use this technique to assist with content organization, process data for SEO, or facilitate debugging.
Tampermonkey Script Example
The following code will collect all unique IDs present in the web page, generate tag strings, and output them as a comma-separated string. You can modify the output method as desired.
|
|
-
The querySelectorAll call finds all elements with an
id
attribute. -
Only unique IDs are collected due to Set usage.
-
Output options include console logging, page injection, or network requests for downstream collection.
-
ID attributes are required to be unique within a page by the HTML standard, but invalid HTML sometimes contains duplicates.
-
Tampermonkey scripts are often used for web scraping prototyping.
-
Pages with many dynamically generated elements can change their IDs on the fly, so for dynamic sites consider using MutationObserver to track changes.
Advanced Options
To monitor new IDs inserted dynamically, you can leverage MutationObserver:
|
|
Relevant Links
- Tampermonkey Documentation
- MDN Element id property
- Best Practices for HTML IDs
- Userscripts in Web Development
- SEO and Semantic HTML
- Unique Constraints in HTML
- Information Architecture and Tagging
- Guide to DOM Traversal
- MutationObserver Patterns
- Debugging with Console Log
Practical applications include debugging broken anchor links, gathering front end metadata, or supporting custom accessibility tools.
It is not recommended to run userscripts on banking or sensitive sites. Always comply with page terms of service.
There is an XKCD comic poking fun at ID and class name proliferation in web pages.
This method does not generate semantic tags from IDs, it only collects the literal strings present in the DOM. For further automation, integrate with external natural language processing tools.