Get the data of the HTML page of the nearest ancestor hierarchy
Demo
Examples
<script type="importmap">
{
"imports": {
"@w0s/closest-html-page": "...",
"whatwg-mimetype": "..."
}
}
</script>
<script type="module">
import ClosestHTMLPage from '@w0s/closest-html-page';
const closestHTMLPage = new ClosestHTMLPage({
maxFetchCount: 3,
fetchOptions: {
redirect: 'error',
},
mimeTypes: ['text/html'],
});
await closestHTMLPage.fetch('https://example.com/path/to/file');
const url = closestHTMLPage.getUrl();
const title = closestHTMLPage.getTitle();
</script>
Constructor
new ClosestHTMLPage(options?: Option)
Parameters
-
options
[Optional] - Options for accessing web content.
Option
interface Option {
maxFetchCount?: number;
fetchOptions?: RequestInit;
mimeTypes?: DOMParserSupportedType[];
}
maxFetchCount
- If no HTML page matching the condition can be retrieved after this number of attempts to access the ancestor hierarchy, the process is rounded up (
0
= ∞). The default value is0
. fetchOptions
- An object containing any custom settings that you want to apply to the reques. Same as the second argument of the `fetch()` method.
mimeTypes
- MIME types of the HTML resource to retrieve. The values that can be specified are limited to
DOMParserSupportedType
types, namely 'text/html
', 'text/xml
', 'application/xml
', 'application/xhtml+xml
', and 'image/svg+xml
'. The default value is['text/html', 'application/xhtml+xml']
.
Methods
async fetch(baseUrl: string = location.toString()): Promise<void>
- Traverse the ancestor hierarchy in order from the base URL and retrieve the data of resources that match the specified condition (MIME types). This method must be called before executing the following
getXXX()
. getFetchedResponses(): Set<Response>
- Get the
Response
data resulting from the execution offetch()
. getUrl(): string | null
- Get the URL of the HTML page of the nearest ancestor hierarchy.
getTitle(): string | null
- Get the title of the HTML page of the nearest ancestor hierarchy.