[英](Chrome Extension) Execute something from extension before page addEventListener
I want to add " HELLO"
to a message box before the message is sent (when press enter ).我想在发送消息之前将
" HELLO"
添加到消息框中(按enter 时)。
Example:例子:
Here I try to change the whole message to (current message + " HELLO") to the message box when the user press ENTER (return key).在这里,当用户按下ENTER (回车键)时,我尝试将整个消息更改为(当前消息 +“HELLO”)到消息框。
Code (example for slack message box):代码(松弛消息框的示例):
document.querySelector('.ql-editor').addEventListener("keydown", async function(event) {
if (event.keyCode === 13) {
//Fetch current element with data
var slackMessageField = document.querySelector('.ql-editor');
event.preventDefault();
textRightNow = slackMessageField.innerText;
slackMessageField.innerText = (slackMessageField.innerText + " HELLO");
}
});
slackMessageField = `<div class="ql-editor ql-blank" data-gramm="false"
contenteditable="true" id="undefined" dir="auto" role="textbox" tabindex="0"
data-team-id="T9L8N47PZ" aria-label="Message #ipnmod"
aria-describedby="context-bar-text" aria-multiline="true"
aria-autocomplete="list" aria-expanded="false"
aria-owns="chat_input_tab_ui" spellcheck="true"><p><br></p></div>`
which is where the user types the message in.这是用户输入消息的地方。
Problem:问题:
Slack sends the regular message before adding " HELLO" and also don't manage to fetch the current innerText (textRightNow). Slack 在添加“HELLO”之前发送常规消息,并且也无法获取当前的innerText(textRightNow)。 So we end up with "(EMPTY SPACE) HELLO" in the message box instead of adding " HELLO".
所以我们最终在消息框中使用“(EMPTY SPACE) HELLO”而不是添加“HELLO”。
The behavior may be happening just because the element slackMessageField
doesn't have any text inside of it and thus slackMessageField.innerText
isn't getting any text.这种行为可能只是因为元素
slackMessageField
里面没有任何文本,因此slackMessageField.innerText
没有得到任何文本。 If you happen to run the same event over and over, you'll have the <space>Hello
concatinated every time.如果您碰巧一遍又一遍地运行相同的事件,则每次都会连接
<space>Hello
。 Please make sure that you've added the text that you're searching is available inside the targeted element.请确保您添加的文本在目标元素内可用。 For instance:
例如:
document.querySelector('.ql-editor').addEventListener("keydown", async function(event) { if (event.keyCode === 13) { //Fetch current element with data var slackMessageField = document.querySelector('.ql-editor'); event.preventDefault(); textRightNow = slackMessageField.innerText; slackMessageField.innerText = (slackMessageField.innerText + " HELLO"); } });
.ql-editor { border: 1px solid #000; padding: 5px; }
<label>Press Enter:</label> <div class="ql-editor ql-blank" data-gramm="false" contenteditable="true" id="undefined" dir="auto" role="textbox" tabindex="0" data-team-id="T9L8N47PZ" aria-label="Message #ipnmod" aria-describedby="context-bar-text" aria-multiline="true" aria-autocomplete="list" aria-expanded="false" aria-owns="chat_input_tab_ui" spellcheck="true"> Lorem Ipsum </div>
On the above example, when the keydown event triggers, there will be some existing text inside the slackMessageField
element, and thus the result will show as Lorem Ipsum HELLO
after the event has triggered.在上面的例子中,当 keydown 事件触发时,
slackMessageField
元素中会有一些现有的文本,因此在事件触发后结果将显示为Lorem Ipsum HELLO
。
There Is no way to do this.没有办法做到这一点。 Site runs a script before which clears the messagebox.
站点在清除消息框之前运行一个脚本。 I'll have to save the content of the messagebox everytime it changes.
每次更改时,我都必须保存消息框的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.