preface
Tencent home QQ mailbox and enterprise mailbox is very easy to use, before has been using the web version, at most plus a mobile APP to receive a push, anyway is not in a hurry.
nasty
Recently, due to work reasons, I have higher requirements on the real-time receiving of emails.
At the beginning, I tried several mail clients, and found that the so-called real time is just to adjust the frequency of pulling mail through POP3. However, the remote pull efficiency is really not very good, and it also needs to do local storage, wasting a lot of disk space, it is not very convenient.
I just wanted to be informed
In fact, my demand is very simple, I just want to receive mail in the mailbox, on the computer desktop to give myself a notice, and QQ mailbox web version has in fact through a long link to achieve real-time monitoring of mail to the account function.
Web desktop notification
So if I could just find the code for the long connection here and call a desktop notification when I get a new email, wouldn’t that be perfect? !
So, I found this piece of code
(function() {
var e = a
, f = getTop();
f.loadJsFile(f.getPath("js") + f.getFullResSuffix("qmwebpush.js"), true, f.document, function() {
QMWebpush.getInst().addEvent((1 << e._nTOTALSERVICES - 1) * 2 - 1.function(g, h) {
e._addData(g, h)._reloadWin(g)._updateTip()._setDocTitle();
}).open(e._mnOpenServices);
e._mbInit = true;
});
}
)();
Copy the code
To put it simply, this code is used to receive a new email, in the bottom right corner of the web page pop-up box to remind the function, so I add a section of desktop notification here is not perfect? !
Cool, that’s how you do it.
Let’s do a monkey script
In the monkey script, you can query the page window object with the unsafeWindow call. In the above code, pick one of the code to hook:
var oldUpdateTip = unsafeWindow.QMWebpushTip._addData;
unsafeWindow.QMWebpushTip._addData = function(g,h){
console.log('_addData',g,h);
setTimeout(function(){
if (h && h[0] && h[0] ['subject'] && h[0] ['summary'] )
{
notifyMail(h[0] ['subject'] , h[0] ['summary']); }},2000);
return oldUpdateTip(g,h);
}
Copy the code
In this case, the h variable is an array containing the data of the new email, so fetch the title and description of the email and pop the desktop notification:
function notifyMail(title,body)
{
var tag = "sds"+Math.random();
Notification.requestPermission(function (perm) {
if (perm == "granted") { var notify = new Notification(title.replace(/ /g,' '), {
tag: tag,
// icon: "https://exmail.qq.com/favicon.ico", body: body.replace(/ /g,' ')}); notify.onclick=function(){// If the notification message is clicked, the notification window will be activated and click the inbox. console.log("Desktop message hit!!");
if (document.getElementById('folder_1'))
{
document.getElementById('folder_1').click();
}
else if (parent.document.getElementById('folder_1'))
{
parent.document.getElementById('folder_1').click();
}
window.focus();
notify.close();
};
notify.onerror = function () {
console.log("Desktop message error!!");
};
notify.onshow = function () {
console.log("Desktop message successful");
// setTimeout(function(){
// notify.close();
// },2000)
};
notify.onclose = function () {
console.log("Desktop message closed!!"); }; }})}Copy the code
Notification is used here. There are a lot of examples on the Internet.
After the language
So, making your own living, programmer, isn’t it fun?
Note: This article only shares some thoughts on working life, and does not provide a complete script download, please understand.