Last active
March 26, 2020 15:21
-
-
Save matiaslopezd/04b3cd5a2a0088ad2e5cb114e3e8cd1a to your computer and use it in GitHub Desktop.
Simple object for get headers and properties of website with ajax
This file contains hidden or 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
| const myHeaders = { | |
| get: function getHeader(url = window.location, obj = {}) { | |
| return new Promise((resolve) => { | |
| function checkWhichSplit(string = '') { | |
| if (string.includes(',')) return ','; | |
| else if (string.includes(';')) return ';'; | |
| else return undefined; | |
| } | |
| function parseHeader(string = '') { | |
| const splitByLines = string.split('\n'); | |
| splitByLines.map(value => { | |
| const splitByDoubleDot = value.split(':'); | |
| obj[splitByDoubleDot[0]] = splitByDoubleDot[1]; | |
| }); | |
| Object.keys(obj).map(key => { | |
| const splitCharacter = checkWhichSplit(obj[key]); | |
| if (splitCharacter) obj[key] = obj[key].split(splitCharacter); | |
| }); | |
| return obj; | |
| } | |
| function doRequest() { | |
| const request = new XMLHttpRequest(); | |
| request.open('HEAD', url); | |
| request.onload = function() { | |
| const headers = request.getAllResponseHeaders(); | |
| const parsedHeaders = parseHeader(headers); | |
| resolve({ plain: headers, parsed: parsedHeaders }); | |
| }; | |
| request.send(); | |
| } | |
| doRequest(); | |
| }); | |
| }, | |
| property: function getProperty(obj = {}, property = '', condition = (e) => e) { | |
| return condition(obj[property]); | |
| } | |
| }; |
This file contains hidden or 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
| const myHeaders={get:function(a=window.location,b={}){return new Promise(c=>{function d(a=""){return a.includes(",")?",":a.includes(";")?";":void 0}function e(a=""){const c=a.split("\n");return c.map(a=>{const c=a.split(":");b[c[0]]=c[1]}),Object.keys(b).map(a=>{const c=d(b[a]);c&&(b[a]=b[a].split(c))}),b}(function(){const b=new XMLHttpRequest;b.open("HEAD",a),b.onload=function(){const a=b.getAllResponseHeaders(),d=e(a);c({plain:a,parsed:d})},b.send()})()})},property:function(a={},b="",c=a=>a){return c(a[b])}}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use
This is a simple object to get headers and properties. The object have two functions for get headers and specific property.
get: Function to get headers in plain and parsed format.property: Function to evaluate property value, like 'x-frame-options' includes 'sameorigin' or simply return the value.Get Headers
By default the function for get the headers works with
window.location, but you can set the following values:url: URL to get headers including the protocolobj: Object in case you want join with headersEvaluate or get property
Here you have three parameters:
obj: Pass the parsed header object (you can get withmyHeaders.get())property: Pass the property to evaluate like 'x-frame-options'.condition: Pass a function which evaluate the property. Example:(value) => value.includes('sameorigin').