assassin-bug/framework/tts/outputs/aria.js

33 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-11-26 01:22:02 +00:00
import { BaseOutput } from './base-output';
export class AriaOutput extends BaseOutput {
constructor(options = {}) {
super();
this.timeout = 100;
this.timeout = options.timeout || 100;
this.init();
}
init() {
this.container = document.createElement('div');
this.container.setAttribute('aria-live', 'polite');
this.speechDisplay = document.createElement('div');
this.speechDisplay.setAttribute('aria-live', 'polite');
this.container.append(this.speechDisplay);
document.body.appendChild(this.container);
document.body.insertBefore(this.container, document.body.firstChild);
}
speak(text) {
this.clearDisplay();
const node = document.createTextNode(text);
const para = document.createElement('p');
para.appendChild(node);
this.speechDisplay.appendChild(para);
setTimeout(this.clearDisplay.bind(this), this.timeout);
}
stop() {
this.clearDisplay();
}
clearDisplay() {
this.speechDisplay.innerHTML = '';
}
}