Chrome プラグインを作成する代わりに、ウィンドウ タイトルを点滅させるか、HTML5 通知を使用します。新しいメッセージについて IMAP Gmail をポーリングする簡単なページを作成し、大きな iFrame に gmail を含めます。新しいメッセージが見つかった場合、外部ウィンドウは通知を発行できます。
HTML5 通知: http://www.html5rocks.com/en/tutorials/notifications/quick/
点滅するタイトル(から採用):
var newMailBlinker = (function () {
var oldTitle = document.title,
msg = 'New Mail!',
timeoutId,
blink = function() {
document.title = document.title == msg ? ' ' : msg;
},
clear = function() {
clearInterval(timeoutId);
document.title = oldTitle;
window.onmousemove = null;
timeoutId = null;
};
return function () {
if (!timeoutId) {
timeoutId = setInterval(blink, 1000);
window.onmousemove = clear;
}
};
}());
PHP Poll Gmail IMAP (から採用):
$t1=time();//mark time in
$tt=$t1+(60*1);//total time = t1 + n seconds
do{
if(isset($t2)) unset($t2);//clean it at every loop cicle
$t2=time();//mark time
if(imap_num_msg($imap)!=0){//if there is any message (in the inbox)
$mc=imap_check($imap);//messages check
//var_dump($mc); die;//vardump it to see all the data it is possible to get with imap_check() and them customize it for yourself
echo 'New messages available';
}else echo 'No new messagens';
sleep(rand(7,13));//Give Google server a breack
if(!@imap_ping($imap)){//if the connection is not up
//start the imap connection the normal way like you did at first
}
}while($tt>$t2);//if the total time was not achivied yet, get back to the beginning of the loop
IMAP スクリプトへの jQuery AJAX ポーリング(このから採用):
// make the AJAX request
function ajax_request() {
$.ajax({
url: '/path/to/gmail/imap/checkMessages.php',
dataType: 'json',
error: function(xhr_data) {
// terminate the script
},
success: function(xhr_data) {
console.log(xhr_data);
if (xhr_data.status == 'No new messages') {
setTimeout(function() { ajax_request(); }, 15000); // wait 15 seconds than call ajax request again
} else {
newMailBlinker(); // blink the title here for new messages
}
}
contentType: 'application/json'
});
}
明らかに、ポーリングに jQuery と PHP を使用することはありません。いずれかを選択してポーリングを行います。クライアントにポーリングを実行させ、接続ごとに PHP で IMAP をチェックさせることをお勧めします。そうは言っても、これらのスニペットから始める必要があります:)