By
Ziru
—
Uploaded
Aug 26, 2009
/*
@author: Ziru Zhu
@url: http://twitter.com/ziru
@update: http://userscripts.org/jetpacks/168.js
@title: 开心网消息提醒
@description: 检查开心网是否有未读消息
@version: 0.1
Based on: http://jetpacks.benji-banned.ch/ by Benjamin Auer
*/
jetpack.future.import("storage.simple");
var gblLogPrefix = '[Kaixin] ';
function log(msg) {
console.log(gblLogPrefix + msg);
};
function updateStatusBar(doc, msg) {
$(doc).find("#count").text(msg);
log('Update status bar: ' + msg);
};
function KaixinNotifier() {
jetpack.storage.simple.kaixin_last_count = jetpack.storage.simple.kaixin_last_count || 0;
this.lastCount = jetpack.storage.simple.kaixin_last_count;
this.latestCount = this.lastCount;
log('Initialize kaixin_lastCount = ' + this.lastCount);
};
KaixinNotifier.prototype = {
open: function() {
jetpack.tabs.open("http://www.kaixin001.com/msg/");
jetpack.tabs[jetpack.tabs.length-1].focus();
},
notify: function(msg) {
jetpack.notifications.show({
title: "开心网",
body: msg,
icon: "http://www.kaixin001.com/favicon.ico"
});
},
update: function() {
log("Start loading the kaixin001 message page");
var inst = this;
$.ajax({
type: "GET",
url: inst.url,
dataType: "html",
async: true,
success: function(html) {
log("Message page load onSuccess()");
if ($(html).find('#body_msg_num')) {
log("Message page loaded");
var count = 0;
count += parseInt($(html).find('#body_msg_num').text().replace(/条新$/,''));
count += parseInt($(html).find('#body_bbs_num').text().replace(/条新$/,''));
count += parseInt($(html).find('#body_comment_num').text().replace(/条新$/,''));
count += parseInt($(html).find('#body_sysmsg_num').text().replace(/条新$/,''));
count += parseInt($(html).find('#body_bbsreply_num').text().replace(/条新$/,''));
count += parseInt($(html).find('#body_reply_num').text().replace(/条新$/,''));
if (count > inst.lastCount) {
inst.notify('New Messages! ('+(count - inst.lastCount)+')');
}
inst.latestCount = count;
log('kaixin_lastCount = ' + inst.lastCount);
log("kaixin_latestCount = " + inst.latestCount);
inst.updateMsg();
} else {
console.error("Not logged in");
inst.updateMsg('Login');
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
inst.updateMsg();
console.error("Failed to grab web page: " + inst.url + ", status = " + textStatus);
}
});
},
onclick: function() {
this.lastCount = this.latestCount;
jetpack.storage.simple.kaixin_last_count = this.lastCount;
log('Store kaixin_last_count = ' + this.lastCount);
this.updateMsg();
jetpack.tabs.open(this.url);
jetpack.tabs[ jetpack.tabs.length - 1 ].focus();
},
updateMsg: function(msg) {
if (msg)
this.msg = 'K(' + msg + ')';
else
this.msg = 'K(' + this.latestCount + ')';
log('Update msg to: ' + this.msg);
},
lastCount: 0,
latestCount: 0,
msg: '...',
url: "http://www.kaixin001.com/msg/"
};
function NotifierManager() {};
NotifierManager.prototype = {
add: function(notifier) {
this.notifierSet.push(notifier);
},
ping: function() {
for each (var notifier in this.notifierSet)
notifier.update();
},
updateStatusMsg: function(doc) {
var finalMsg = '';
for each (var notifier in this.notifierSet)
finalMsg += notifier.msg + ' ';
finalMsg = finalMsg.replace(/^\s*/, '').replace(/\s*$/, '').replace(/\s+/g, ' ');
updateStatusBar(doc, finalMsg);
},
update: function(doc) {
log('# of notifiers: ' + this.notifierSet.length);
this.ping();
this.updateStatusMsg(doc);
var inst = this;
setTimeout(function() { inst.updateStatusMsg(doc) }, 5000);
},
onclick: function(doc) {
for each (var notifier in this.notifierSet)
notifier.onclick();
this.update(doc);
},
notifierSet: [],
};
function onInit(doc) {
// ====== Start of jQuery Hack =====
// Reference: http://groups.google.com/group/mozilla-labs-jetpack/msg/3b22891f2425865f
// =================================
//Use jQuery extend to override init in jQuery.fn
jQuery.fn.extend(jQuery.fn, {
_init : jQuery.fn.init, //the original init function
init : function (selector, context) {
//If context is not specified, let's use jetpack's current document
return this._init(selector, context ||
jetpack.tabs.focused.contentDocument);
}
});
//Add jQuery's prototype to the new init function (so we can instantiate object via init() call)
$.prototype.init.prototype = $.prototype;
// ====== End of jQuery Hack =====
var mgr = new NotifierManager();
mgr.add(new KaixinNotifier());
mgr.update(doc);
setInterval( function() { mgr.update(doc); }, 10 * 60 * 1000 ); // 10 minute
// hook clicks
$(doc).click(function() { mgr.onclick(doc); });
};
jetpack.statusBar.append({
html: <>
<div><span id="count"></span></div>
</>,
onReady: function (doc) {
onInit(doc);
},
});
// onInit();