2009年5月5日星期二

Blogger “手气不错” 小工具

虽然“随机文章”在 WordPress 的世界里早已成为一个最平凡不过的链接,但是 Blogger 官方并没有提供一个快捷地添加“随机文章”功能的方法。这里,一位国外资深的 Blogger 用户给出了他编写的“手气不错”小工具,供 Blogger 用户使用,同时兼容了 Blogger 布局(Layout)与模板(Template)。

这个小工具可以轻松地在作者制作的页面上(Blogger Feeling Lucky Widget)直接导入到 Blogger 中。
页面上的“Are ya feeling lucky, punk?”就是将显示在小工具里的“随机文章”链接,可以任意修改为你喜欢的字句。

下面是作者对这个小工具的介绍,包含了它的工作原理,希望能对各位 Blogger 有帮助。
Notes
备注

For those that may be interested, here's how it works. Nothing really earth shattering going on. In the initial page load, the scripts make a json call to the feed url. Here I'm only interested in getting the total number of posts in the blog, so the call is made to the summary feed (to keep load time minimal) and with only one entry returned. The total number of posts is returned in New Blogger in the openSearch:totalResults field, so take the total number of posts and then use standard means to generate a random number within that range. Then the link is created using that number as it's variable.
对于想了解这个工具是如何工作的朋友,这里我做一下介绍。这并不是什么大制作。在最初页面载入时,这段脚本会向 Blog 的摘要 Feed(为了保证最优化载入时间)做一个 JSON 调用,并返回一个条目。文章的总数被返回到新版 Blogger 的 openSearch:totalResults 域,这样使用这个文章总数并使用标准平均值来生成一个在此范围内的随机数字。而后,这个链接就用这个数字作为变量创建了。

Now at this point we don't have a valid url yet, just a number. So when the link is clicked, another quick json call is made, using the random number generated as the start-index parameter to get that entry in the feed only, a quick parse of the results to grab that entries url, then good old document.location (Thanks to Pete changed to window.location and a few other small tweaks to keep the back button working) to switch to it.
现在我们还没有一个有效的 URL,只是一个数字。所以当这个链接被点击之后,另一个快速 JSON 调用被创建,只是使用这个生成的随机数字作为起始索引参数从 Feed 中获取条目,从结果中做快速解析来得到条目的 URL,然后用美妙的 document.location (感谢 Pete 更换为 window.location,并做了一些小调整来让“后退”按钮正常工作)来切换到这个地址。

Old Blogger did have a similar tweak by Mario Brhemenz, but like a lot of our Old Blogger stuff relied on an outside service (del.icio.us)to work. I don't know that I've seen one for New Blogger before.
旧版 Blogger 也有一个由 Mario Brhemenz 制作的类似的技巧,但就像其他一大堆旧版 Blogger 技巧一样,需要依靠外围的服务(del.icio.us)来使用。在我看到新版 Blogger 的技巧之前我并不知道这些。

I think everything works as it should, but if you find any issues lemme know.
我想一切都应该能按预想那样正常工作,但如果你发现了问题,请通知我。

4 条评论:

  1. 我以为在后台设置了feed全文输出,就不会有摘要输出了,结果我又错了:(
    很明显blogger的feed非常强力、非常开放,这也是blogger hack的重要基础。
    我曾看到有心人公布过blogger的一些“隐秘”feed地址,看来他公布的也并不全面。
    另强烈要求blogger的feed改成默认按发表日期顺序显示。

    回复删除
  2. 先留个言,回头再仔细看看。
    好多hack都提到了JSON,不知道这究竟是什么?Marcher兄可否专门出文一篇,介绍一下啊。呵呵

    回复删除
  3. @Sandlong: ?orderby=published 默认是按最后修改的发布时间排序吧?这样应该也可以了。
    @小毅: 不好意思,我对json也不了解多少,不敢随便乱写。建议你还是自己找一些书学习一下吧。

    回复删除

请勿张贴商业广告。评论可能需要审核。
No advertisement. Comment review enabled.