MrAndrew
MrAndrew

Reputation: 1

Javascript access to Blogger "Search Description" meta tag

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

Answers (1)

MrAndrew
MrAndrew

Reputation: 1

OK. Many hours of research, experimentation, and labor later, I have concluded the following (please correct me if I am wrong):

  1. Google has implemented a number of methods that apply to the "post" object which are not documented, e.g. innerHTML, textContent, snippet.
  2. Google has not implemented a number of methods for "post" that would be helpful, and which are implemented for "blog", e.g. outerHTML, body, head, etc.
  3. There is no document anywhere which describes the full DOM for "post". Google's API documentation is obsolete/incomplete. The only way to find out if a given method is implemented is to try it.
  4. Google has added "search description" to blogger posts, but has not provided a programmatic way to get to it from pages other than the post itself. The "search description" is stored in a meta tag in the header of the post, but only the inner HTML is accessible from other pages (e.g. the index page), so it is unavailable from other pages.
  5. Google explicitly recommends use of search description but has then crippled its utility by failing to provide programmatic access to it in the DOM.

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

Related Questions