2010年1月30日

リモートコンピュータから別のコンピュータにリモート接続する

カテゴリー: Linux — admin @ 3:28 PM

シェルにログイン後、以下のようなコマンドを実行する。

ssh username@192.168.1.100

リモートコンピュータから別のコンピュータにリモート接続できる。

2010年1月29日

IEはradioボタンをappendChildしてもつかえない

カテゴリー: JavaScript, jQuery — admin @ 2:03 AM

IE6~7でラジオボタンをappendChildした場合、そのラジオボタンはクリックできない。ラジオボタンとして致命的なバグである。

■失敗例

以下のようにDOM要素を操作する。

var input = document.createElement('input');
var p     = document.createElement('p');
p.appendChild(input);

ちなみにjQueryを使うと以下のようになる。

var input = document.createElement('input');
var p     = document.createElement('p');
$(p).append(input);

■解決策

IEだけinnerHTMLを使う。

var input = document.createElement('input');
var p     = document.createElement('p');
if(!!(!window.opera && window.attachEvent)){
    p.innerHTML = '<input type="radio" name="id" />';
}
else{
    p.appendChild(input);
}

ちなみにjQueryを使うと以下のようになる。

var input = document.createElement('input');
var p     = document.createElement('p');
if($.browser.msie){
    $(p).append('<input type="radio" name="id" />');
}
else{
    $(p).append(input);
}

2010年1月29日

mb_ereg系関数が使えない

カテゴリー: Linux, PHP — admin @ 1:46 AM

EC-CUBEが使えなかったりする。

yum install php-mbstring

コンパイルは面倒だからyumする。

2010年1月27日

Shadowboxの設定

カテゴリー: JavaScript — admin @ 1:24 AM

以下のようにHTML側で設定する。

<a href="#" id="shadowbox" rel="shadowbox;width=450;height=110;">open window</a>

script側でも以下のように設定した場合、上手く動作しないことがある。

Shadowbox.open({
    "content" : 'this is a shadow box',
    "player"  : "html",
    "width"   : 300,
    "height"  : 200,
    "options" : {
        "onFinish" : function(){
            alert('test');
        }
    }
});

onFinishが上手く動作しなかった。設定ミスに起因するが、見つけにくいミスでもある。

2010年1月23日

Firefox3.6で旧バージョン用のプラグインを動かす

カテゴリー: ブラウザ — admin @ 6:12 PM

URL欄に以下のように打ち込む。

about:config

右クリックで項目を追加する。「真偽値」を選択だ。項目名は以下の通りだ。

extensions.checkCompatibility.3.6

値は「偽」(=false)だ。

false

3.6にアップデートしたらlive http headersが動かなくなってしまったので対処。

2010年1月23日

JavaScriptで画像を縦横比を維持しつつ指定サイズに丸める

カテゴリー: JavaScript, jQuery — admin @ 6:02 PM

SVGやcanvasを使えば確かそのままトリミングもできた気がするが、別にそこまでしたくない用の関数。画像のオリジナルサイズを取得し計算する感じだ。面倒なのでjQueryを使う。

var frameWidth  = 700;
var frameHeight = 400;
$('li').css({
    "overflow" : "hidden"
});
$('li img').each(function(){
    var nWidth  = this.naturalWidth  || getNaturalSize(this).width;//ff : ie
    var nHeight = this.naturalHeight || getNaturalSize(this).height;//ff : ie
    if(nHeight < nWidth * (frameHeight / frameWidth)){
        this.width  = nWidth * frameHeight / nHeight;
        this.height = frameHeight;
        $(this).css({
            "width"  : nWidth * frameHeight / nHeight + 'px',
            "height" : frameHeight + 'px',
            "position" : "relative",
            "left" : -((nWidth * frameHeight / nHeight) - frameWidth) / 2 + 'px'
        });
    }
    else{
        this.width  = frameWidth;
        this.height = nHeight * frameWidth / nWidth;
        $(this).css({
            "width"  : frameWidth + 'px',
            "height" : nHeight * frameWidth / nWidth + 'px',
            "position" : "relative",
            "top" : -((nHeight * frameWidth / nWidth) - frameHeight) / 2 + 'px'
        });
    }
});

以下の部分でブラウザ分岐をしている。IEとOpera以外はimgオブジェクトにオリジナルのサイズが格納されたプロパティ(naturalWidth、naturalHeight)を持つ。

var nWidth  = this.naturalWidth  || getNaturalSize(this).width;//ff : ie
var nHeight = this.naturalHeight || getNaturalSize(this).height;//ff : ie

getNaturalSize関数は以下のようになる。

var getNaturalSize = function(image){
    var w, h, key = "actual", run, mem;
    if(window.opera){
    }
    if (image[key] && image[key].src === image.src) {
        return image[key];
    }
    run = image.runtimeStyle;
    mem = {
        "w" : run.width,
        "h" : run.height
    }; // keep runtimeStyle
    run.width  = "auto"; // override
    run.height = "auto";
    w = image.width;
    h = image.height;
    run.width  = mem.w; // restore
    run.height = mem.h;
    image[key] = {
        "width"  : w,
        "height" : h,
        "src"    : image.src
    };
    return image[key]; // bond
};

ちなみにOperaには対応していない。

2010年1月20日

Cookieを使って別サイトの閲覧履歴を取る

カテゴリー: PHP, ウェブサイト制作 — admin @ 1:42 AM

自社サイトを訪れたユーザが他サイトAを訪れたか調べたい。他サイトAに対してビーコンを貼れることを条件に、これが可能となる。

■他サイト側(http://sample.jp/index.html)

imgタグでビーコンを貼り付けてあげれば良い。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ドキュメント</title>
</head>
<body>
<p><img src="http://sample.org/spacer.php" width="1" height="1" /></p>
</body>
</html>

■自社(サイト)ドメイン側実装

他サイトに貼り付けたビーコンを準備してあげる。

ビーコン用のイメージ(http://sample.org/spacer.php)

クッキーをセットしたいのでphpで書く。

<?php
header('Content-Type: image/gif');
setcookie('sample', 'see');
print(file_get_contents('spacer.gif'));

検証ページ(http://sample.org/index.php)

このページを訪問したユーザが、他サイトAを訪問したか調べられる。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>検証ページ</title>
</head>
<body>
<?php
if($_COOKIE['sample'] === 'see'){
    print('<p>you have already seen http://sample.jp</p>');
}
else{
    print('<p>Nice to meet you!</p>');
}
?>
</body>
</html>

基本的にはユーザの行動収集はこの原理で行う。iframeとJavaScriptを使用することで、さらに多くの情報を取得することができる。

2010年1月17日

Shadowboxでショートカットキーを使えなくする

カテゴリー: JavaScript — admin @ 9:53 PM

Shadowboxでは「x」や「w」のキーを押すとウィンドが閉じる。しかし、入力欄などがShadowbox内に存在していた場合、この挙動が不具合を生じさせる。そこで以下のようにenableKeysオプションをfalseにする。

Shadowbox.init({
    "language" : 'ja',
    "players"  : ['img', 'html', 'iframe', 'qt', 'wmp', 'swf', 'flv'],
    "enableKeys" : false
});

2010年1月17日

tinyMCEはjQueryのcloneで複製できない

カテゴリー: JavaScript, jQuery — admin @ 3:06 AM

jQueryのtinyMCEプラグインを使用する。

■cloneメソッド

そっくりそのままコピーができるが、コピーされたtinyMCEは機能しないはずだ。この不具合は、tinyMCEをsortable要素にした時にも生じる。恐らく、tinyMCEが内部的にiframeを使用していることに起因するのではないだろうか。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無題ドキュメント</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.js"></script>
<script type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="tiny_mce/jquery.tinymce.js"></script>
<script type="text/javascript">
$(function(){
	$('textarea.tinymce').tinymce({
		theme : "advanced",
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_toolbar_location   : "top",
		theme_advanced_toolbar_align      : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		init_instance_callback : function(){
			var clone = $('div.parts').clone(true);
			$('div.parts').after(clone);
		}
	});
});
</script>
</head>
<body>
<div class="parts">
<form method="post" action="">
<textarea class="tinymce" name="" rows="15" cols="60"></textarea>
</form>
</div>
</body>
</html>

■解決策

document.createElementでtextareaから生成したtextareaに対して、tinymce()する。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無題ドキュメント</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.js"></script>
<script type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="tiny_mce/jquery.tinymce.js"></script>
<script type="text/javascript">
$(function(){
	$('textarea.tinymce').tinymce({
		theme : "advanced",
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_toolbar_location   : "top",
		theme_advanced_toolbar_align      : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		init_instance_callback : function(){
			var div      = document.createElement('div');
			var form     = document.createElement('form');
			var textarea = document.createElement('textarea');
			$(div).attr('class', 'parts');
			$(div).append(form);
			$(form).append(textarea);
			$(textarea).attr('class', 'tinymce');
			$(textarea).attr('rows', '15');
			$(textarea).attr('cols', '60');
			$('div.parts:last').append(div);
			$(textarea).tinymce({
				theme : "advanced",
				plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
				theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
				theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
				theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
				theme_advanced_toolbar_location   : "top",
				theme_advanced_toolbar_align      : "left",
				theme_advanced_statusbar_location : "bottom",
				theme_advanced_resizing : true
			});
		}
	});
});
</script>
</head>
<body>
<div class="parts">
<form method="post" action="">
<textarea class="tinymce" name="" rows="15" cols="60"></textarea>
</form>
</div>
</body>
</html>

しっかり機能するtinyMCEが生成されるはずだ。

init_instance_callback

エディタが完成するとコールされる。tinymce()から完成までは時間がかかり、気をつけなくてはいけないのは、その間にscript処理が止まらない。従って、完成した(直後の)エディタに対してscriptからアクセスする場合は、init_instance_callbackを使用する必要がある。

2010年1月13日

Yahooひらがな変換API

カテゴリー: PHP — admin @ 12:05 AM

ドキュメントの文字コードはutf-8である。

<?php
$str = urlencode('庭には二羽鶏がいる。');
$url = "http://jlp.yahooapis.jp/FuriganaService/V1/furigana?appid=<apikey>&sentence={$str}";
$xml = new SimpleXmlElement($url, null, true);
var_dump($xml);
/*
object(SimpleXMLElement)#1 (1) {
  ["Result"]=>
  object(SimpleXMLElement)#2 (1) {
    ["WordList"]=>
    object(SimpleXMLElement)#3 (1) {
      ["Word"]=>
      array(8) {
        [0]=>
        object(SimpleXMLElement)#4 (3) {
          ["Surface"]=>
          string(3) "庭"
          ["Furigana"]=>
          string(6) "にわ"
          ["Roman"]=>
          string(4) "niwa"
        }
        [1]=>
        object(SimpleXMLElement)#5 (3) {
          ["Surface"]=>
          string(3) "に"
          ["Furigana"]=>
          string(3) "に"
          ["Roman"]=>
          string(2) "ni"
        }
        [2]=>
        object(SimpleXMLElement)#6 (3) {
          ["Surface"]=>
          string(3) "は"
          ["Furigana"]=>
          string(3) "は"
          ["Roman"]=>
          string(2) "ha"
        }
        [3]=>
        object(SimpleXMLElement)#7 (3) {
          ["Surface"]=>
          string(6) "二羽"
          ["Furigana"]=>
          string(6) "にわ"
          ["Roman"]=>
          string(4) "niwa"
        }
        [4]=>
        object(SimpleXMLElement)#8 (3) {
          ["Surface"]=>
          string(3) "鶏"
          ["Furigana"]=>
          string(12) "にわとり"
          ["Roman"]=>
          string(8) "niwatori"
        }
        [5]=>
        object(SimpleXMLElement)#9 (3) {
          ["Surface"]=>
          string(3) "が"
          ["Furigana"]=>
          string(3) "が"
          ["Roman"]=>
          string(2) "ga"
        }
        [6]=>
        object(SimpleXMLElement)#10 (3) {
          ["Surface"]=>
          string(6) "いる"
          ["Furigana"]=>
          string(6) "いる"
          ["Roman"]=>
          string(3) "iru"
        }
        [7]=>
        object(SimpleXMLElement)#11 (1) {
          ["Surface"]=>
          string(3) "。"
        }
      }
    }
  }
}
*/
foreach($xml->Result->WordList->Word as $word){
    print($word->Furigana);
}
//にわにはにわにわとりがいる

文字コードがshift-jisの場合は以下のようになる。

$str = urlencode(mb_convert_encoding('庭には二羽鶏がいる。', 'utf-8', 'sjis'));
$url = "http://jlp.yahooapis.jp/FuriganaService/V1/furigana?appid=<apikey>&sentence={$str}";
$xml = new SimpleXmlElement($url, null, true);
foreach($xml->Result->WordList->Word as $word){
    print($word->Furigana);
}

API

次ページへ »