Skip to content

Instantly share code, notes, and snippets.

@ulternate
Created February 10, 2020 04:38
Show Gist options
  • Save ulternate/01d00c98ebde8302cd8967b89efb1582 to your computer and use it in GitHub Desktop.
Save ulternate/01d00c98ebde8302cd8967b89efb1582 to your computer and use it in GitHub Desktop.
Convert text to emoji in open Bitbucket editor.
// Use as a bookmarklet by saving a bookmark with `javascript:${codeBelow}`.
// Will change all characters from `a` to `:regional_indicator_a` in an open Bitbucket editor.
// [a-zA-Z0-9#] supported.
// Working in the bitbucket editor at the time this gist was created (10/02/2020).
(function() {
'use strict';
// Get the open editor.
const editorWrapper = document.getElementsByClassName('ak-editor-content-area')[0];
const editor = editorWrapper.querySelectorAll('[contenteditable="true"]')[0];
// Make the content use the emoji if it's an alphabetical character.
const existingText = editor.textContent;
const charArray = [...existingText];
const newCharArray = [];
charArray.forEach((char) => {
if (/[a-zA-Z]/.test(char)) {
newCharArray.push(`:regional_indicator_${char.toLowerCase()}:`)
} else if (/[0-9#]|10/.test(char)) {
switch (char.toLowerCase()) {
case '0':
newCharArray.push(':zero:');
break;
case '1':
newCharArray.push(':one:');
break;
case '2':
newCharArray.push(':two:');
break;
case '3':
newCharArray.push(':three:');
break;
case '4':
newCharArray.push(':four:');
break;
case '5':
newCharArray.push(':five:');
break;
case '6':
newCharArray.push(':six:');
break;
case '7':
newCharArray.push(':seven:');
break;
case '8':
newCharArray.push(':eight:');
break;
case '9':
newCharArray.push(':nine:');
break;
case '10':
newCharArray.push(':keycap_ten:');
break;
case '#':
newCharArray.push(':hash:');
break;
default:
break;
}
} else if (/[ ]/.test(char)) {
// Bitbucket strips spaces out, so use `___` to represent a space.
newCharArray.push('__');
} else {
newCharArray.push(char);
}
});
const newMessageString = newCharArray.join(' ');
// Replace the content.
editor.textContent = newMessageString;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment