exmail.qq.com_notification_plus

让腾讯企业邮箱支持浏览器通知

当前为 2019-01-21 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name            exmail.qq.com_notification_plus
// @description     让腾讯企业邮箱支持浏览器通知
// @version         1.0
// @namespace       https://wanyaxing.com/blog/20181203203742.html
// @author          [email protected]
// @include         https://exmail.qq.com/*
// ==/UserScript==


function setCheckout(check,fuc,time){
    if (check())
    {
        fuc();
    }
    else
    {
        setTimeout(function(){
            setCheckout(check,fuc,time);
        },time);
    }
}

// 新邮件通知
if (document.getElementById("useraddr") && document.getElementById("useraddrcontainer") )
{
    function receiveTheNewest()
    {
        console.log("尝试收取最新邮件!!!",document.getElementById('folder_1'),document.getElementById('readmailbtn_link'));
        if (document.getElementById('folder_1'))
        {
            document.getElementById('folder_1').click();
        }

        if (document.getElementById('readmailbtn_link'))
        {
            document.getElementById('readmailbtn_link').click();
            eval(document.getElementById('readmailbtn_link').getAttribute('onclick'));
        }
    }
    function notifyMail(title,body)
    {
        // 收到来信就刷新收件箱。
        receiveTheNewest();
        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(){
                    notify.close();
                    window.focus();
                    //如果通知消息被点击,通知窗口将被激活。
                };
                notify.onerror = function () {
                    console.log("桌面消息出错!!!");
                };
                notify.onshow = function () {
                    console.log("桌面消息成功");
                };
                notify.onclose = function () {
                    console.log("桌面消息关闭!!!");
                };
            }
        });
    }

    setCheckout(function(){
            return (unsafeWindow.QMWebpushTip && unsafeWindow.QMWebpushTip._addData)
        },function(){
            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);
            }
            console.log('unsafeWindow.QMWebpushTip hooked');
            var checkStatusNode = document.createElement('span');
            checkStatusNode.style.cssText = 'margin-left: 10px;';
            checkStatusNode.innerHTML = '☑︎收到新邮件时使用桌面通知。';
            checkStatusNode.onclick=function(){
                receiveTheNewest();
            }
            document.getElementById("useraddrcontainer").parentNode.appendChild(checkStatusNode);
    },1000);

    //每小时刷新一次
    setTimeout(function(){
        window.history.go(0);
    },1000 * 60 * 60);

}