twwp

No Pants, No Gag, No Success.
about | archive | tako3
 

February 10, 2012

location.href = 'javascript:(' + function(){ //hogehoge }.toString() + ')()';

location.href = 'javascript:(' + function(){
  //hogehoge
}.toString() + ')()';

February 08, 2012

何を*見?て-も何-か?思い.出す

http://ermos-tily.dotcloud.com/

$('.thing').each(function() {
    format($(this), $(this).text())
});
function keypress() {
  var prev = '';
  return function() {
    var ab = $(this).attr('id');
    if(prev != $(this).val()) {
      var input = $(this).val();
      var thing = $('.thing-wrapper#' + ab + ' .thing');
      format(thing, input);
    }
    prev = $(this).val();
  };
};

function format(container, text) {
  if(text.match(/^https?:/)) {
    container.oembed(text, {
      embedMethod: 'fill',
      maxWidth: 460,
      maxHeight: 200,
      defaultOEmbedProvider: "none",
      afterEmbed: function(res, oembedData) {
        if(res.type == 'link' || res.type == 'error') {
	  adjustFontSize(container.get(0));
	}
      }
    });
  } else {
    container.text(text);
    formatText(container);
  }
};

// based on http://la.ma.la/misc/js/takahashi_oop.html
function formatText(element) {
  var child = element.get(0);
  var parent = element.parent().get(0);
  with(child.style) {
    fontSize = "10px";
    display = "inline";
  }
  var parent_w = parent.offsetWidth;
  var parent_h = parent.offsetHeight;
  var child_w = child.offsetWidth;
  var new_fs = Math.ceil((parent_w/child_w) * 9);
  if(new_fs > 10000) { return }
  with(child.style) {
    fontSize = new_fs + "px";
  }
  var child_h = child.offsetHeight;
  if(child_h > parent_h) {
    var new_fs = Math.ceil((parent_h/child_h) * new_fs * 0.85);
    with(child.style) {
      fontSize = new_fs + "px";
    }
  }
};

繋いだ2ページ目から画像や文字が展開されなかった。

(function () {
  document.addEventListener('DOMNodeInserted', function (evt) {
    var elm = evt.target;
    if (elm.className == 'item') {
      $('.thing', elm).each(function () {
        format($(this), $(this).text())
      });
    }
  });
})()

↓ 失敗作。

Bookmarklet起動する前に読み込んだヤツも展開しようと思ったら、すでに展開済みのヤツが真っ白になってしまった。ちょっとめんどうだからやめた。

(function () {
  var hoge = function (e) {
      $('.thing', e).each(function () {
        format($(this), $(this).text());
      });
    }
  hoge(document);
  document.addEventListener('DOMNodeInserted', function (evt) {
    var elm = evt.target;
    if (elm.className == 'item') {
      hoge(elm);
    }
  });
})()

jQuery 全然わかんないんだけどたぶん DOMNodeInserted 的なの何かあるに違いない

あと、コード片拾ってきて貼りあわせてなんかやるのやっぱり楽しいな

でも this がどこを指しているのかは全然わかっていない。結果オーライ

February 07, 2012

Dashboardが250件以上読めるようになりました

1. offset=200くらいまで普通に読む

2. 読めたPostの最初と最後のpost_idの差をPostの数で割る → これでだいたいのpost_idの差が出る

3. 最後のPostのpost_idから (post_idの差) * 20 を引いた値をsince_idとする

4. since_idをセットしてDashboard APIで20件読む

 4.1. (読んだPostのpost_idの最大値) < (今表示してるPostのpost_idの最少値) の場合 → since_id = (読んだPostのpost_idの最大値) → 4へ

 4.2. (読んだPostのpost_idの最小値) >= (今表示してるPostのpost_idの最少値) の場合 → since_id -= (post_idの差) * 20 → 4へ

 4.3. それ以外 → 終了