// ==UserScript==
// @name            Google My Cache
// @namespace       http://espion.just-size.jp/archives/05/136155838.html
// @description     All displayed pages are sent to Google Bookmarks.
// @include         http://*
// @exclude         http://mixi.jp*
// ==/UserScript==
//
// $Id: GoogleMyCache.user.js 882 2006-07-27 06:32:10Z takayama $

(function() {
   // cut frame page
   if(self.location.href != top.location.href) return;

   var replace_query = 1;
   var min_size      = 1000;

   function checkURL() {
      var href = location.href;
      if(href.match(/^https:\/\//)) return;
      if(!href.match(/^http:/)) return;

      href = href.replace(/^http:\/\//, '');
      if(href.match(/^google./)) return;
      if(href.match(/^\w+.google./)) return;
      if(href.match(/^www.google./)) return;
      if(href.match(/^\d+\.\d+\.\d+\.\d+\/search\?/)) return;
      if(href.match(/^[^\/]+\/search\?/)) return;
      if(href.match(/^localhost/)) return;
      if(href.match(/^127.0.0.1/)) return;
      if(href.match(/^192.168./)) return;
      if(href.match(/^172.16./)) return;

      return 1;
   }

   function checkCookie() {
      var _main = function() {
         var path = location.href;
         path = path.replace('http://'+document.domain, '');
         path = path.replace(/#.*$/, '');
         if(replace_query) path = path.replace(/\?.*$/, '');
         path = escape(path);

         var cook = new Cookie('__gmGoogleMyCache_'+path);
         if(cook.get() > 0) return 0;
         cook.set(1);

         return 1;
      }

      var Cookie = function(n) {
         this.name   = n;
         //this.expire = new Date;
         //this.expire.setTime (this.expire.getTime() + (30*60*60*1000)); // 30 hours
      }

      Cookie.prototype = {
         set: function(v) {
            if(v) document.cookie = this.name+"="+v;
            //if(v) document.cookie = this.name+"="+v+";expires ="+this.expire.toGMTString();
         },

         get: function() {
            var c = document.cookie;
            var r = new RegExp(this.name+'=(\\d+)', "");
            c.match(r);

            return (parseInt(RegExp.$1));
         }
      }

      return _main();
   }

   function checkOther() {
      if(document.body.innerHTML.length < min_size) return;

      return 1;
   }

   function main() {
      var href = location.href;
      href = href.replace(/#.*$/, '');
      if(replace_query) href = href.replace(/\?.*$/, '');

      var form  = document.createElement('form');
      form.appendChild(createHidden('title',  document.title));
      form.appendChild(createHidden('bkmk',   href));
      form.appendChild(createHidden('labels', makeLabel()));
      form.setAttribute('Accept-charset', "UTF-8");
      form.action = 'http://www.google.com/bookmarks/mark';
      form.method = 'post';
      form.target = '__gmGoogleForm';
      document.body.appendChild(form);

      var ifm  = document.createElement('iframe');
      ifm.name = '__gmGoogleForm';
      ifm.style.display = 'none';
      document.body.appendChild(ifm);

      form.submit();

      ifm.addEventListener('load',
         function() {
            setTimeout(function() {
               document.body.removeChild(form);
               document.body.removeChild(ifm);
            }, 1);
         }, false);

      function createHidden(n, v) {
         var input   = document.createElement('input');
         input.type  = 'hidden';
         input.name  = n;
         input.value = v;
         return input;
      }

      function makeLabel(){
         var date  = new Date()
         var month = date.getMonth();
         var day   = date.getDate();
         if(month < 10) month = '0'+month;
         if(day   < 10) day   = '0'+day;
         return ('AutoPost,' + 'Y'+(date.getYear()+1900) + ', M' + month + ', D' + day);
      }
   }

   if(checkURL() && checkOther() && checkCookie()) main();
   //if(checkURL()) main();

})();


