Utilizing HTML5 proxy technology opens up new possibilities:
Here is a handy utility function for reading all attributes within the given context:
(function($) {
$.fn.getAllAttributes = function() {
var attributes = {};
if( this.length ) {
$.each( this[0].attributes, function( index, attr ) {
attributes[ attr.name ] = attr.value;
} );
}
return attributes;
};
})(jQuery);
An example code snippet showcasing its usage:
$.get(
'http://www.corsproxy.com/' +
'en.wikipedia.org/wiki/Cross-origin_resource_sharing',
function(response) {
var elements = $(response);
for(var j = 0; j < elements.length; j++) {
if (typeof $(elements[j]).prop("tagName") != 'undefined' && $(elements[j]).prop("tagName").toLowerCase() == 'meta')
console.log($(elements[j]).getAllAttributes());
}
});
Explanation of how it functions: The corsproxy.com acts as a proxy utilizing CORS to overcome issues with the same origin policy. For more insights, refer to this explanation Loading cross domain endpoint with jQuery AJAX.
This method involves creating a jQuery object from the fetched page, searching for meta tags, and retrieving their attributes accordingly.
If relying on a third-party website concerns you, your alternative would be setting up a script on your server, sending the URL via AJAX, fetching the page on the server side, and delivering it back to the client for processing.
In scenarios where loading the entire page seems excessive, consider fetching only the initial segment containing the <head> tag which encapsulates all meta tags according to HTML specifications:
var xhr = new XMLHttpRequest();
xhr.addEventListener("progress", updateProgress, false);
xhr.addEventListener("load", transferComplete, false);
xhr.addEventListener("error", transferFailed, false);
xhr.addEventListener("abort", transferCanceled, false);
xhr.open("GET", 'http://www.corsproxy.com/en.wikipedia.org/wiki/List_of_law_clerks_of_the_Supreme_Court_of_the_United_States?1234', true);
xhr.send(null);
var offset = 0;
var responseText = '';
// Progress monitoring for server-to-client transfers
function updateProgress(evt) {
responseText = xhr.response.slice(offset);
console.log(responseText.length);
if (responseText.indexOf('</head>') != -1) {
var elements = $(responseText);
for(var k = 0; k < elements.length; k++) {
if (typeof $(elements[k]).prop("tagName") != 'undefined' && $(elements[k]).prop("tagName").toLowerCase() == 'meta')
console.log($(elements[k]).getAllAttributes());
}
xhr.abort();
}
offset = xhr.response.length;
}
function transferComplete(evt) {
console.log("Transfer completed successfully.");
}
function transferFailed(evt) {
console.log("An error occurred during file transfer.");
}
function transferCanceled(evt) {
console.log("User has cancelled the transfer.");
}