在网站中嵌入美味的JSON-Feed

时间:2014-05-07 08:07:07

标签: javascript json rss feed delicious-api

我想将我的delicious.com链接嵌入我的CMS驱动网站。我试图了解https://delicious.com/rss上的说明,但我无法理解获取链接所需的内容,例如在我的网站上按标签排序。

直到2014年4月,它才使用了这样的代码:

<script type="text/javascript" src="http://feeds.delicious.com/v2/js/fachschule_gartenbau/Teichbau_allgemein?title=&amp;count=100&amp;sort=date&amp;extended"></script>

现在他们说他们将语法改为http://feeds.delicious.com/v2/json/ ...

我试图用“json”替换我的代码中的“js”,但这不起作用。有人有想法吗?


实际(和旧)代码例如:

<script type="text/javascript" src="http://feeds.delicious.com/v2/js/fachschule_gartenbau/gemüse+gemüsebau?title=&amp;count=100&amp;sort=alpha"></script>

头脑中没什么。

此代码不起作用。可以在http://www.fachschule-gartenbau.de/gemuese.htm

上进行检查

1 个答案:

答案 0 :(得分:1)

派对有点晚了,只是注意到标签云也为我打破了。您现在必须自己下载JSON,然后从中构建标记列表(或标记云)。或者,您当然也可以在PHP等服务器端构建一些东西,但我假设您希望坚持使用基于客户端的JavaScript解决方案。

步骤如下:

  1. 下载JSON。
  2. 解析JSON。
  3. 从中构建一些东西。
  4. 要下载JSON,我通常使用XMLHttpRequest(),但由于跨站点脚本保护,这在此处不起作用。所以我只是使用HTML <script>标记中的函数回调来调用JSON API(在此示例中我使用了用户名 delicious ):

    <script type="text/javascript" src="http://feeds.delicious.com/v2/json/tags/delicious?callback=printDeliciousTags"></script>
    

    此脚本标记将处理步骤1和2,然后将JSON的内容作为参数调用printDeliciousTags函数,以便我们可以从中构建一些内容。

    这是一个非常基本的实现,按字母顺序对标记进行排序,并将它们打印为无序列表:

    var username = "delicious";
    var ignoreFiletypes = true;
    var minCount = 1;
    var tagURLPrefix = "http://delicious.com/" + username + "/";
    
    function printDeliciousTags(tags)
    {
        var out = "";
        var visibleTags;
    
        // find all tags matching our min count and exclude filetypes as needed
        visibleTags = [];
        maxCount = 0;
        for (tag in tags) {
            if (tags.hasOwnProperty(tag)) {
                if (tags[tag] > maxCount) {
                    maxCount = tags[tag];
                }
                if (
                    tags[tag] >= minCount && 
                    (!ignoreFiletypes || tag.indexOf(":") == -1)
                ) {
                    visibleTags[visibleTags.length] = tag;
                }
            }
        }
    
        // sort the tags alphabetically (case-insensitive)
        visibleTags.sort(function (a, b) {
            return a.toLowerCase().localeCompare(b.toLowerCase());
        });
    
        // print the tags
        out += '<ul>';
        for (var i = 0; i < visibleTags.length; i++) {
            var tag;
            tag = visibleTags[i];
            out += '<li><a href="' + tagURLPrefix + tag + '">';
            out += tag + ' (' + tags[tag] + ' bookmarks)';
            out += '</a></li>';
        }
        out += '</ul>';
        document.getElementById("deliciousPlaceholder").innerHTML = out;
    

    基本无序列表的完整HTML示例:

    <html>
        <head>
        <title>Delicious JavaScript Tag API Replacement (Basic List)</title>
        </head>
        <body>
            <div id="deliciousPlaceholder"></div>
            <script type="text/javascript">
                var username = "delicious";
                var ignoreFiletypes = true;
                var minCount = 1;
                var tagURLPrefix = "http://delicious.com/" + username + "/";
    
                function printDeliciousTags(tags)
                {
                    var out = "";
                    var visibleTags;
    
                    // find all tags matching our min count and exclude filetypes as needed
                    visibleTags = [];
                    maxCount = 0;
                    for (tag in tags) {
                        if (tags.hasOwnProperty(tag)) {
                            if (tags[tag] > maxCount) {
                                maxCount = tags[tag];
                            }
                            if (
                                tags[tag] >= minCount && 
                                (!ignoreFiletypes || tag.indexOf(":") == -1)
                            ) {
                                visibleTags[visibleTags.length] = tag;
                            }
                        }
                    }
    
                    // sort the tags alphabetically (case-insensitive)
                    visibleTags.sort(function (a, b) {
                        return a.toLowerCase().localeCompare(b.toLowerCase());
                    });
    
                    // print the tags
                    out += '<ul>';
                    for (var i = 0; i < visibleTags.length; i++) {
                        var tag;
                        tag = visibleTags[i];
                        out += '<li><a href="' + tagURLPrefix + tag + '">';
                        out += tag + ' (' + tags[tag] + ' bookmarks)';
                        out += '</a></li>';
                    }
                    out += '</ul>';
                    document.getElementById("deliciousPlaceholder").innerHTML = out;
                }
            </script>
            <script type="text/javascript" src="http://feeds.delicious.com/v2/json/tags/delicious?callback=printDeliciousTags"></script>
        </body>
    </html>
    

    标签云的完整HTML示例:

    <html>
        <head>
        <title>Delicious JavaScript Tag API Replacement (Tag Cloud)</title>
        </head>
        <body>
            <div id="deliciousPlaceholder"></div>
            <script type="text/javascript">
                var username = "delicious";
                var minFont = 14;
                var maxFont = 36;
                var ignoreFiletypes = true;
                var minCount = 1;
                var tagURLPrefix = "http://delicious.com/" + username + "/";
    
                function printDeliciousCloud(tags)
                {
                    var out = "";
                    var maxCount;
                    var factor;
                    var offset;
                    var visibleTags;
    
                    // find all tags matching our min count and exclude filetypes as needed
                    visibleTags = [];
                    maxCount = 0;
                    for (tag in tags) {
                        if (tags.hasOwnProperty(tag)) {
                            if (tags[tag] > maxCount) {
                                maxCount = tags[tag];
                            }
                            if (
                                tags[tag] >= minCount && 
                                (!ignoreFiletypes || tag.indexOf(":") == -1)
                            ) {
                                visibleTags[visibleTags.length] = tag;
                            }
                        }
                    }
    
                    // sort the tags alphabetically (case-insensitive)
                    visibleTags.sort(function (a, b) {
                        return a.toLowerCase().localeCompare(b.toLowerCase());
                    });
    
                    // the tag cloud looks nicer if we use a logarithmic function, so fit
                    // one to our configured font sizes and min count
                    factor = (minFont - maxFont) / (Math.log(minCount) - Math.log(maxCount));
                    offset = maxFont - (factor * Math.log(maxCount));
    
                    // print the tag cloud
                    for (var i = 0; i < visibleTags.length; i++) {
                        var size;
                        var style;
                        var tag;
                        tag = visibleTags[i];
                        size = offset + (factor *  Math.log(tags[tag]));
                        style = 'text-decoration:none; color:#73adff; font-size:' + size + 'px;';
                        out += '<a href="' + tagURLPrefix + tag + '" style="' + style + '" title="' + tags[tag] + ' bookmarks">';
                        out += tag;
                        out += '</a>&nbsp; ';
                    }
                    document.getElementById("deliciousPlaceholder").innerHTML = out;
                }
    
    
            </script>
            <script type="text/javascript" src="http://feeds.delicious.com/v2/json/tags/delicious?callback=printDeliciousCloud"></script>
        </body>
    </html>