Skip to content

Instantly share code, notes, and snippets.

@wakaba
Last active December 31, 2015 00:09
Show Gist options
  • Save wakaba/7905941 to your computer and use it in GitHub Desktop.
Save wakaba/7905941 to your computer and use it in GitHub Desktop.
javascript:
/* You are granted a license to use, reproduce and create derivative works of this document. */
var isDirectiveMetadata = {viewport: true, referrer: true};
var isDirectiveLink = {stylesheet: true, shortcut: true, prerender: true, prefetch: true};
function collectMetadata () {
var result = [];
var els = document.querySelectorAll ('meta[name], meta[property]');
for (var i = 0; i < els.length; i++) {
var el = els[i];
if (el.name) {
var name = el.name.replace (/[A-Z]+/g, function (v) { return v.toLowerCase () });
if (isDirectiveMetadata[name]) continue;
result.push ({type: 'metadata', name: name, value: el.content});
}
if (el.hasAttribute ('property')) {
result.push ({type: 'ogp', name: el.getAttribute ('property'), value: el.content});
}
}
var els = document.querySelectorAll ('link[rel]');
for (var i = 0; i < els.length; i++) {
var el = els[i];
var hasAlternate;
var hasStylesheet;
var rels = el.rel.replace (/[A-Z]+/g, function (v) { return v.toLowerCase () }).split (/[\x09\x0A\x0C\x0D\x20]+/).filter (function (v) {
if (v === 'alternate') hasAlternate = true;
if (v === 'stylesheet') hasStylesheet = true;
return !isDirectiveLink[v];
});
if (rels.length === 0) continue;
if (hasAlternate && hasStylesheet && rels.length === 1) continue;
for (var j = 0; j < rels.length; j++) {
result.push ({type: 'link', name: rels[j], value: el.href});
}
}
return result.sort (function (a, b) { return a.name > b.name ? 1 : -1 });
} // collectMetadata
var meta = collectMetadata ();
console.log (meta);
showMetadata (meta);
function showMetadata (metas) {
var container = document.createElement ('aside');
container.innerHTML = '<style scoped>.meta-list { background: white; color: black; display: block; position: relative; float: none; border: 2px blue solid; padding: 0.5em; font-size: 1rem; font-style: normal; font-weight: normal; text-align: left; z-index: 21000000; }</style><table class=meta-list></table>';
var listEl = container.lastChild;
for (var i = 0; i < metas.length; i++) {
var meta = metas[i];
var li = document.createElement ('tr');
li.innerHTML = '<th><code></code> (<span></span>)<td>';
li.firstChild.firstChild.textContent = meta.name;
li.firstChild.lastElementChild.textContent = meta.type;
li.lastChild.textContent = meta.value;
listEl.appendChild (li);
}
document.body.appendChild (container);
container.scrollIntoView ();
} // showMetadata
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment