happy-dom
happy-dom copied to clipboard
add Referer when load web assets
some websites have enabled anti-theft links, we need to add headers to solve the problem.
https://github.com/capricorn86/happy-dom/blob/f4320c383fefce587effbf856ef7ba9954533cba/packages/happy-dom/src/fetch/ResourceFetchHandler.ts#L33-L46
/**
* Returns resource data synchonously.
*
* @param document Document.
* @param url URL.
* @returns Response.
*/
public static fetchSync(document: IDocument, url: string): string {
// We want to only load SyncRequest when it is needed to improve performance and not have direct dependencies to server side packages.
const absoluteURL = RelativeURL.getAbsoluteURL(document.defaultView.location, url);
const syncRequest = require('sync-request');
const response = syncRequest('GET', absoluteURL, {
headers: {
'user-agent': document.defaultView.navigator.userAgent,
cookie: document.defaultView.document.cookie,
referer: document.defaultView.location.href,
pragma: 'no-cache'
}
});
if (response.isError()) {
throw new DOMException(
`Failed to perform request to "${absoluteURL}". Status code: ${response.statusCode}`
);
}
return response.getBody().toString();
}
}