PHP & JavaScript Room :: 設置サンプル

実行結果

設置サンプル

指定したTwitterユーザーの最新のツイート3件を通知ウィンドウに表示します。
 

設置サンプルのソース

<!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" xml:lang="ja" lang="ja">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta http-equiv="Content-Language" content="ja" />
      <meta http-equiv="Content-Script-Type" content="text/javascript" />
      <meta http-equiv="Content-Style-Type" content="text/css" />
      <meta http-equiv="imagetoolbar" content="no" />
       <title>設置サンプル</title>
      <link rel="stylesheet" type="text/css" href="/content/lib/global.css" />
      <!-- JS -->
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
      <script type="text/javascript" src="/content/lib/jquery/jquery.jsonp.js"></script>
      <script type="text/javascript">
         $(function(){
            /* デスクトップ通知がサポートされていないの場合 */
            if(!window.webkitNotifications){
               $("#res").html("このブラウザでは、デスクトップ通知がサポートされていません>< Google Chromeでお試しください。");
            }
            /* 【通知】ボタンをクリックした時 */
              $("#show_html_notification").click(function() {
                 /* 入力されたTwitterユーザー名を取得 */
                 var username=$("#username").val();
                 /* 未入力の場合はアラート表示 */
                 if(!username){
                    alert("Twitterユーザー名を指定してください");
                    return;
                 }
                 /* 通知を表示する権限がある場合 */
               if (window.webkitNotifications.checkPermission() == 0) {
                  $.getJSONP(
                     /* 指定したユーザーのTwitterらイムラインから最新3件取得 */
                     "http://api.twitter.com/1/statuses/user_timeline.json?screen_name="+username+"&count=3&callback={callback}",
                     function(json){
                        for(var i in json){
                           /* 通知を表示 */
                           window.webkitNotifications.createNotification(
                              /* icon: プロフィールアイコン */
                              json[i].user.profile_image_url,
                              /* title: スクリーンネーム */
                              json[i].user.screen_name,
                              /* body: ツイート内容 */
                              json[i].text
                           ).show();
                        }
                     }
                  );
               /* 通知を表示する権限がない場合 */
                } else {
                   /* 通知確認バー表示 */
                  window.webkitNotifications.requestPermission();
                  }
               });
         });
      </script>
   <link rel="stylesheet" type="text/css" href="/common/css/example.css"></head>
   <body id='example3' class='example'><div class="ads" style="margin:32px auto;text-align:center;"></div><h1 class='h'><a href='/'>PHP &amp; JavaScript Room</a> :: 設置サンプル</h1>
<h3 class='h'>実行結果</h3>
      <div id="wrap">
         <h1>設置サンプル</h1>
<!-- CODE -->
         <p id="res" style="color:red;"></p>
         <p>
            指定したTwitterユーザーの最新のツイート3件を通知ウィンドウに表示します。<br>
            <label for="username"><input type="text" id="username" value="cocoism" /></label> 
            <button id="show_html_notification">HTML通知を表示</button>
         </p>
<!-- / CODE -->
      </div>
   </body>
</html>