OpenUserJS3165 / simple automatic response message

// ==UserScript==
// @namespace     https://openuserjs.org/users/OpenUserJS3165
// @name          simple automatic response message
// @description   简单脚本
// @copyright     2020, OpenUserJS3165 (https://openuserjs.org/users/OpenUserJS3165)
// @license       0BSD
// @version       0.0.7
// @include       https://sellercenter.lazada.com.my/im/window
// @include       https://sellercenter.lazada.sg/im/window
// @include       https://sellercenter.lazada.co.th/im/window
// @include       https://sellercenter.lazada.co.id/im/window
// @include       https://sellercenter.lazada.com.ph/im/window
// @include       https://sellercenter.lazada.vn/im/window
// @grant none
// @require      https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.slim.min.js
// ==/UserScript==

// ==OpenUserJS==
// @author OpenUserJS3165
// ==/OpenUserJS==

//respToContent 回复的文本信息
var respToContent = 'Dear customer, we are on the weekend and have no access to your message. But rest assured, we will reply to you as soon as possible on Monday, you can:\n 1.Make an order directly, early order early delivery.\n 2.For logistics issues, please kindly contact with lazada customer care. ';
//waitMessageLoadTime 打开未读消息后等待对话框加载的时间 需小于respToNextTime
var waitMessageLoadTime = 800;
//respToNextTime 每条消息回复的间隔时间 需大于waitMessageLoadTime
var respToNextTime = 1300;
//indexNum 当前经过了多少个页面间隔时间 禁止修改
var indexNum = 0;
//openORCloseTime 打开新页面或者关闭之前打开页面的间隔时间 
var openORCloseTime = 15000;

(function() {
    'use strict';
    console.log('1:进入lazada客服自动回复js脚本');
    window.setInterval(function() {
        console.log('components-badge:');
        var unreadTab = document.querySelector(".search-bar").lastChild.lastChild;
        unreadTab.click();
        var badge = document.querySelector(".badge");
        if (null != badge) {
            console.log('3:进入未读回话框,回话数为'+badge.innerText);
            badge.parentNode.parentNode.click();
            console.log(badge.innerText);
            window.setTimeout(function() {
                var messagesDiv = document.querySelector(".message-content").firstChild.firstChild;
                if (0 < messagesDiv.lastChild.className.indexOf("user-type-1")) {
                    var inputTexarea = document.querySelector(".message-fields__autosize");
                    inputTexarea.value = respToContent;
                    inputTexarea.select();
                    var event = new Event('input', { bubbles: true });
                    inputTexarea.dispatchEvent(event);
                    var msgBtn = document.querySelector(".btn-msg-submit");
                    msgBtn.click();
                }
                var readTab = document.querySelector(".search-bar").lastChild.firstChild;
                readTab.click();
            }, waitMessageLoadTime);
        }
    }, respToNextTime);

    window.setInterval(function() {
        indexNum++;
        console.log('open or close check');
        var maxNum = 8;//打开或者关闭页面最久间隔
        var urlIndex = document.URL;
        var mark = urlIndex.indexOf("/im/window");
        var urlSite = urlIndex.substring(mark-2, mark).toUpperCase();
        console.log(" urlSite : "+urlSite + " indexNum :"+indexNum );
        if (indexNum >= maxNum-1 && 'MY' != urlSite) {
            window.close();
        }
        var siteSpans = document.querySelectorAll(".im-components-badge");
        [].forEach.call(siteSpans, function(siteSpan) {
            console.log(siteSpan.title+" content:"+siteSpan.textContent);
            var span_li = siteSpan.parentNode;
            var site = siteSpan.textContent.substring(0,2);
            if ('0'!= siteSpan.title) {
                if ('MY' == urlSite && 'MY' != site && indexNum%maxNum == 1) {
                    console.log(" urlSite : "+urlSite + " site :"+site +" siteSpan.title : " + siteSpan.title);
                    span_li.click();
                    var iframe = document.querySelector("iframe.active");
                    var url = iframe.src.split("?")[0];
                    openNewWindow(url);
                }
            } else {
                if (urlSite == site && 'MY' != urlSite) {
                    window.close();
                }

            }
        });
    }, openORCloseTime);
    // Your code here...
})();

function openNewWindow(url) {
    let a = $("<a href='"+url+"' target='_blank'>baidu</a>").get(0);
    let e = document.createEvent('MouseEvents');
    e.initEvent( 'click', true, true );
    a.dispatchEvent(e);
}