2010-01-01から1年間の記事一覧

関数を遅延して実行する

Function.prototype.delay = function(n) { var self = this; var args = Array.prototype.slice.call(arguments, 1); var id = setTimeout(function() { self.apply(self, args); }, n * 1000); return function() { clearTimeout(id); }; } 1秒後に実行 va…

Ruby1.9を使うにあたり1.8と違う部分

エンコーディングの指定1.9だと内部のエンコーディングが固定されてないので、文字列のエンコーディングを指定します。 #!/usr/bin/env ruby # encoding: utf-8 p "あいうえabcd" 文字列のサイズRuby1.8 p "あいうえabcd".size #=> 16 Ruby1.9 p "あいうえab…

Rubyで複数のバージョンを共存して使う方法

コンパイルする時prefixを別な所に指定してインストールする方法は、 あまり綺麗なやり方じゃないので、Ruby Version Managerを使いましょう。 rvmを使うとコマンドでバージョン指定するだけで切り替えることができます。 環境はDebian sidです。rvmのインス…

オブジェクト指向プログラミングをしやすくする

普通の書き方。 var Apple = function() { this.initialize(); }; Apple.prototype.initialize = function() { alert("init"); }; new Apple(); applyを使ってみる。var Appleに入るのはprototype.jsのCrass.create()で渡されるクロージャと同じ。 var Apple…

デバッグ

alert使えばいいんだけど、いちいちダイアログが出てくるのがうっとうしいのと、あとループの中に入れると厄介なので。htmlの方に以下を追加して。 <div id="debug"></div> この関数を使う。alertOnceはループとかに入れたりして使う。 var debug = document.getElementById("debug…

文字列リテラル展開

String#replaceの第二引数に関数を入れると加工して返せる string.replace(reg, function replacer(str, p1, p2, p3....., offset, s) { }); str: regにマッチした部分文字列 p1, p2, p3........: strがRexExpオブジェクトの場合の()のマッチ結果 offset: マ…

ビットの数え上げ

ハッカーのたのしみに載ってた。x &= (x - 1)が凄いね。 これは、x-1でxが0以外だとxを下位ビットからみていって一番最初に1になってるビットが0になる。 それを元の値に対してかければその場所の位置だけを0にできる。 全部が0になるまで繰り返せば終了、こ…

変更があったらブラウザを自動リロードするブックマークレット(2)

フックするファイルの指定をなくして、ロケーションのhtml本体と、そのhtmlにリンクしてるjsとcssがローカルにある場合フックするように修正した。 javascript: (function () { if (location.href.indexOf("file:///")) { alert("not local file"); return; …

カラーピッカー

カラービッカーを作ってみた。とりあえず試作。 デモ var ColorPicker = {}; ColorPicker.HSVtoRGB = function(h, s, v) { var r, g, b; if (s == 0) { var i = Math.round(v * 255); return [i, i, i]; } var hi = Math.floor(h / 60); var f = h / 60 - hi…

変更があったらブラウザを自動リロードするブックマークレット

複数のファイルを指定してどれかが変更されてればリロードできるようにした。 キャンセルを押すまでファイル指定して使う。 javascript: (function () { if (!location.href.indexOf("http://")) { alert("not local file"); return; } var func = function …

文字列を切り分けて検索をする

function find(reg, data) { var tmp = reg.exec(data); if (tmp == null) return null; var start = data.lastIndexOf("\n", tmp.index) + 1; var end = data.indexOf("\n", start); var r = data.slice(start, end); reg.lastIndex = end + 1; return r; }…

補完付きのテキストボックス

デモ var Suggest = function(input, suggest, dic) { this.idInput = document.getElementById(input); this.idSuggest = document.getElementById(suggest); this.dic = dic this.idx = -1; this.serchWord = ""; this.init(); }; Suggest.Key = { UP: 38,…

livedoor Readerのメッセージを移動するGreasemonkeyスクリプト

常に上部分非表示にして使ってるので移動した。 インストール // ==UserScript== // @name move message // @namespace http://d.hatena.ne.jp/f96q/ // @include http://reader.livedoor.com/reader/ // ==/UserScript== var m = document.getElementById("…

jQueryを使えるようにするブックマークレット

javascript: (function(src) { var s = document.createElement("script"); s.type = "text/javascript"; s.src = src; document.getElementsByTagName("head")[0].appendChild(s); setTimeout(function() {document.title += "(jQuery" + jQuery.fn.jquery …

livedoor ReaderからはてなブックマークにポストするGreasemonkeyスクリプト

ユーザー名とパスワード設定して使う。はてなブックマークにbでコメントなしで登録、Shift+bでコメントを書いて登録する。 インストール // ==UserScript== // @name postbhatena // @namespace http://d.hatena.ne.jp/f96q/ // @include http://reader.live…

livedoor Readerを広くして使うGreasemonkeyスクリプト

インストール // ==UserScript== // @name ldr resize // @namespace http://d.hatena.ne.jp/f96q/ // @include http://reader.livedoor.com/reader/ // ==/UserScript== var style = document.createElement("style") style.type = "text/css"; style.inner…

CSSを編集するGreasemonkeyスクリプト

ほかのサイトのCSSをブラウザから変更。デモ インストール // ==UserScript== // @name CSSEdit // @namespace http://d.hatena.ne.jp/f96q/ // @include http://* // ==/UserScript== function cssEdit() { function gid(id) { return document.getElementB…

HTMLの構造ダンプ

HTMLの構造をダンプして表示。 function dumpNode(node, data, deep) { for (var i = 0; i < node.childNodes.length; i++) { var e = node.childNodes[i]; if (e.nodeType != 1) continue; var r = { deep: deep, tag: e.tagName.toLowerCase(), id: ((e.id…

自分の環境で使えるヒープの大きさを取得する

エキスパートCプログラミングに書いてあるのとほぼ同じだけど、1MB単位で測る。 あと、メモリが少ない環境の場合はKB単位で測ったほうがいい。 #include <stdio.h> #include <stdlib.h> #define SIZE_KB (1<<10) #define SIZE_MB (1<<20) int main(int argc, char **argv) { int</stdlib.h></stdio.h>…

Googleのトップページをずっと成人式仕様にするGreasemonkeyスクリプト(2)

XPath使って書き直し。 // ==UserScript== // @name seizin // @namespace http://d.hatena.ne.jp/f96q/ // @include http://www.google.co.jp/ // ==/UserScript== (function () { var r = document.evaluate("//img[@alt='Google']", document, null, XPat…

迷路探索

人材獲得作戦・4 試験問題ほかの迷路探索を解いてみた。BFS知らなかったので調べてから。 #include <stdio.h> #include <vector> #include <queue> struct Pos { int x, y; }; struct MValue { int d; bool visit; }; struct Map { int width, height; std::vector<std::vector<struct MValue> > data; struct P</std::vector<struct></queue></vector></stdio.h>…

YouTubeを常に高画質モードで再生するGreasemonkeyスクリプト

インストール // ==UserScript== // @name youtube fmt18 // @namespace http://d.hatena.ne.jp/f96q/ // @include http://www.youtube.com/* // ==/UserScript== window.addEventListener("load", function () { (location.href.match(/http:\/\/www.youtub…

メモ

#include <iostream> #include <typeinfo> int main(int argc, char **argv) { int a = 30; int &i = a; std::cout << typeid (i).name() << std::endl; }</typeinfo></iostream>

Googleのトップページの上のリンクにlivedoor Readerを追加するGreasemonkeyスクリプト

posの数値を変えて位置を変えられます。インストール // ==UserScript== // @name add ldr // @namespace http://d.hatena.ne.jp/f96q/ // @include http://www.google.co.jp/ // ==/UserScript== (function () { var pos = 5; var ldr = document.createEle…

Googleのトップページをずっと成人式仕様にするGreasemonkeyスクリプト

インストール // ==UserScript== // @name seizin // @namespace http://d.hatena.ne.jp/f96q/ // @include http://www.google.co.jp/ // ==/UserScript== (function () { var img = document.getElementsByTagName("img"); for (var i = 0; i < img.length;…

ふぁぼったーのを見えるようにするGreasemonkeyスクリプト

既に既出だったりしますが。あとAutoPagerizeに対応してません。インストール // ==UserScript== // @name change censored // @namespace http://d.hatena.ne.jp/f96q/ // @include http://favotter.matope.com/* // ==/UserScript== (function () { var sp…

Twitterでフォローしてる人を取得

apiで取得しようとすると100人ぐらいで取得できなくなるので、作った。 ユーザー名とパスワード入れて使う。 スクレイピングもっと効率的なやり方あるだろうし。 ページ変わると動かなくなるのであまり推奨できない。 あとrubyのマーシャルデータで保存。 #!…

TwitterIRCGatewayの設定

2010-02-12追記TIGのコマンドコンソールに入る /join Console@tig 設定のとこに入る config 設定されてるもの一覧 show 設定する set EnableTypeMap true ルートに戻る exit TypableMapconfigの所で set EnableTypableMap true TypableMapの文字列の色を赤に…

リリース

某爆弾を爆発されるゲームぽいものを作りました。ここにからどうぞ。