Reputation: 1
In my main blogger page I show descriptions of the posts on the blog. Currently it is just a snippet from the top of the post text.
Blogger allows specifying a "search description", which shows up as a meta tag, e.g.
<meta content="This is what the post is really about." name="description">
If the post has a search description, I would like to show that instead of the snippet. However, I have been unable to grab the search description for the posts.
In the JSON docs there is no indication of any way to access this. So, I wrote some code to get the meta tags from the posts, but after hours of reading and experimenting I have been utterly thwarted. I can get the meta tags for the blog homepage, but I cannot get the meta tags for the posts (though I can get any other tag I want - such as img) Examining the posts shows meta tags, but I cant get any of them (description or otherwise).
Here is a chunk of code. Note that I have tried 100 different versions of this without luck, e.g. variations on "meta", looking at the body, the innerHTML, the outerHTML, the header, etc. Also note that I am using lots of extra variables because I was trying to figure out what was going on. Please dont be too harsh on me for extraneous variables, white space, etc (though I must admit I prefer it that way.) And, for concision I have left out the code that deals with the data extracted from the posts.
function showImgAndDescForPost(postId)
{
var postT = document.getElementById(postId);
imgtag = "";
ifrtag = "";
img = postT.getElementsByTagName("img");
ifr = postT.getElementsByTagName("iframe");
// Get the description meta tag, if any
// BUGBUG: fails to get the meta tags. Why?
var metas = postT.getElementsByTagName("meta");
mLen = metas.length;
var postdescr = "";
for (var iM=0; iM < mLen; iM++)
{
metaattr = metas[iM].getAttribute('name');
postdescr += metaattr + ' + ';
if (metas[iM].getAttribute('name') == 'description')
{
postdescr = metas[iM].getAttribute('content');
break;
}
}
if (ifr.length >= 1)
// deal with iframe
else
// deal with img
if (postdescr.length > 1)
// deal with search description
else
// get snippet of text for description
}
Does anyone know if Blogger has a builtin method to get the search description? Otherwise, anyone know what is wrong with my code?
Note: This is not a duplicate of "How do I get the information from a meta tag with javascript?" I tried each of the solutions suggested in that question, but none of them worked because I cannot get the meta tags of the posts at all. Parsing the meta tags isnt the issue. Getting to the meta tags of the post pages is the problem [or better yet, if Blogger has an API for getting the search descriptions for posts.]
fyi: code is for andrewsigal.blogspot.com if you want to see the pages.
Upvotes: 0
Views: 953
Reputation: 1
OK. Many hours of research, experimentation, and labor later, I have concluded the following (please correct me if I am wrong):
I have created a workaround for the problem for my blogger template as follows: I have created my own "xmeta" tag which I place in the HTML of the post. I put the text from the search description into my xmeta tag. I have written code to get the xmeta tag from the post's innerHTML and use its text when I need the search description.
Of course, this is a total hack. It requires duplication of information (putting the search description both in the actual "search description" and in my xmeta tag), and is error prone since the duplicated text can get out of sync if someone updates it in one place but not the other.
Sigh.
Upvotes: 0