1

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?

Chris Armstrong
  • 3,585
  • 12
  • 42
  • 47

2 Answers2

2

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)
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • and for static files you can find document.lastModified useful if there is no date meta – mplungjan Jul 12 '10 at 09:25
  • 1
    PPS: Here is more information - note the part about meta sometimes changed to link rel : http://www.w3.org/TR/html401/struct/global.html#h-7.4.4.2 – mplungjan Jul 12 '10 at 09:29
  • thanks, that looks like it makes sense but I havn't been able to get it working yet, it returns blank when I test it on a Smashing Magazine post. Is this the kind of thing where every site is going to have a different way of putting this info in? – Chris Armstrong Jul 12 '10 at 10:36
  • 1
    Please see my edit for the answer to this part of your question – mplungjan Jul 12 '10 at 12:42
  • Thanks, do you think I'm going to have to have several loops to catch various methods of labelling 'author' info, or would there be a single meted that would catch the majority of cases? I notice even the New York Times doesn't seem to use the author meta, but has an element with a class of author. Would I be better looking for any element with a class of author? – Chris Armstrong Jul 13 '10 at 11:15
  • Hmm, I would say the website SHOULD use the standards which would be the meta or perhaps some other method like http://wiki.foaf-project.org/w/Autodiscovery - you might need a small database telling you what to look for at given sites.. – mplungjan Jul 13 '10 at 12:01
  • Thanks for flagging as answer. Good luck – mplungjan Jul 14 '10 at 09:59
0

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 .

NM.
  • 1,909
  • 1
  • 13
  • 21