Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/**
* We search the first URL because it will be the one
* that was originally specified in the DOM.
*/
const redirectChain = request.redirectChain();
const requestUrl = redirectChain && redirectChain.length > 0 ?
redirectChain[0].url() :
source.url();
/*
* TODO: Check what happens with prefetch, etc.
* `type` can be "parser", "script", "preload", and "other": https://chromedevtools.github.io/debugger-protocol-viewer/tot/Network/#type-Initiator
*/
// The doesn't seem to be an initiator in puppeteer :/
if (dom && requestUrl.startsWith('http')) {
return getElementByUrl(dom, requestUrl);
}
return null;
};
public async fetch(url: string): Promise {
/* istanbul ignore if */
if (!url) {
const promise = Promise.resolve(null);
(promise as any).abort = () => { };
return await promise;
}
/*
* We need a `HTMLElement` instead of the element returned by jsdom.
* To do so, we create a query from the element returned by jsdom and
* look for it in our `HTMLDocument`.
*/
const element = getElementByUrl(this._HTMLDocument, url);
const urlAsUrl = new URL(url);
let resourceUrl: string = urlAsUrl.href;
/* istanbul ignore if */
if (!urlAsUrl.protocol) {
resourceUrl = new URL(resourceUrl, this._connector.finalHref).href;
}
// Ignore if the resource has already been fetched.
if (this._connector.fetchedHrefs.has(resourceUrl)) {
return null;
}
this._connector.fetchedHrefs.add(resourceUrl);
debug(`resource ${resourceUrl} to be fetched`);
private setFetchElement(event: FetchEnd) {
const url = event.request.url;
if (this._document) {
event.element = getElementByUrl(this._document, url);
}
}