This commit is contained in:
hiro98 2020-04-09 14:31:27 +02:00
commit b25e3c13d0
5 changed files with 130 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules

11
README.md Normal file
View file

@ -0,0 +1,11 @@
# Riot KaTeX
This extension injects KaTeX into the `Riot` chat client for
`matrix`. This currently works for just one domain
(matrix.tu-dresden.de). If you want to use it, you have to adjust the
`manifest.json`.
## Writing LaTeX
- `$$x^2$$` renders math block style
- `\(x^2\)` renders math inline

26
manifest.json Normal file
View file

@ -0,0 +1,26 @@
{
"manifest_version": 2,
"name": "Riot KaTeX",
"version": "1.0",
"description": "Renders formulars in Riot chats using KaTeX.",
"icons": {
"48": "icons/border-48.png"
},
"permissions": [
"activeTab"
],
"content_scripts": [
{
"matches": ["*://matrix.tu-dresden.de/*"],
"js": ["node_modules/katex/dist/katex.js",
"node_modules/katex/dist/contrib/auto-render.js",
"node_modules/observable-slim/observable-slim.js",
"riot-katex.js"],
"css": ["node_modules/katex/dist/katex.css"],
"all_frames": true,
"run_at": "document_idle"
}
]
}

24
package-lock.json generated Normal file
View file

@ -0,0 +1,24 @@
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"commander": {
"version": "2.20.3",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz",
"integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ=="
},
"katex": {
"version": "0.11.1",
"resolved": "https://registry.npmjs.org/katex/-/katex-0.11.1.tgz",
"integrity": "sha512-5oANDICCTX0NqYIyAiFCCwjQ7ERu3DQG2JFHLbYOf+fXaMoH8eg/zOq5WSYJsKMi/QebW+Eh3gSM+oss1H/bww==",
"requires": {
"commander": "^2.19.0"
}
},
"observable-slim": {
"version": "0.1.5",
"resolved": "https://registry.npmjs.org/observable-slim/-/observable-slim-0.1.5.tgz",
"integrity": "sha512-xhDK4EjwM6ehZ6KhTtDc0z5kqBhqoBQPP9pq2n1c+RXA4IcyLIm68ipDwS3nZVXhrCssoKu4LcHDUT1tl0kP9A=="
}
}
}

68
riot-katex.js Normal file
View file

@ -0,0 +1,68 @@
(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;
}
window.hasRun = true;
const math_config = [
{left: "$$", right: "$$", display: true},
{left: "\\(", right: "\\)", display: false},
{left: "\\[", right: "\\]", display: true}
]
function init() {
const header = document.querySelector('.mx_RoomList');
let chat_observer = listen_on_chat_element();
function listen_on_chat_element() {
const chat_elem = document.querySelector('.mx_RoomView_MessageList');
renderMathInElement(chat_elem, math_config);
const config = { attributes: true, childList: true, subtree: true };
const callback = function(mutationsList, observer) {
// Use traditional 'for loops' for IE 11
for(let mutation of mutationsList) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
for(let node of mutation.addedNodes) {
renderMathInElement(node, math_config);
}
}
}
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(chat_elem, config);
return observer;
}
function change_chat_element(mutationsList, observer) {
if (chat_observer) {
chat_observer.disconnect();
}
chat_observer = listen_on_chat_element();
}
const header_obs = new MutationObserver(change_chat_element)
const config = { attributes: true, childList: true, subtree: true };
header_obs.observe(header, config);
}
let = window.wrappedJSObject;
function wait_for_matrix() {
if(!('matrixChat' in window.wrappedJSObject
&& window.wrappedJSObject.matrixChat.firstSyncComplete))
return setTimeout(wait_for_matrix, 500);
init();
}
wait_for_matrix();
})();