Last active
April 19, 2017 22:44
-
-
Save rufus2021/dd43479296733b98a9ef8d5785ecc7c0 to your computer and use it in GitHub Desktop.
get meta tag and title value from the DOM
This file contains 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 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