2020-04-09 14:31:27 +02:00
|
|
|
(function() {
|
|
|
|
/**
|
|
|
|
* Check and set a global guard variable.
|
|
|
|
* If this content script is injected into the same page again,
|
|
|
|
* it will do nothing next time.
|
|
|
|
*/
|
|
|
|
if (window.hasRun) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-09 18:16:14 +02:00
|
|
|
// window.hasRun = true;
|
2020-04-09 14:31:27 +02:00
|
|
|
|
2020-04-09 16:46:59 +02:00
|
|
|
/**
|
|
|
|
* Set up math delimiters.
|
|
|
|
*/
|
2020-04-09 14:31:27 +02:00
|
|
|
const math_config = [
|
|
|
|
{left: "$$", right: "$$", display: true},
|
|
|
|
{left: "\\(", right: "\\)", display: false},
|
|
|
|
{left: "\\[", right: "\\]", display: true}
|
|
|
|
]
|
|
|
|
|
2020-04-09 18:16:14 +02:00
|
|
|
function renderMath(node) {
|
|
|
|
let li = node.closest('li');
|
2020-04-09 18:22:36 +02:00
|
|
|
|
|
|
|
// we have already rendered this element
|
|
|
|
if (li.getAttribute('originalContent')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:16:14 +02:00
|
|
|
let og_content = (' ' + node.textContent).slice(1);
|
|
|
|
li.setAttribute('originalContent', og_content);
|
|
|
|
li.querySelector('.mx_MessageActionBar_maskButton[title="Edit"]')
|
2020-04-09 18:22:36 +02:00
|
|
|
.addEventListener("click", event => {
|
|
|
|
event.target.closest('li').setAttribute('originalContent', '');
|
2020-04-09 18:16:14 +02:00
|
|
|
node.textContent = og_content;
|
|
|
|
});
|
|
|
|
renderMathInElement(node);
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:46:59 +02:00
|
|
|
/**
|
|
|
|
* Initialize Extension as soon as matrix has been loaded.
|
|
|
|
*/
|
2020-04-09 14:31:27 +02:00
|
|
|
function init() {
|
2020-04-09 16:46:59 +02:00
|
|
|
// render with KaTeX as soon as the message appears
|
2020-04-09 14:31:27 +02:00
|
|
|
function listen_on_chat_element() {
|
|
|
|
const chat_elem = document.querySelector('.mx_RoomView_MessageList');
|
2020-04-09 18:16:14 +02:00
|
|
|
//renderMathInElement(chat_elem, math_config);
|
|
|
|
for(let node of chat_elem.querySelectorAll('.mx_MTextBody')) {
|
|
|
|
renderMath(node);
|
|
|
|
}
|
2020-04-09 14:31:27 +02:00
|
|
|
|
|
|
|
const callback = function(mutationsList, observer) {
|
|
|
|
// Use traditional 'for loops' for IE 11
|
|
|
|
for(let mutation of mutationsList) {
|
2020-04-09 18:16:14 +02:00
|
|
|
if(mutation.type === 'attributes' && mutation.attributeName === 'class') {
|
|
|
|
let node = mutation.target.querySelector('.mx_MTextBody');
|
|
|
|
renderMath(node);
|
|
|
|
}
|
2020-04-09 14:31:27 +02:00
|
|
|
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
|
|
|
|
for(let node of mutation.addedNodes) {
|
2020-04-09 18:16:14 +02:00
|
|
|
node = node.querySelector('.mx_MTextBody');
|
|
|
|
renderMath(node);
|
2020-04-09 14:31:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-09 16:46:59 +02:00
|
|
|
const observer = new MutationObserver(callback);
|
|
|
|
observer.observe(chat_elem, { attributes: true, childList: true, subtree: true });
|
2020-04-09 14:31:27 +02:00
|
|
|
return observer;
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:16:14 +02:00
|
|
|
let last_title = ''
|
|
|
|
const header = document.querySelector('title');
|
2020-04-09 16:46:59 +02:00
|
|
|
// when changing the chat room, we have to create a new listener
|
2020-04-09 14:31:27 +02:00
|
|
|
function change_chat_element(mutationsList, observer) {
|
2020-04-09 18:16:14 +02:00
|
|
|
if(last_title == header.textContent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
last_title = header.textContent;
|
2020-04-09 14:31:27 +02:00
|
|
|
if (chat_observer) {
|
|
|
|
chat_observer.disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
chat_observer = listen_on_chat_element();
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:46:59 +02:00
|
|
|
// start listening
|
|
|
|
let chat_observer = listen_on_chat_element();
|
|
|
|
|
|
|
|
// start listening on the header as well
|
2020-04-09 14:31:27 +02:00
|
|
|
const header_obs = new MutationObserver(change_chat_element)
|
2020-04-09 16:46:59 +02:00
|
|
|
header_obs.observe(header, { attributes: true, childList: true, subtree: true });
|
2020-04-09 14:31:27 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:46:59 +02:00
|
|
|
// a clever hack to check if riot has been loaded yet
|
2020-04-09 14:31:27 +02:00
|
|
|
function wait_for_matrix() {
|
|
|
|
if(!('matrixChat' in window.wrappedJSObject
|
|
|
|
&& window.wrappedJSObject.matrixChat.firstSyncComplete))
|
|
|
|
return setTimeout(wait_for_matrix, 500);
|
|
|
|
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
wait_for_matrix();
|
|
|
|
})();
|