Skip to content

Instantly share code, notes, and snippets.

@Sanix-Darker
Last active April 2, 2023 06:39
Show Gist options
  • Save Sanix-Darker/510754a73bdf6f94c2ef6ee9ce89ed3d to your computer and use it in GitHub Desktop.
Save Sanix-Darker/510754a73bdf6f94c2ef6ee9ce89ed3d to your computer and use it in GitHub Desktop.
Trigger emoji box on :: from the tweet modal text input.
// hit :: two times to have emoji popup open
let keystrokes = [];
const TARGET_EMOJI_POPUP_ON = ['::']
const LETTERS = [
'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X',
'Y', 'Z'
]
const FORBIDEN_STROKES = [
'Shift',
'Backspace',
'CapsLock',
'WakeUp',
'Alt',
'Control',
...LETTERS,
...LETTERS.map(e => e.toLowerCase())
]
// Twitter DOM elements
const tweetInput = document.querySelectorAll('div.public-DraftStyleDefault-block')[0]
const emojiButton = document.querySelectorAll('div[aria-label="Add emoji"]')[0]
if (typeof emojiButton === "undefined"){
alert("To use the emoji text, open a modal to tweet")
}else{
document.addEventListener('keydown', (event) => {
if (FORBIDEN_STROKES.indexOf(event.key) === -1){
if(keystrokes.length == 2) keystrokes.shift();
keystrokes.push(event.key);
if (
keystrokes.length == 2 &&
keystrokes[0] == keystrokes[1] &&
TARGET_EMOJI_POPUP_ON.indexOf(keystrokes.join('')) !== -1
){
// we want to delete our ::
for (var i=0; i < 2; i++)
window.dispatchEvent(
new KeyboardEvent('keydown', {
key: "Backspace",
})
);
// we open the emoji popUp
emojiButton.click();
// we wait for the popUp of the emoji to be opened
setTimeout(() => {
[...document.querySelectorAll(
'#emoji_picker_categories_dom_id > div[role="option"]'
)].forEach(
el => el.addEventListener(
'click', () => {
// we close the emoji popUp by clicking again
emojiButton.click();
}
))
}, 1000)
// we click on the select messsage to search emojis
keystrokes = [];
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment