Skip to content

Latest commit

 

History

History

closest-html-page

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

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

npm version Workflow 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.url;
  const title = closestHTMLPage.title;
</script>

Constructor

new ClosestHTMLPage(options?: Readonly<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().
get fetchedResponses(): Set<Response>
Get the Response data resulting from the execution of fetch().
get url(): string | null
Get the URL of the HTML page of the nearest ancestor hierarchy.
get title(): string | null
Get the title of the HTML page of the nearest ancestor hierarchy.