Last active
April 19, 2021 15:34
-
-
Save jherax/5f919aad8ea7535790f3d311c26a6342 to your computer and use it in GitHub Desktop.
Gets all Response Headers for the current document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets all Reponse Headers for the current document. | |
* @returns An object cointaining the key-value pairs for each header | |
*/ | |
function getHeaders(url = document.location.href) { | |
const req = new XMLHttpRequest(); | |
req.open("GET", url, false); | |
req.send(null); | |
let key: string; | |
let value: string; | |
let colon: number; | |
const headers = req.getAllResponseHeaders().split(/\n/).filter(Boolean); | |
const data = headers.reduce((pairs, header) => { | |
// some values contains ":" then, the split method is not recommended | |
colon = header.indexOf(":"); | |
key = header.substring(0, colon); | |
value = header.substring(colon + 1); | |
pairs[key] = value; | |
return pairs; | |
}, new Object()); | |
data["Referer"] = document.referrer; | |
data["UserAgent"] = navigator.userAgent; | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment