author Santo Pfingsten
 *  see https import * as MarkdownIt from "markdown-it" ; import { browser } from "webextension-polyfill-ts" ; import { wetLayer } from "wet-layer" ; const md = new MarkdownIt ( ) ; const domParser = new DOMParser ( ) ; function setMarkdown ( element , value ) { const doc = domParser . parseFromString ( md . render ( value ) , "text/html" ) ; removeAllChildren ( element ) ; for ( const child of doc . body . childNodes ) element . appendChild ( child ) ; const links = element . querySelectorAll ( "a" ) ; for ( const link of links ) { link . target = "_blank" ; link . addEventListener ( "click" , handleClickOpenNewTab ) ; } } export function byId ( id ) { return document . getElementById ( id ) ; } export function on < K extends keyof HTMLElementEventMap > ( node , event , callback ) { node . addEventListener ( event , callback as EventListener ) ; } export function translateElement ( element ) { const i18n = element . dataset . i18n ; if ( i18n ) { let parts = i18n . split ( "?" ) ; const id = parts [ 0 ] ; parts . splice ( 0 , 1 ) ; if ( parts . length === 0 ) parts = [ "text" ] ; for ( const attribute of parts ) { if ( attribute === "text" ) element . textContent = wetLayer . getMessage ( id ) ; else if ( attribute === "markdown" ) setMarkdown ( element , wetLayer . getMessage ( id ) ) ; else ( element as any ) [ attribute ] = wetLayer . getMessage ( id + "@" + attribute ) ; } } } export function translateChildren ( parent ) { const elements = parent . querySelectorAll ( "[data-i18n]" ) ; for ( const element of elements ) translateElement ( element as HTMLElement ) ; } export function translateDocument ( ) { document . head && translateChildren ( document . head ) ; document . body && translateChildren ( document . body ) ; } export function removeAllChildren ( node ) { while ( node . firstChild ) node . removeChild ( node . firstChild ) ; } export function handleClickOpenNewTab ( e ) { e . stopImmediatePropagation ( ) ; e . preventDefault ( ) ; browser . tabs . create ( { active : true , url : ( e . currentTarget as HTMLAnchorElement ) . href } ) ; window . close ( ) ; }