exmail.qq.com_contacts_plus

通讯录增强,在邮件列表展示头像(需要先进通讯录收集头像)

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            exmail.qq.com_contacts_plus
// @description     通讯录增强,在邮件列表展示头像(需要先进通讯录收集头像)
// @version         1.1
// @namespace       https://wanyaxing.com/blog/20190123201508.html
// @author          [email protected]
// @include         https://exmail.qq.com/*
// @grant           GM_getValue
// @grant           GM_setValue
// ==/UserScript==


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

function bindImageEvent(imgNode)
{
    imgNode.onmouseover = function(event){
        hintImageShow(this,event);
    }
    imgNode.onmouseout = function(event){
        hintImageHide(this,event);
    }
    imgNode.onclick = function(event){
                    event.preventDefault();
                    event.stopPropagation();
                    return false;
                }
}

function hintImageShow(imgNode,event)
{
    console.log(imgNode,event);
    var hintNode = document.getElementById('hint_with_mouse');
    if (!hintNode)
    {
        hintNode = document.createElement('div');
        hintNode.setAttribute('id','hint_with_mouse');
        hintNode.style.cssText = 'position:absolute;z-index:999999;';
        document.body.append(hintNode);
    }
    hintNode.innerHTML='<img src="'+imgNode.getAttribute('src')+'"/>';
    hintNode.style.left = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft + 6 +'px';
    hintNode.style.top = event.clientY + document.body.scrollTop + document.documentElement.scrollTop + 9 +'px';
    hintNode.style.display="block";
}

function hintImageHide(imgNode,event)
{
    var hintNode = document.getElementById('hint_with_mouse');
    if (hintNode)
    {
        hintNode.style.display="none";
    }
}

if (document.querySelector('.contacts #list')) {
    var needCheckLis;
    function checkPhotos()
    {
        needCheckLis = [];
        document.querySelectorAll('#list li[data-id]').forEach(function(liNode){
            var email = liNode.querySelector('.email').innerText;
            var photo = GM_getValue(email + '_photo');
            if (!photo)
            {
                needCheckLis.push(liNode);
            } else {
                var imgNode = document.createElement('img');
                imgNode.src=photo;
                imgNode.style.cssText = '    height: 100%;position: absolute;left: 50px;';
                var nameNode = liNode.querySelector('.name');
                nameNode.style.cssText = 'position:relative;';
                nameNode.appendChild(imgNode);
            }
        });
    }

    function getBase64Image(img) {
        var canvas = document.createElement("canvas");
        canvas.width = img.width;
        canvas.height = img.height;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, img.width, img.height);
        var dataURL = canvas.toDataURL("image/png");
        return dataURL; // return dataURL.replace("data:image/png;base64,", "");
    }

    function checkEmailIds()
    {
        if (needCheckLis.length>0) {
            var liNode = needCheckLis.shift();
            var email = liNode.querySelector('.email').innerText;
            liNode.click();
            setCheckout(function(){
                return document.querySelector('.detail_mail') && document.querySelector('.detail_mail').innerText == email;
            },function(){
                var imgNode = document.querySelector('.avatar_img');
                if (imgNode && imgNode.getAttribute('src').indexOf('default')<0) {
                    var dataURL = getBase64Image(imgNode);
                    GM_setValue(email + '_photo',dataURL);
                }
                var nameNode = document.querySelector('.info_name');
                if (nameNode) {
                    GM_setValue(email + '_name',nameNode.innerText);
                }
                // console.log(imgNode.getAttribute('src'),GM_getValue(email + '_photo'));
                checkEmailIds();
            },1000);
        }
    }

    var aNode = document.createElement('a');
    aNode.innerHTML = '遍历刷新头像';
    aNode.className="button_gray ft_topbar_btn";
    aNode.onclick = function(){
        checkPhotos();
        checkEmailIds();
    }
    document.querySelector('#bar .tool').appendChild(aNode);

    checkPhotos();
}


// 邮件列表简情处理
if (document.getElementById('frm')) {

    function  checkEmailAvatar() {
        document.getElementById('frm').querySelectorAll('.toarea>table').forEach(function(child){
            var formNode       = child.querySelector('.tl');

            var email = formNode.getAttribute('title');
            var photo = GM_getValue(email + '_photo');
            if (photo) {
                var imgNode = document.createElement('img');
                imgNode.src=photo;
                imgNode.style.cssText = 'height: 18px;right: 60px;position:absolute;z-index:99999;';
                formNode.style.cssText = 'position:relative;';
                bindImageEvent(imgNode);
                formNode.appendChild(imgNode);
            }
        });
    }

    setTimeout(function(){
         checkEmailAvatar();
    },1);
}


//邮件详情显示头像
if (document.getElementById("mainmail"))
{
    document.querySelectorAll('span[e]').forEach(function(itemNode){
        var email = itemNode.getAttribute('e');
        var photo = GM_getValue(email + '_photo');
        if (photo) {
            var imgNode = document.createElement('img');
            imgNode.src=photo;
            imgNode.style.cssText = 'height: 15px;position: relative;top: 2px;';
            bindImageEvent(imgNode);
            itemNode.insertBefore(imgNode,itemNode.firstChild);
        }
    });
    document.querySelectorAll('#contentDiv a').forEach(function(itemNode){
        var href = itemNode.getAttribute('href');
        if (href && href.indexOf('mailto:')==0){
            var email = href.replace('mailto:','');
            var name = GM_getValue(email + '_name');
            if (name) {
                itemNode.innerHTML = '<span>'+name + '&lt;' + email + '&gt;'+'</span>';
            }
            var photo = GM_getValue(email + '_photo');
            if (photo) {
                var imgNode = document.createElement('img');
                imgNode.src=photo;
                imgNode.style.cssText = 'height: 15px;position: relative;top: 2px;';
                bindImageEvent(imgNode);
                if (itemNode.firstChild){
                    itemNode.insertBefore(imgNode,itemNode.firstChild);
                } else {
                    itemNode.append(imgNode);
                }
            }
        }
    });

}