Hoctro's Place - Góc Học Trò

I am now more avtively creating and adding contents to my music blog, please take a look. http://hoctroyoutube2010.blogspot.com/

Introducing The New Related Articles Widget

Hi friend,

This is my first take on the new Related Articles Widget.

Please cut and paste the code below to a new HTML Widget from your Page Elements to see if it is working.



I cut and paste the same code to create a new widget, located at the end of this post.




You don't have to get rid of the existing Related Articles widget on your blog. This new one is a totally independent, self-contained widget!

As you can see, the widget forms a query of all the "keyword" (in this case "labels") in this post, queries, and gets a resulting list for the most relevant posts from your blog. It's just like you query a list of terms and get results from the google search at http://www.google.com/ .




The result is, in my humble opinion, a truly "most related articles" relevant set of links, unlike my previous attempts - where only the newest posts from each label showed up. This time, we employ Google's famous ranking algorithm to do the "related" part for us.

I will explain the motivations, excecution and nuances of this "beta" widget in a follow-up post. There are ways to modify the look of the results, such as showing the snippet text or not, or use CSS to alter the look of the widget.


Update: See my explanations here:

Part 1: http://hoctro.blogspot.com/2010/08/dissecting-new-related-articles-part-1.html

Part 2: in progress.


Regards,

<!-- Related Articles - Take 5 - Using JQuery & Google Search API - by Hoctro 8/2010 -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script language="Javascript" type="text/javascript">//<![CDATA[

//var website = "hoctro.blogspot.com";
var website = getBaseURL();
var hoctroRelated = "site:" + website + " ";
var labelArray = new Array();

function getParams() {
  $('div.post-footer-line span.post-labels a').each(function(idx, el) {
    var test = 0;
    var textLabel = $(el).html();
    for (var i = 0; i != labelArray.length; i++) {
      if (labelArray[i] == textLabel.toString()) test = 1;
    }
    if (test == 0) {
      labelArray.push(textLabel.toString());
      hoctroRelated = hoctroRelated + '"' + textLabel.toString() + '" OR ';
    }
  }); // each
}
// Callback main function
$(document).ready(function() {
  getParams() ;
}); // function

function OnLoadRelated() {
  // create a search control
  var searchControl = new google.search.SearchControl();

  // create a draw options object so that we
  // can position the search form root

  var options = new google.search.DrawOptions();
  options.setSearchFormRoot(document.getElementById("searchHoctroRelatedForm"));

  // populate with searcher
  var blogSearch = new google.search.WebSearch();
  blogSearch.setUserDefinedLabel("Related Articles");
  var options2 = new google.search.SearcherOptions();
  options2.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);

  searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
  searchControl.addSearcher(blogSearch, options2);
  searchControl.draw(document.getElementById("searchHoctroRelatedResults"), options);
  getParams();
  searchControl.execute(hoctroRelated);
}

// Copied from http://www.gotknowhow.com/articles/how-to-get-the-base-url-with-javascript
function getBaseURL() {
  var url = location.href; // entire url including querystring - also: window.location.href;
  var baseURL = url.substring(0, url.indexOf('/', 14));
  if (baseURL.indexOf('http://localhost') != -1) {
  // Base Url for localhost
    var url = location.href; // window.location.href;
    var pathname = location.pathname; // window.location.pathname;
    var index1 = url.indexOf(pathname);
    var index2 = url.indexOf("/", index1 + 1);
    var baseLocalUrl = url.substr(0, index2);
    return baseLocalUrl + "/";
  }
  else {
  // Root Url for domain name
  return baseURL + "/";
  }
}



  // Important, this has to be called here, otherwise main var is not yet created
  google.load('search', '1.0');
  google.setOnLoadCallback(OnLoadRelated, true);

//]]>
</script>

<style>
  //#searchHoctroRelatedResults a.gs-title, a.gs-title * { color : brown; }
  //#searchHoctroRelatedResults .gs-webResult .gs-snippet { display : none; }
  //#searchHoctroRelatedResults .gs-webResult .gs-visibleUrl { display : none; }
  //#searchHoctroRelatedForm { display : none; }
</style>

<div id="searchHoctroRelatedResults">Loading...</div>
<div id="searchHoctroRelatedForm">Loading...</div>
<h6>Related Articles Widget by <u><a href='http://hoctro.blogspot.com'>Hoctro</a></u></h6>
Labels: Related Articles, Widgets

10 comments:

Leo Piccioli said...

Hi! Congrats on the approach. A separate widget, and using Google (ie PageRank) are two great ideas.

I have installed it but found that the first result is always my blog's address, either from the main page or from a post page.

I left it in the left column, at the bottom, in case you want to see it. It is in Spanish, but you'll see what I mean.

It would be great:

a) To be able to define # of results
b) To show a thumbnail image
c) Maybe to add other funcionality, like "Sites linking to this page" (anything from Google should be easier with all the code you've made)

Again, congratulations and thanks!

19/8/10 06:43
Hoctro said...

Hi Leo,

Thank you for posting back your comments. Your blog seems to have the same problem as my other two blogs: if you have sth else using the AJAX Search (in your case the video bar) then the new widget doesn't include the new labels in the search line, but show only the "site:yoursite" content. Let me look further more on why is that.

Will consider your other a,b, c, comments in the revising widget.

Maybe you can try having a test blog with some labels, etc. without the video search bar, then try my widget, then maybe you can help me solve the mystery.

Thanks again,

19/8/10 23:33
Beben said...

its look interest...when the final...hihihi :D
review...;))

20/8/10 00:05
Beben said...

I think if there are clashes code installed on my blog...@_@

20/8/10 00:58
Leo Piccioli said...

I tried it for a second in the same blog, taking out the Youtube script... but got the same results...

20/8/10 13:35
Beben said...

This hack works well, but if there are several other hacks in the form of a script as well, this hack a little clash and I think that's the source of the problem ...
When I tried on dummies blog that not many hacks (script), this hack worked very well
This cool man ....: D
Cek get out...
http://3.bp.blogspot.com/_ALuJjudg6Rg/TG9TPDwlGRI/AAAAAAAACao/_Sn7GiKqoHI/s1600/related-post.gif

20/8/10 21:31
Hoctro said...

Leo and Beben,

THank you for your great help. I think I found out what happened. The timing between the AJAX callback and the JQuery ready is not the same, so I call the function to get the tags twice to make sure we have it.

Please cut-and-paste the code and try again.

21/8/10 01:13
Beben said...

if I was a prodigy on the blog have clashed with tabview, tabview so it is not running and the hack does not work from the master Hoctro :)
cek cek to part 2 :D

23/8/10 22:46
x said...

Well, my master is back !

30/8/10 01:11
citromduro said...

hoctro thanks
i use this widget fo my blog

16/10/10 23:32

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

Blog Archive

  • ►  2011 (2)
    • ►  September (1)
      • It's been ten years already! 9.11.2001-9.11.2011
    • ►  January (1)
      • YouTube is now integrated into Blogger!!! (But I s...
  • ▼  2010 (8)
    • ►  December (1)
      • Happy New Year 2011 & Introducing my new blog
    • ▼  August (5)
      • Dissecting the New Related Articles Widget - Part ...
      • The new comment managing system from Blogger
      • Pages-Related Articles-Your Own Photo: A Blogger's...
      • Introducing The New Related Articles Widget
      • Calling for New Ideas for an Advanced Related Post...
    • ►  March (1)
      • How to quickly upload your pictures to a new or ex...
    • ►  February (1)
      • Embedding Youtube music links in your blog (withou...
  • ►  2009 (5)
    • ►  December (4)
      • Improved "Table of Contents" Widget
      • New and Improved "Related Articles" Widget using t...
      • Translation Widget: Translating your current post ...
      • Season's Greetings
    • ►  October (1)
      • Lenka
  • ►  2008 (5)
    • ►  October (2)
      • New Translation Widget
      • "Blog List" Widget
    • ►  September (3)
      • "Latest Headlines" Widget
      • (Superseded) "Related Articles" Widget
      • Gadget
  • ►  2007 (11)
    • ►  September (1)
      • Adding a front page to your blog
    • ►  May (7)
      • Andreas02 Template
      • Yet another TabView Application: Latest Posts of F...
      • TabView Application: A Blogger Hack Directory
      • Introducing TabView Widget - Part 2
      • TabView Widget
      • Overcoming the "25 Latest Posts" Limitation
      • Overcoming the "25 Recent Comments" Limitation
    • ►  February (1)
      • What is a widget, after all? (continued.)
    • ►  January (2)
      • What is a widget, after all?
      • Showing Related Articles to your Post
  • ►  2006 (16)
    • ►  December (2)
      • Showing Headlines by Labels
      • Adding Google Text Search Utility with Custom Tab ...
    • ►  November (2)
      • "Listing of Contents" Widget
      • How To Modify the Blogger Template - The Core Java...
    • ►  October (3)
      • Showing Link List or Label Widgets as Tabs
      • It's a boy!
      • Understanding the New Blogger Template (3)
    • ►  September (9)
      • Understanding the New Blogger Template (2)
      • My Blog Has Just Made It to "Blogs Of Note"!
      • Understanding the New Blogger Template (1)
      • Showing your Blogger Labels as Vertical Tabs
      • Adding a Breadcrumb Trail to your Blogger Post
      • Showing your Blogger Labels as Tabs
      • Blogger Hack: Creating Thumbnail Pictures
      • My First Blogger Template Attempt
      • Welcome to my Blog

Labels

Archived (28) Widgets (10) Miscellaneous (8) Related Articles (4) Blogger Tools (2) Gadgets (1) JavaScript (1) New Music Blog (1) Template Designer (1)
Powered by Blogger.
Loading...