Get an article's title/author/date info with Javascript
-
14-09-2020 - |
Question
I'm trying to build a bookmarklet that will get the current page/article's author and date information, for referencing purposes. I know that I can get the Page title and url with document.title and document.URL but I'm drawing a blank when it comes to the other information. Any ideas?
Solution
If the site puts such information in a META tag you can do this:
var author = "";
var info = document.getElementsByTagName('META');
for (var i=0;i<info.length;i++) {
if (info[i].getAttribute('NAME').toLowerCase()=='author') {
author = info[i].getAttribute('CONTENT');
}
}
For the site you mention in your comment, you need to do this non-standard processing
var author = "";
var other = document.getElementsByTagName('li');
for (var i=0;i<other.length;i++) {
if (other[i].className.toLowerCase()=='author') author=other[i].getElementsByTagName('a')[0].innerHTML;
}
alert(author)
}
OTHER TIPS
Does the HTML has a predefined format ? If yes , you could maybe parse the HTML or query the DOM to get the other info that you need .
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow