您是否建议使用博客创建一个随机评论我的博客的脚本?

时间:2017-05-20 20:07:02

标签: comments blogger blogspot

晚上好。 您是否建议使用博客创建一个随机评论我的博客的脚本? 谢谢

1 个答案:

答案 0 :(得分:0)

应该可以使用Blogger提供的全局评论Feed生成 -

http://blogname.blogspot.com/feeds/comments/default

相同的工作片段看起来像 -

<div id='stylify_random_comments'></div>
<script style='text/javascript'>
//<![CDATA[
var commentTitleOriginal, myLink, myDiv, myImage;
var main;

function getcomment(json) {
    var s;
    var entry = json.feed.entry[0];
    var commentTitle = entry.title.$t;
    commentTitleOriginal = commentTitle;
    if (isNaN(titleLength) || titleLength == 0) {
        commentTitle = '';
    } else if (commentTitle.length > titleLength) commentTitle = commentTitle.substring(0, titleLength) + "...";
    var commentUrl;
    for (var k = 0; k < entry.link.length; k++) {
        if (entry.link[k].rel == 'replies' && entry.link[k].type == 'text/html') {
            var commentText = entry.link[k].title;
            var commentUrl = entry.link[k].href;
        }
        if (entry.link[k].rel == 'alternate') {
            commentUrl = entry.link[k].href;
            break;
        }
    }
    if (showThumbs == true) {
        var thumbUrl = "";
        try {
            thumbUrl = entry.author["0"].gd$image.src;
            if (imgDim == "80" || imgDim == "85" || imgDim == "90" || imgDim == "95" || imgDim == "100") thumbUrl = thumbUrl.replace("/s72-c/", "/s104-c/");
        } catch (error) {
            if ("content" in entry) s = entry.content.$t;
            else s = "";

            if (thumbUrl == "" && mediaThumbsOnly == false) {
                a = s.indexOf("<img");
                b = s.indexOf("src=\"", a);
                c = s.indexOf("\"", b + 5);
                d = s.substr(b + 5, c - b - 5);
                if ((a != -1) && (b != -1) && (c != -1) && (d != "")) thumbUrl = d;

            }

        }
        if (thumbUrl == "" && showNoImage == true) thumbUrl = 'http://1.bp.blogspot.com/_u4gySN2ZgqE/SosvnavWq0I/AAAAAAAAArk/yL95WlyTqr0/s400/noimage.png';
    } //end ifcommenthumbs
    if (showcommentDate == true) {
        var commentdate = entry.published.$t;
        var cdyear = commentdate.substring(0, 4);
        var cdmonth = commentdate.substring(5, 7);
        var cdday = commentdate.substring(8, 10);
        var monthnames = new Array();
        monthnames[1] = "Jan";
        monthnames[2] = "Feb";
        monthnames[3] = "Mar";
        monthnames[4] = "Apr";
        monthnames[5] = "May";
        monthnames[6] = "Jun";
        monthnames[7] = "Jul";
        monthnames[8] = "Aug";
        monthnames[9] = "Sep";
        monthnames[10] = "Oct";
        monthnames[11] = "Nov";
        monthnames[12] = "Dec";
    } //end if date
    code = "";
    main = document.getElementById('stylify_random_comments');
    myDiv = document.createElement('div');
    myDiv.setAttribute("class", "stylify_item_title");
    myDiv.style.clear = "both";
    myDiv.style.marginTop = "4px";
    myLink = createLink(commentUrl, "_top", commentTitleOriginal)

    if (commentTitle != '') myDiv.appendChild(myLink);
    main.appendChild(myDiv);
    if (commentTitle != '') myLink.innerHTML = commentTitle;




    if (showThumbs == true && thumbUrl != "") {
        myImage = document.createElement('img');
        myImage.style.border = "0px solid transparent";
        myImage.style.margin = "5px";
        myImage.style.boxShadow = "0 0 0px rgba(0, 0, 0, 0.3)";

        myImage.setAttribute("src", thumbUrl);
        myImage.style.cssFloat = imgFloat;
        myImage.style.styleFloat = imgFloat;
        //myImage.setAttribute("alt", commentTitleOriginal);
        myImage.setAttribute("width", imgDim);
        //myImage.setAttribute("align", imgFloat);
        myImage.setAttribute("height", imgDim);
        myLink = document.createElement('a');
        myLink.setAttribute("href", commentUrl + "?utm_source=blog&utm_medium=gadget&utm_campaign=stylify_random_comments");
        myLink.setAttribute("target", "_top");
        myLink.setAttribute("title", commentTitleOriginal);
        myLink.appendChild(myImage);

        myDiv = document.createElement('div');
        myDiv.setAttribute("class", "stylify_item_thumb");
        myDiv.appendChild(myLink);
        main.appendChild(myDiv);
    }




    try {
        if ("content" in entry) {
            var commentContent = entry.content.$t;
        } else if ("summary" in entry) {
            var commentContent = entry.summary.$t;
        } else var commentContent = "";
        var re = /<\S[^>]*>/g;
        commentContent = commentContent.replace(re, "");


        if (showSummary == true) {
            myDiv = createDiv("stylify_item_summary");

            if (commentContent.length < summaryLength) {
                myDiv.innerHTML = commentContent;
            } else {
                commentContent = commentContent.substring(0, summaryLength);
                var quoteEnd = commentContent.lastIndexOf(" ");
                commentContent = commentContent.substring(0, quoteEnd);
                myDiv.innerHTML = commentContent + '...';
            }

            main.appendChild(myDiv);
        }
    } //end try
    catch (error) {}

    myDiv = createDiv("stylify_item_meta");
    myDiv.style.clear = "both";
    myDiv.style.marginBottom = "4px";

    var flag = 0;
    if (showcommentDate == true) {
        myDiv.appendChild(document.createTextNode(monthnames[parseInt(cdmonth, 10)] + '-' + cdday + '-' + cdyear));
        flag = 1;
    }

    if (showCommentCount == true) {
        if (flag == 1) {
            myDiv.appendChild(document.createTextNode(" | "));
        }
        if (commentText == '1 Comments') commentText = '1 Comment';
        if (commentText == '0 Comments') commentText = 'No Comments';
        var myLink = createLink(commentUrl, "_top", commentText + " on " + commentTitleOriginal)
        myDiv.appendChild(myLink);
        myLink.innerHTML = commentText;
        flag = 1;;
    }

    if (showReadMore == true) {
        if (flag == 1) {
            myDiv.appendChild(document.createTextNode(" | "));
        }
        var myLink = createLink(commentUrl, "_top", commentTitleOriginal)
        myDiv.appendChild(myLink);
        myLink.innerHTML = readMore + " &raquo;";
        flag = 1;;
    }



    if (flag == 1 || showSummary || commentTitle != "") main.appendChild(myDiv);


}

function getRandom(json) {
    var feedUrl = '/feeds/comments/default';
    if (mediaThumbsOnly || !showThumbs) feedUrl = feedUrl.replace("comments/default", "comments/summary");
    totalcomments = parseInt(json.feed.openSearch$totalResults.$t);
    var rand = [];
    if (numberOfcomments > totalcomments) numberOfcomments = totalcomments;
    if (numberOfcomments > 15) numberOfcomments = 15;
    while (rand.length < numberOfcomments) {
        var randomNumber = Math.ceil(Math.random() * totalcomments);
        var found = false;
        for (var i = 0; i < rand.length; i++) {
            if (rand[i] == randomNumber) {
                found = true;
                break;
            }
        }
        if (!found) rand[rand.length] = randomNumber;
    }
    var head = document.getElementsByTagName("head")[0] || document.documentElement;

    for (var i = 0; i < rand.length; i++) {

        script = document.createElement("script");
        script.src = feedUrl + "?start-index=" + rand[i] + "&max-results=1&alt=json-in-script&callback=getcomment";
        script.charSet = "utf-8";
        head.appendChild(script);
    }



}

function createDiv(className) {
    var myDiv = document.createElement('div');
    myDiv.setAttribute("class", className);

    return myDiv;
}


function createLink(href, target, title) {

    var myLink = document.createElement('a');
    if (href.indexOf("?utm_source=") == -1) href = href + "?utm_source=blog&utm_medium=gadget&utm_campaign=stylify_random_comments";
    myLink.setAttribute("href", href);
    myLink.setAttribute("target", target);
    myLink.setAttribute("title", title);


    return myLink;
}

//]]>
</script>
<script style='text/javascript'>
var numberOfcomments = 5;
var showcommentDate = false;
var showSummary = true;
var summaryLength = 200;
var titleLength = 100;
var showCommentCount = false;
var showThumbs = true;
var showNoImage = false;
var imgDim = 50;
var imgFloat = 'left';
var myMargin = 0;
var mediaThumbsOnly = true;
var showReadMore = false;
var readMore = 'More';
</script>
<script src='/feeds/comments/default?max-results=1&alt=json-in-script&callback=getRandom'></script>