WebScrap

October 13, 2009

Googleの検索結果にサムネイル画像を表示するGreasemonkey

Google Thumbnailsのmod版を更にいじったもの。 FirefoxアドオンのGooglePreviewと違い、こちらはAutoPagerizeに対応しているので、追加されたページにもサムネイル画像を表示してくれる。


これはサムネイル画像の表示サイズを直し、YouTubeやニコニコ動画にはサムネイル画像を表示しないように修正したもの。

// ==UserScript==
// @name          Google Thumbnails
// @version       0.3.5 mod2 by A. (for autopagerize)
// @namespace     http://flet.ch/things/greasemonkey/
// @description   Adds web site thumbnail images to google search results
// @include       http://www.google.*/search*
// ==/UserScript==
(function () {

  var gthumbnails = {

    config: {
      use_amazon_thumbnails: true, // use amazon product images in place of screen shots for amazon.com results?
      debug: true,                // print debug output to the javascript console
    },
    result_link_xpath:
        [
          ".//A[@class='l']", /*new google*/
          ".//*[(name() = 'DIV' and @class='g') or (name() = 'H3' and @class='sem')]/H2/A",/*google*/
          ".//li/div[@class='title']/a[1]" /*goo*/
        ],

    amazon_regex: /^http:\/\/(?:www\.)?amazon\.(?:com|(?:co.)?[a-z]{2})\/exec\/obidos\/(?:tg\/detail\/-|ASIN)\/(?:[a-z]+\/)?([a-zA-Z0-9]+)\/?.*$/i,
    result_link_regex: /^(?:([^\/].*)|\/url\?.*q=([^&]+)&?.*$)/i,

    addCSS: function( css ) {
      var head = window.document.getElementsByTagName( "head" )[0];
      var style = window.document.createElement( "style" );
      style.setAttribute( "type", "text/css" );
      style.innerHTML = css;
      head.appendChild( style );
    },

    addThumbnails2: function(context) {
      for(var i=0; i< context.length;i++)
        gthumbnails.addThumbnails(context[i])
    },

    addThumbnails: function(context) {
      //Autopagerize
      if(!context){
        root = document;
      }else{
        root = context;
      }
      for (var i=0; i < gthumbnails.result_link_xpath.length; i++){
        var results = document.evaluate( gthumbnails.result_link_xpath[i], root, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null );
        if (results.snapshotLength > 0) break;
      }
      if (results.snapshotLength == 0) return;

      gthumbnails.config.debug && ( t0 = new Date().getTime() );
      for( var result_link = null, i = 0; ( result_link = results.snapshotItem( i ) ); i++ ) {
        // create the thumbnail <img /> and surrounding <a> elements
        var thumbnail = document.createElement( &quot;span&quot; );

        var result_url = result_link.getAttribute( &quot;href&quot; );
        var real_result_url = result_url.replace( gthumbnails.result_link_regex, &quot;$1$2&quot; );
        var asin = result_url.match( gthumbnails.amazon_regex ) &amp;&amp; result_url.replace( gthumbnails.amazon_regex, &quot;$1&quot; );

        if( gthumbnails.config.use_amazon_thumbnails &amp;&amp; asin ) {
          var is_amazon_link = true;
          var thumb_url = &quot;http://images.amazon.com/images/P/&quot; + asin + &quot;.01._SS78_BO1,255,255,255_BO1,170,170,170_BO61,255,255,255_CR46,61,112,82_.jpg&quot;;
        } else {
          var is_amazon_link = false;
          var thumb_url = &quot;http://open.thumbshots.org/image.pxf?url=&quot; + real_result_url;
        }

        thumbnail.innerHTML =
          '<a href="' + result_url + '">' +
            '<img class="gms_gthumbnail&#39; + ( is_amazon_link ? " align="left" src="' + thumb_url + '" width="120" height="90" ?? + ) : _amazon? /> 1){
      setTimeout(arguments.callee, 1000, filter, i - 1);
    }
  }
  addFilter(gthumbnails.addThumbnails2);

})();

0 Coment:

Post a Comment