Skip to content

Instantly share code, notes, and snippets.

@matiaslopezd
Last active March 26, 2020 15:21
Show Gist options
  • Save matiaslopezd/04b3cd5a2a0088ad2e5cb114e3e8cd1a to your computer and use it in GitHub Desktop.
Save matiaslopezd/04b3cd5a2a0088ad2e5cb114e3e8cd1a to your computer and use it in GitHub Desktop.
Simple object for get headers and properties of website with ajax
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]);
}
};
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])}};
@matiaslopezd
Copy link
Author

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 protocol
  • obj: Object in case you want join with headers
// async/await way
const { plain, parsed } = await myHeaders.get();


// promise way
myHeaders.get().then(function ({ plain, parsed }) {
  // Do something
});

Evaluate or get property

Here you have three parameters:

  • obj: Pass the parsed header object (you can get with myHeaders.get())
  • property: Pass the property to evaluate like 'x-frame-options'.
  • condition: Pass a function which evaluate the property. Example: (value) => value.includes('sameorigin').
// Get headers
const { plain, parsed } = await myHeaders.get();

// Evaluate
myHeaders.property(parsed, 'x-frame-options', (value) => {
   return value.toLowerCase().includes('sameorigin');
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment