@w0s/closest-html-page
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

Get the data of the HTML page of the nearest ancestor hierarchy

npm version test status

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 is 0.
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 of fetch().
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.

Readme

Keywords

none

Package Sidebar

Install

npm i @w0s/closest-html-page

Weekly Downloads

4

Version

3.0.0

License

MIT

Unpacked Size

49.7 kB

Total Files

11

Last publish

Collaborators

  • saekitominaga