Un nuevo hack de Hoctro's Place que podéis ver en funcionamiento al final de las entradas de este blog. Despliega todos los comentarios del blog o el número que deseéis mostrar (en mi caso, diez... que ya está bien).
Y la forma de insertar este hack es de lo más sencilla:
1º.- Un script, a insertar antes del cierre /head de la plantilla extendida
<script type='text/javascript'>
// Developed by Hoctro - All rights reserved 2007
// This credit must be included in all your derived usages.
// "cb" is intended to be a common library, where different widgets would
// utitlize the shared operations such as getTitle, getLink, etc. from a json object.
var cb = {
// search function requires these parameters:
// 1. query: a blogger address, such as "hoctro.blogspot.com",
// 2. type: type of return data, either "comments" or "posts",
// 3. start: the start-index parameter (where to start extracting data)
// 4. increment: the number of elements the json will get back. (the smaller value, the faster time to travel back)
// 5. func: the returned function the json object will feed.
search: function(query, type, start, increment, func) {
var script = document.createElement('script');
script.setAttribute('src', 'http://' + query + '/feeds/' + type + '/default?alt=json-in-script&start-index='
+ start + '&max-results=' + increment + '&callback=' + func);
script.setAttribute('type', 'text/javascript');
document.documentElement.firstChild.appendChild(script);
},
// getTotalResults needs the json object, and it'll return an array of the available members.
getTotalResults: function(json) {
return json.feed.openSearch$totalResults.$t;
},
// getStartIndex gets the start index of a search inside an json object.
getStartIndex: function(json) {
return json.feed.openSearch$startIndex.$t;
},
// getLink return a href link if "name" matches the content *inside "a" tags) of the link
getLink: function(entry, name) {
var alturl;
for (var k = 0; k < entry.link.length; k++) {
if (entry.link[k].rel == name)
alturl = entry.link[k].href;
}
return alturl;
},
// getTitle gets the title of the search inside an json object.
getTitle: function(entry) {
return entry.title.$t;
},
// getContent gets the array content of the search inside an json object.
getContent: function(entry) {
return entry.content.$t;
},
// getCommentAuthor: gets the commenter inside an json object.
getCommentAuthor: function(entry) {
return entry.author[0].name.$t;
}
};
</script>
2º.- Insertar un nuevo widget en la plantilla (yo lo he colocado bajo las entradas del blog):
<b:widget id='HTML1' locked='false' title='Latest Comments' type='HTML'>
<b:includable id='main'>
<!-- only display title if it's non-empty -->
<b:if cond='data:title != ""'>
<h2 class='title'><data:title/></h2>
</b:if>
<div class='widget-content'>
<data:content/>
<div id='data'/>
<div id='hoctro'>Comment Widget by <a href='http://hoctro.blogspot.com'>Hoctro</a></div>
</div>
<script type='text/javascript'>
// Developed by Hoctro - All rights reserved 2007
// This credit must be included in all your derived usages.
// This object stores current and future data to be used in conjuction with
// the cW object. Right now it only stores the desired value of returned comment
// if you want all comments back (!), use the value -1 instead (nComments : -1)
var uC = {
nComments : 10
};
// this is the special object, one specialized for each widget, to be written in such
// a way that myriad of these objects all used the same library "cb" defined above.
var cW = {
// private variable to store the total results
totCount : 0,
// the number of elements for each json call
increment : 10,
// this is the place to define the blog name for subsequent retrieval data
web : "hoctro.blogspot.com",
// This is the callback funtion and is called everytime we have a result. If we
// call 10 times, this function will be automatically called 10 times, for example.
// Inside, we make a loop through all elements. For each element, the common functions
// from "cb" is called (reused) repeatedly to form a piece of HTML "text".
// At the end, "innerHTML" is called to add this data to the "data" div we defined inside
// the widget.
listComments: function(json, tag) {
var text="";
for (var i = 0; i < json.feed.entry.length; i++) {
var entry = json.feed.entry[i];
text += "<b>" + cb.getCommentAuthor(entry) + "</b>";
text += "<a href='" + cb.getLink(entry, "alternate")
+ "'>" + " " + "wrote:</a> <br/>";
text += "<ul><li>" + cb.getContent(entry) +
"</li></ul><br/>";
}
var p = document.createElement('div');
document.getElementById(tag).appendChild(p);
p.innerHTML = text;
},
// This function serves as a "stub", allowing multiple parameters to be called
// instead of the limitation of "one" default value. For example, the callback
// is "callcommentJson" with the passing variable "json", but we empower this by
// turning it into "listComments" where we can pass "data" as the name of the
// div
callcommentJson: function(json) {
this.listComments(json, "data");
},
// "getCo" acts as the mediator, as it can call as many times and the volume each time
// as it wants to match the parameters set from the user (uC.nComment, cW.increment)
// It does this by getting the total results then do some comparisons and make calls
// accordingly (using a while loop) until the conditions are met.
getCo : function(json) {
this.totCount = cb.getTotalResults(json);
var count = (uC.nComments < 0) ? this.totCount : uC.nComments;
var i=1;
while(i<=count) {
var incr = (count-i < this.increment) ? count-i+1 : this.increment;
cb.search( cW.web, "comments", i, incr, 'cW.callcommentJson');
if (incr==0) incr=incr+1;
i = i+incr;
}
}
};
// This is the function call that starts it all:
// Essentially, this call sends a json call to ask for the first two elements
// of the "comments" feed of "cW.web" (hoctro.blogspot.com), and the returning
// json object then fed to "getCo" function.
// "getCo" then finds out how many calls are needed to satisfy "uC.nComment", "cW.increment"
// then make these calls. For each call, the results are fed into "callcommentJson" and through it
// "listComments" is called. Here, json data are extracted and appended to "data" div.
cb.search( cW.web, "comments", 1, 2, 'cW.getCo');
</script>
</b:includable>
</b:widget>
Aquí tenéis que cambiar el nombre de la web por el de vuestro blog y, en la tag var uC = {
nComments : 10
}; el número de comentarios que queréis desplegar. Si queréis verlos todos, entonces sustituid el "10" por un "-1".
Además, he quitado la rerefencia a la autoría del widget (que por eso la pongo ya en su sitio, o sea: aquí)y sustituído, en el segundo código, la expresión "wrote" por "comenta".
Y eso es todo... ¡Bienvenido Hoctro! (a mi juicio, el primer genio de la red... del que aprendí muchísimo :) )
Vía: Education and Technology
✍ ¿Quieres hacer algún comentario? ✍