preciousmouse / 微信内容分析

// ==UserScript==
// @name         微信内容分析
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  try to take over the world!
// @author       You
// @match        https://mp.weixin.qq.com/s*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=qq.com
// @grant        none
// @license MIT
// ==/UserScript==

function delay(time){
    return new Promise(res=>setTimeout(res,time));
}
function textNodesUnder(el){
    var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
    while(n=walk.nextNode()) a.push(n);
    return a;
}

(async function() {
    'use strict';
    await delay(1000);
    // Your code here...
    const console = window.console; // console已被劫持,需要重新赋值
    console.clear();
    console.group('微信内容分析');

    const allTextNodes = textNodesUnder(document);
//    console.log('allTextNodes',allTextNodes);

    const usefulTexts = allTextNodes
    .filter(item=> { // 判定是否显示
        const rect = item.parentElement.getBoundingClientRect();
        return rect.width>0 && rect.height>0;
    })
    .map(item=>item.textContent)
    .map(item=>item.split('\n'))
    .flat()
    .map(item=>item.trim()) // 去掉空格
    .filter(item=> item && item.length >0)
    //.filter(item=> item && item.length >2 && item.length < 1000)
    //.filter(item=> (new RegExp(".*[\u4E00-\u9FA5]+.*")).test(item)) // 筛选出中文

    const result = [...new Set(usefulTexts)]; // 去重

    console.log('解析到'+result.length+'条文案');
    console.log(result.join('\n'));

    console.groupEnd();
})();