EZ-Blurb

EZ-Blurb will save your time with frequently used blurbs.

当前为 2024-12-26 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         EZ-Blurb
// @namespace    http://tampermonkey.net/
// @version      V0.2
// @description  EZ-Blurb will save your time with frequently used blurbs.
// @author       Amin Ahmadizadeh
// @match        https://omnia.it.a2z.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=a2z.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    cardNumbers = 0
    let cardsObjects = []
    let blurbs = []
    'use strict';
    setTimeout(start,10000)
    class Card{
        constructor(card){
            this.card = card
            this.customerName = card.children[0].innerText.split(' ')[0]
            this.card.loaded = true
            this.blurbDiv = document.createElement("div")
            this.blurbDiv.style.marginTop = '-10px'
            this.card.appendChild(this.blurbDiv)
            this.addButtons()
        }

        addButtons(){
            if (blurbs.length > 0)
                blurbs.forEach((blurb)=>{
                    this.addButton(blurb.symb, blurb.tooltip, blurb.text, blurb.backColor, blurb.textColor)
            })
        }
        sendMessage(text){
            let chatText = document.querySelector("textarea[data-node-id='ccp-chat-composer-editor']")
            if (chatText)
                chatText.value = text
            alert("No chat text found:\n" + text)
        }
        addButton(symb,tooltip,text,backColor,textColor){
            var div = document.createElement("SPAN")
            
            div.innerText = symb
            div.style.display = 'inline-block'
            div.style.padding = '0px 5px'
            div.style.borderRadius = "10px"
            div.style.margin = "1px"
            div.style.backgroundColor = backColor
            div.style.color = textColor
            div.title = tooltip
            this.blurbDiv.appendChild(div)
            div.addEventListener('click',()=>{this.sendMessage(this.compileText(text))})
            div.addEventListener('mouseover',()=>{
                div.style.backgroundColor = 'red'
            })
            div.addEventListener('mouseout',()=>{
                div.style.backgroundColor = backColor
                div.style.color = textColor
                div.style.padding = '0px 5px'
                div.innerHTML = symb
            })
        }
        
        compileText(text){
            return text.replace("$customer$", this.customerName)
        }
    }

    class Blurb{
        constructor(symb, tooltip, text, backColor, textColor){
            this.symb = symb
            this.tooltip = tooltip
            this.text = text
            this.backColor = backColor
            this.textColor = textColor
        }
    }

    function readBlurbs(){
        console.log("Reading blurbs...")
        savedBlurbs =  localStorage.getItem('EZBlurbs')
        const inputBtn = document.createElement("input")
        inputBtn.type= "file"
        inputBtn.multiple = false
        inputBtn.style.display = 'None'
        inputBtn.addEventListener('change',(event)=>{
            const file = event.target.files[0]
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    blurbsJSON = JSON.parse(e.target.result)
                    makeBlurbs(blurbsJSON.Blurbs, true)
                };
                reader.readAsText(file);
            }
        })
        const btn = document.createElement("input")
        btn.type = 'button'
        btn.className = 'awsui-button awsui-button-variant-normal awsui-hover-child-icons'
        btn.value= "Load EZ-Blurbs"
        btn.addEventListener('click',()=>{
            inputBtn.click()
        })
        document.getElementsByClassName("omnia-ui-tabtools")[0].appendChild(btn)
        if ( savedBlurbs ) {
            console.log ('Blurbs found:', localStorage.getItem("EZBlurbs"))
            makeBlurbs(JSON.parse(localStorage.getItem("EZBlurbs")), false)        
        }
    }

    function makeBlurbs(blurbsJSON, save) {
        for (i=0; i< blurbsJSON.length; i++){
            let blurbJSON = blurbsJSON[i]
            symb = blurbJSON.symbol
            tooltip = blurbJSON.tooltip
            text = blurbJSON.text
            backColor = blurbJSON.backColor
            textColor = blurbJSON.textColor
            blurbs.push(new Blurb(symb,tooltip,text,backColor, textColor))
            if (save)
                localStorage.setItem("EZBlurbs", JSON.stringify(blurbsJSON))
            }
    }

    function extractCards (mainContainer){
        cardsDivs = mainContainer.children[1].firstChild.firstChild.children
        cardsDivs = [...cardsDivs]
        return cardsDivs
    }

    function start(){
        console.log("EZBlurbs V0.1 started.")
        readBlurbs()
        var observer = new MutationObserver(function (mutations){
            const mainContainer = document.querySelector('.contact-queue-card-list-all-contacts')
            if (mainContainer) {
                console.log("Observig Contacts...")
                observer.observe(mainContainer,{
                    childList: true,
                    subtree: true,
                    })
                let cards = extractCards(mainContainer)
                cards.forEach(card => { 
                    if (!card.loaded){
                        if (card.children.length != 0)
                            cardsObjects.push(new Card(card))
                    }
                })
            }
        })
        const cqcl=document.querySelector('#contact-queue-card-list')
        observer.observe(cqcl,{
                            childList: true,
                            subtree: true,
                        })
    }
})();