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.
Blogger允许指定“搜索说明”,其显示为元标记,例如
<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).
在JSON文档中,没有任何方法可以访问它。所以,我写了一些代码来从帖子中获取元标记,但经过几个小时的阅读和实验,我完全被挫败了。我可以获取博客主页的元标记,但我无法获取帖子的元标记(虽然我可以获得我想要的任何其他标记 - 例如img)检查帖子显示元标记,但我无法获得任何(描述或其他)。
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.
这是一大堆代码。请注意,我已经尝试了100个不同版本,但没有运气,例如“meta”的变体,查看body,innerHTML,outerHTML,header等。还要注意我使用了很多额外的变量,因为我试图弄清楚发生了什么。对于无关的变量,空格等等,请不要太苛刻(虽然我必须承认我更喜欢它。)而且,为了简洁起见,我遗漏了处理从帖子中提取的数据的代码。
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?
有没有人知道Blogger是否有内置方法来获取搜索描述?否则,任何人都知道我的代码有什么问题?
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.]
注意:这不是“我如何从带有javascript的元标记获取信息?”的副本。我尝试了该问题中建议的每个解决方案,但没有一个能够工作,因为我根本无法获得帖子的元标记。解析元标记不是问题。获取帖子页面的元标记是个问题[或者更好的是,如果Blogger有一个API来获取帖子的搜索描述。]
fyi: code is for andrewsigal.blogspot.com if you want to see the pages.
fyi:代码是针对andrewsigal.blogspot.com的,如果你想看到这些页面的话。
1 个解决方案
#1
0
OK. Many hours of research, experimentation, and labor later, I have concluded the following (please correct me if I am wrong):
好。经过几个小时的研究,实验和劳动,我得出以下结论(如果我错了,请纠正我):
- Google has implemented a number of methods that apply to the "post" object which are not documented, e.g. innerHTML, textContent, snippet.
- 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.
- 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.
- 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.
- Google explicitly recommends use of search description but has then crippled its utility by failing to provide programmatic access to it in the DOM.
Google已经实施了许多适用于“帖子”对象的方法,这些方法没有记录,例如: innerHTML,textContent,snippet。
谷歌还没有实施一些有用的“帖子”方法,这些方法是为“博客”实施的,例如outerHTML,body,head等
在任何地方都没有描述“post”的完整DOM的文档。 Google的API文档已过时/不完整。找出是否实现给定方法的唯一方法是尝试它。
谷歌已将“搜索描述”添加到博客帖子中,但没有提供一种以编程方式从帖子本身以外的页面访问它。 “搜索描述”存储在帖子的标题中的元标记中,但是只有内部HTML可以从其他页面(例如索引页面)访问,因此其他页面不可用。
谷歌明确建议使用搜索描述,但由于未能在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.
我为我的博客模板创建了一个解决方法,如下所示:我创建了自己的“xmeta”标签,我将其放在帖子的HTML中。我将搜索描述中的文本放入我的xmeta标签中。我编写了代码来从post的innerHTML中获取xmeta标记,并在需要搜索描述时使用其文本。
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.
当然,这完全是黑客攻击。它需要重复信息(将搜索描述放在实际的“搜索描述”和我的xmeta标记中),并且容易出错,因为如果有人在一个地方而不是另一个地方更新了重复的文本,那么复制的文本可能会失去同步。
Sigh.
#1
0
OK. Many hours of research, experimentation, and labor later, I have concluded the following (please correct me if I am wrong):
好。经过几个小时的研究,实验和劳动,我得出以下结论(如果我错了,请纠正我):
- Google has implemented a number of methods that apply to the "post" object which are not documented, e.g. innerHTML, textContent, snippet.
- 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.
- 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.
- 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.
- Google explicitly recommends use of search description but has then crippled its utility by failing to provide programmatic access to it in the DOM.
Google已经实施了许多适用于“帖子”对象的方法,这些方法没有记录,例如: innerHTML,textContent,snippet。
谷歌还没有实施一些有用的“帖子”方法,这些方法是为“博客”实施的,例如outerHTML,body,head等
在任何地方都没有描述“post”的完整DOM的文档。 Google的API文档已过时/不完整。找出是否实现给定方法的唯一方法是尝试它。
谷歌已将“搜索描述”添加到博客帖子中,但没有提供一种以编程方式从帖子本身以外的页面访问它。 “搜索描述”存储在帖子的标题中的元标记中,但是只有内部HTML可以从其他页面(例如索引页面)访问,因此其他页面不可用。
谷歌明确建议使用搜索描述,但由于未能在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.
我为我的博客模板创建了一个解决方法,如下所示:我创建了自己的“xmeta”标签,我将其放在帖子的HTML中。我将搜索描述中的文本放入我的xmeta标签中。我编写了代码来从post的innerHTML中获取xmeta标记,并在需要搜索描述时使用其文本。
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.
当然,这完全是黑客攻击。它需要重复信息(将搜索描述放在实际的“搜索描述”和我的xmeta标记中),并且容易出错,因为如果有人在一个地方而不是另一个地方更新了重复的文本,那么复制的文本可能会失去同步。
Sigh.