3

我想创建一个谷歌浏览器扩展,除其他外,它将根据输入的内容修改聊天文本。我将在视频旁边添加一个按钮,呼叫,并在名称下方添加人员按钮,单击时将激活修改。我不想在页面上放置更多的脚本,所以我希望能够以 Gmail 在聊天框中按“返回”的方式发送消息。此外,我希望能够通过在聊天框中显示文本来显示聊天的两个人都在使用我的扩展程序,就像“此聊天已关闭记录”文本一样,并且如果双方都在使用它,可能会添加额外的东西聊天。我试图做的是模仿文本区域,当用户“发送”它时,抓住它并修改它,然后将其插入真实的并发送新文本。我可以更改文本,但似乎无法发送...

这是我到目前为止所拥有的,我将所有内容都包含在 setInterval 中以检查聊天框是否存在并向其中添加适当的内容:

var chatBtnClone = setInterval(function() {
    if ($("body").find(".nH .NG").length > 0) {  //if chat is active

        var clone = $("body").find(".nH .NG .NJ").first();
        if (clone.children()[0].className.indexOf("chat") < 0) { //if already added my class
            var clonned = clone.clone();
            var clonnedChd = clonned.children().first();
            clonnedChd.attr("title", "Start encrypted chat");
            clonnedChd.on('click', function() {
                console.log("clicked chatBtn!"); //make sure it works
                var self = $(this);
                if (self[0].className.indexOf("chatEncX") >= 0) { //toggle button pic 
                    self.removeClass('chatEncX').addClass('chatEnc');
                    self.attr("title", "Stop encrypted chat");
                } else {
                    self.removeClass('chatEnc').addClass('chatEncX');
                    self.attr("title", "Start encrypted chat");
                }
            });
            clone.parent().prepend(clonned);
            clonned.find('.NK').removeClass("NK-Y8").addClass("chatEncX");
        }

        var chatBoxs = $('body').find(".nn .AD");
        var chatArea = chatBoxs.first().find(".nH textarea"); //get chat textareas
        if (chatArea.length === 1) {
            var clonChatArea = chatArea.first().clone();
            clonChatArea.removeAttr("id");
            chatArea.first().parent().append(clonChatArea);
            // chatArea.first().hide();
            var chatTextDiv = chatBoxs.first().find(".jp .nH .nH").first();
            clonChatArea.focusin(function() {
                chatTextDiv.removeClass("gv").addClass("f7");
            });
            clonChatArea.focusout(function() {
                chatTextDiv.removeClass("f7").addClass("gv");
            });
            clonChatArea.on('keyup', function(event) {
                var self = this;
                //console.log(this.style.height); //make sure height it working

                if (self.scrollHeight === 38) {
                    self.style.overflowY = "hidden";
                    self.style.height = "36px";
                } else if (self.scrollHeight === 47) {
                    self.style.height = "54px";
                } else if (self.scrollHeight === 62) {
                    self.style.height = "72px";
                } else if (self.scrollHeight >= 77) {
                    self.style.height = "80px";
                    self.style.overflowY = "scroll";
                }

                if( event.keyCode === 13 && event.shiftKey ){
                    //regular, just insert a newline
                } else if (event.keyCode === 13) {
                    //grab text and modify then reinsert into real textarea
                    var chatTxt = $(this).val();
                    var chatHidden = chatBoxs.first().find(".nH textarea").first();
                    var chatEncTxt = Crypto.AES.encrypt(chatTxt, "pass"); //modify text
                    //console.log(chatEncTxt);
                    chatHidden.val(chatEncTxt);
                    chatHidden.focus();
                    chatHidden.trigger({ type : 'keypress', which : 13 }); //try to imitate the return key and send (NOT WORKING!!!)
                    // $(this).focus();
                }
            });
        }
    }
},150);
4

1 回答 1

1

这可能有点晚了,但如果其他人有兴趣,那么我通过执行以下操作来管理它:

var e = new Event("keypress");
e.keyCode = e.which = 13;
// :mc is an example textarea id, but the OP has the code for finding that already
document.getElementById(':mc').dispatchEvent(e);
于 2014-01-13T09:53:36.100 回答