To "release" comments on a post using this API, you must start it using code below
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load("gdata", "1.x", { packages : ["blogger"] });
</script>
After you call google.load(), then you create a new function for blogger service object. Format of this blogger service is as follow
function getYourComment(){
var content = document.getElementById('content');
var bloggerService =
new google.gdata.blogger.BloggerService('com.appspot.interactivesampler');
var feedUri = 'http://www.threelas.com/feeds/posts/default'; //threelas feed Uri
var bloggerService =
new google.gdata.blogger.BloggerService('com.appspot.interactivesampler');
var feedUri = 'http://www.threelas.com/feeds/posts/default'; //threelas feed Uri
// Handle blog post feed code here
// Handle comments code here
// Handle error code here
bloggerService.getBlogPostFeed(feedUri, handleBlogPostFeed, handleError);
}
Threelasappname is application name. You must fill this application name, bloggerService will call method inside them, one of them is
getBlogPostFeed(<string|google.gdata.blogger.BlogPostQuery> uriOrQuery,
<function(Object)> continuation, <function(Error)|undefined> opt_errorHandler, opt_param)
So, you call it after bloggerService
var handleBlogPostFeed = function(blogPostsFeed) {
// Display blog's title and clear 'Loading...' message, this is using getBlogPostFeed()
content.innerHTML = '<p><strong>Blog:</strong> '
+ '<a href="'
+ blogPostsFeed.feed.getLink('alternate').getHref()
+ '">'
+ blogPostsFeed.feed.getTitle().getText()
+ '</a></p>';
var postEntry = blogPostsFeed.feed.getEntries()[0]; // only get first post
var commentsFeedUri = postEntry.getRepliesLink().getHref();
// This is using getBlogCommentFeed()
bloggerService.getBlogCommentFeed(commentsFeedUri, handleCommentFeed,
handleError);
};
If you see the code above, we must call getBlogCommentFeed(), where this function has format as follow
getBlogCommentFeed(commentsFeedUri, handleCommentFeed, handleError)
commentsFeedUri is comment feed uri of a posting, handleCommentFeed is a function to access comments of a post from commentFeedUri, handleError is optional parameter to handle your application if something error is happen. After getBlogCommentFeed() is success to call comments from comment feed, then we can access all comments via handleCommentFeed like below
// The callback method used when getBlogCommentFeed() returns comments
var handleCommentFeed = function(commentsFeedRoot) {
var commentEntries = commentsFeedRoot.feed.getEntries();
// Buffer HTML until execution completes
var html = '<p><strong>Recent Comments:</strong></p>';
if (commentEntries.length > 0) {
html += '<ul>';
for(var i = 0, commentEntry; commentEntry = commentEntries[i]; i++) {
var commentTitle = commentEntry.getTitle().getText();
var commentLink = commentEntry.getLink('alternate').getHref();
html += '<li>'
+ '<a href="' + commentLink + '">'
+ commentTitle + '</a></li>';
}
html += '</ul>';
} else {
html += '<p>No comments found for that post.</p>';
}
// Output buffered HTML and clear 'Loading...' message.
content.innerHTML += html;
};
And you can make error message like below
var handleError = function(error) {
content.innerHTML = '<pre>' + error + '</pre>';
};
After that, you have to tell the google.load to wait until the page finishes loading and then call your code
google.setOnLoadCallback(getYourComment);
Below is the complete code for this tutorial
}
Threelasappname is application name. You must fill this application name, bloggerService will call method inside them, one of them is
getBlogPostFeed(<string|google.gdata.blogger.BlogPostQuery> uriOrQuery,
<function(Object)> continuation, <function(Error)|undefined> opt_errorHandler, opt_param)
So, you call it after bloggerService
var handleBlogPostFeed = function(blogPostsFeed) {
// Display blog's title and clear 'Loading...' message, this is using getBlogPostFeed()
content.innerHTML = '<p><strong>Blog:</strong> '
+ '<a href="'
+ blogPostsFeed.feed.getLink('alternate').getHref()
+ '">'
+ blogPostsFeed.feed.getTitle().getText()
+ '</a></p>';
var postEntry = blogPostsFeed.feed.getEntries()[0]; // only get first post
var commentsFeedUri = postEntry.getRepliesLink().getHref();
// This is using getBlogCommentFeed()
bloggerService.getBlogCommentFeed(commentsFeedUri, handleCommentFeed,
handleError);
};
If you see the code above, we must call getBlogCommentFeed(), where this function has format as follow
getBlogCommentFeed(commentsFeedUri, handleCommentFeed, handleError)
commentsFeedUri is comment feed uri of a posting, handleCommentFeed is a function to access comments of a post from commentFeedUri, handleError is optional parameter to handle your application if something error is happen. After getBlogCommentFeed() is success to call comments from comment feed, then we can access all comments via handleCommentFeed like below
// The callback method used when getBlogCommentFeed() returns comments
var handleCommentFeed = function(commentsFeedRoot) {
var commentEntries = commentsFeedRoot.feed.getEntries();
// Buffer HTML until execution completes
var html = '<p><strong>Recent Comments:</strong></p>';
if (commentEntries.length > 0) {
html += '<ul>';
for(var i = 0, commentEntry; commentEntry = commentEntries[i]; i++) {
var commentTitle = commentEntry.getTitle().getText();
var commentLink = commentEntry.getLink('alternate').getHref();
html += '<li>'
+ '<a href="' + commentLink + '">'
+ commentTitle + '</a></li>';
}
html += '</ul>';
} else {
html += '<p>No comments found for that post.</p>';
}
// Output buffered HTML and clear 'Loading...' message.
content.innerHTML += html;
};
And you can make error message like below
var handleError = function(error) {
content.innerHTML = '<pre>' + error + '</pre>';
};
After that, you have to tell the google.load to wait until the page finishes loading and then call your code
google.setOnLoadCallback(getYourComment);
Below is the complete code for this tutorial
Thanks helped alot
ReplyDeleteyou are welcome
ReplyDelete