bookmarklet

  • /*
     * @title bookmarklet
     * @description my bookmarklet
     * @include http://b.hatena.ne.jp/search/*
     * @license MIT License
     * @require 
     * @javascript_url
     */
    (function(){
      var is_hatebSearch = document.body.id === 'hatena-bookmark-search' ? 1 : 0;
      if (!is_hatebSearch) return;
      var res = {}, temp_date = '';
      var default_start = '20150101';
      var default_end = '20150131';
      var start = prompt('開始', default_start);
      validation(start);
      if (res.bool) {
        start = res.value;
        var end = prompt('終了', default_end);
        validation(end);
        if (res.bool) {
          end = res.value;
          if (res.exchange) {
            do_search(end, start);
          } else {
            do_search(start, end);
          }
        } else {
          return error(end);
        }
      } else {
        return error(start);
      }
      function do_search(s, e) {
        var from = document.getElementById('narrow-down-start');
        var to = document.getElementById('narrow-down-end');
        from.value = s;
        to.value = e;
        if (window.confirm('start = ' + s + ' end = ' + e + 'agree ?')) {
          var post = document.querySelector('dd > input.inputbutton');
          if (post) {
            post.click();
          }
        }
      }
      function validation(val) {
        if (val == null || val == '') {
          res['bool'] = false;
          return;
        } else {
          var x = val.match(/^(\d{4})(\d{2})(\d{2})$/) || false;
          //    console.log(x);
          if (!x || x.length !== 4) {
            return res['bool'] = false;
          } else if (x[1] < 2005 || x[1].toString() == '2005' && x[2].toString().match(/02|01/) && x[3] < 10) {
            return res['bool'] = false;
          }
          // Check for the date exists
    
          var sample = (x[1].toString() + '/' + x[2].toString() + '/' + x[3].toString());
          var the_date = new Date(sample);
          if (temp_date !== '') {
            if (temp_date > the_date) {
              res['exchange'] = true;
            }
          }
          var check = the_date.getFullYear() + '/' + ('00' + (the_date.getMonth() + 1).toString()).slice(-2) + '/' + ('00' + (the_date.getDate().toString())).slice(-2);
          // console.log(check + ' : ' + sample);
          if (sample !== check) {
            res['bool'] = false;
            return;
          } else {
            temp_date = the_date;
            res['value'] = x[1] + '-' + x[2] + '-' + x[3];
            res['bool'] = true;
            return;
          }
        }
      }
      function error(e) {
        if (e == null) {
          return undefined;
        } else if (e == '') {
          return alert('エラー 値が入力されていません');
        } else {
          return alert('エラー ' + e + 'が正しくなかったようです');
        }
      }
      return;
    })();
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2015/09/24 14:04:49 - 2015-09-24
  2. 2015/09/24 14:00:10 - 2015-09-24