Skip to content

Instantly share code, notes, and snippets.

@rufus2021
Last active April 19, 2017 22:44
Show Gist options
  • Save rufus2021/dd43479296733b98a9ef8d5785ecc7c0 to your computer and use it in GitHub Desktop.
Save rufus2021/dd43479296733b98a9ef8d5785ecc7c0 to your computer and use it in GitHub Desktop.
get meta tag and title value from the DOM
const keys = [
'description',
'publishDate',
'keywords',
'author',
'og:site_name',
'og:title',
'og:url',
'og:description',
'og:type',
'twitter:card',
'twitter:site',
'twitter:title',
'twitter:description',
'twitter:image:src',
'twitter:domain'
];
function getSEOData() {
const collection = {};
const metaTags = document.getElementsByTagName('meta');
const title = document.getElementsByTagName('title')[0].textContent;
collection.title = title;
for (let i = 0; i < metaTags.length; i++) {
const metaTag = metaTags[i];
const metaAttributes = metaTag.attributes;
const metaName = metaAttributes.name ? metaAttributes.name.value : '';
const metaProperty = metaAttributes.property ? metaAttributes.property.value : '';
const metaContentValue = metaAttributes.content && metaAttributes.content.value;
if (keys.indexOf(metaName) > -1) {
collection[metaName] = metaContentValue;
}
if (keys.indexOf(metaProperty) > -1) {
collection[metaProperty] = metaContentValue;
}
}
console.log(JSON.stringify(collection));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment