Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit last message with Up Arrow Key #6

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ You can [download](https://chromewebstore.google.com/u/1/detail/whatsapp-web-too
## Features:
- Rotate images by 90deg
- Avoid Enter key sending the message (Feature toggleable)
- Press Up Arrow Key to edit last message sent.



Expand Down
1 change: 1 addition & 0 deletions createMouseEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const createMouseEvent = () => { };
3 changes: 2 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"js": [
"observer-setup.js",
"add-rotate-image-button.js",
"avoid-enter-send.js"
"avoid-enter-send.js",
"up-arrow-edit.js"
],
"matches": [
"https://web.whatsapp.com/"
Expand Down
1 change: 1 addition & 0 deletions observer-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var observer = new WebKitMutationObserver(function (mutations) {
console.log(className);
if (className == CHAT_CONTAINER_CLASS) {
onInputShow();
handleArrowEdit();
} else {
onImageOpen(mutation.addedNodes[0]);
}
Expand Down
59 changes: 59 additions & 0 deletions up-arrow-edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const MESSAGE_CONTAINER_CLASS = "x3psx0u xwib8y2 xkhd6sd xrmvbpv";
const HOVER_TARGET = "_amk6 _amlo";
const ARROW_BUTTON_CLASS = "_ahkm";

let listenerCreated = false;

const handleArrowEdit = () => {
if(listenerCreated) return;
listenerCreated = true;
document.addEventListener("keydown", (e) => {
if (e.key !== "ArrowUp") return;
const message_input_container = document.querySelector("span.selectable-text[data-lexical-text='true']");
if (message_input_container) return;
editLastMessage();
});
}


const editLastMessage = async () => {
const mouseOverEvent = createMouseEvent('mouseover');
let lastMessage = getLastMessageHoverTarget();
lastMessage.dispatchEvent(mouseOverEvent);

await sleep(10);

lastMessage = getLastMessageHoverTarget();
const arrowButton = lastMessage.getElementsByClassName(ARROW_BUTTON_CLASS)[0];
arrowButton.click();
lastMessage.dispatchEvent(mouseOverEvent);

await sleep(10);

const editButton = document.querySelector("div[role='button'][aria-label='Edit']");
if (editButton) editButton.click();
else hideMenu();
}

const getLastMessageHoverTarget = () => {
const message_container = document.getElementsByClassName(MESSAGE_CONTAINER_CLASS)[0];
const lastMessage = message_container.children[message_container.children.length - 1];
return lastMessage.getElementsByClassName(HOVER_TARGET)[0];
}

const hideMenu = () => {
const menu = document.querySelector("div._ak4w[role='application']");
menu.innerHTML = "";
}

const createMouseEvent = (t) => {
return new MouseEvent(t, {
'view': window,
'bubbles': true,
'cancelable': true
});
}

const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}