Added anchorme to make chat messages with links clickable

This commit is contained in:
joker-x 2020-09-16 03:49:34 +02:00 committed by Jonas Herzig
parent 78161b3a2c
commit 7692a02ab7
3 changed files with 27 additions and 4 deletions

View file

@ -9,6 +9,7 @@ import audioContext from 'audio-context'
import ko from 'knockout'
import _dompurify from 'dompurify'
import keyboardjs from 'keyboardjs'
import anchorme from 'anchorme'
import { ContinuousVoiceHandler, PushToTalkVoiceHandler, VADVoiceHandler, initVoice } from './voice'
import {initialize as localizationInitialize, translate} from './loc';
@ -17,10 +18,25 @@ const dompurify = _dompurify(window)
function sanitize (html) {
return dompurify.sanitize(html, {
ALLOWED_TAGS: ['br', 'b', 'i', 'u', 'a', 'span', 'p']
ALLOWED_TAGS: ['br', 'b', 'i', 'u', 'a', 'span', 'p', 'img', 'center']
})
}
const anchormeOptions = {
// force target _blank attribute
attributes: {
target: "_blank"
},
// force https protocol except email
protocol: function(s) {
if (anchorme.validate.email(s)) {
return "mailto:";
} else {
return "https://";
}
}
}
function openContextMenu (event, contextMenu, target) {
contextMenu.posX(event.clientX)
contextMenu.posY(event.clientY)
@ -405,7 +421,7 @@ class GlobalBindings {
type: 'chat-message',
user: sender.__ui,
channel: channels.length > 0,
message: sanitize(message)
message: anchorme({input: sanitize(message), options: anchormeOptions})
})
})
@ -779,13 +795,13 @@ class GlobalBindings {
if (target.users) { // Channel
this.log.push({
type: 'chat-message-self',
message: sanitize(message),
message: anchorme({input: sanitize(message), options: anchormeOptions}),
channel: target
})
} else { // User
this.log.push({
type: 'chat-message-self',
message: sanitize(message),
message: anchorme({input: sanitize(message), options: anchormeOptions}),
user: target
})
}