2023-12-27 17:25:49 +00:00
window . matrix = ( opts ) => new Proxy ( {
2023-12-27 21:31:42 +00:00
el : null , // HTML element
2023-12-27 17:25:49 +00:00
plugin : {
type : 'network' ,
2024-01-03 14:23:34 +00:00
name : '[Matrix]' ,
2023-12-27 21:31:42 +00:00
description : 'a standardized decentralized privacy-friendly protocol' ,
2023-12-27 17:25:49 +00:00
url : 'https://matrix.org' ,
2023-12-27 21:31:42 +00:00
protocol : 'matrix://' ,
2023-12-27 17:25:49 +00:00
video : false ,
audio : false ,
chat : true ,
scene : true
} ,
html : {
2023-12-27 21:31:42 +00:00
generic : ( opts ) => ` <div>
< a href = "${opts.url}" target = "_blank" class = "badge ruler" > matrix < / a >
< table >
< tr >
< td > channel < / t d >
< td >
< input type = "text" id = "channel" placeholder = "#xrfragment:matrix.org" / >
< / t d >
< / t r >
< tr >
< td > server < / t d >
< td >
< input type = "text" id = "server" placeholder = "https://matrix.org" / >
< / t d >
< / t r >
< tr >
< td > user < / t d >
< td >
< input type = "text" id = "username" placeholder = "@you:matrix.org" / >
< / t d >
< / t r >
< tr >
2023-12-28 09:22:54 +00:00
< td > auth < / t d >
2023-12-27 21:31:42 +00:00
< td >
2023-12-28 09:22:54 +00:00
< select id = "auth" >
2023-12-27 21:31:42 +00:00
< option > via password < / o p t i o n >
< option > via access token < / o p t i o n >
< / s e l e c t >
< / t d >
< / t r >
2023-12-27 17:25:49 +00:00
< tr >
2023-12-27 21:31:42 +00:00
< td > < / t d >
2023-12-27 17:25:49 +00:00
< td >
2023-12-28 09:22:54 +00:00
< input type = "text" id = "secret" placeholder = "enter password" / >
2023-12-27 17:25:49 +00:00
< / t d >
< / t r >
< / t a b l e >
2023-12-27 21:31:42 +00:00
< small style = "display:inline-block;float:right" > Support for Oauth / OpenID is < a href = "https://matrix.org/blog/#openid-connect" target = "_blank" > in progress < / a > < / s m a l l >
2024-01-03 14:23:34 +00:00
< br >
2023-12-27 17:25:49 +00:00
< / d i v >
`
} ,
init ( ) {
2024-01-03 14:23:34 +00:00
frontend . plugin [ 'matrix' ] = this
2023-12-27 17:25:49 +00:00
$connections . chatnetwork = $connections . chatnetwork . concat ( [ this ] )
$connections . scene = $connections . scene . concat ( [ this ] )
2023-12-27 21:31:42 +00:00
this . reactToConnectionHrefs ( )
} ,
connect ( opts ) {
console . log ( "connecting " + this . plugin . name )
console . dir ( opts )
2023-12-27 17:25:49 +00:00
} ,
config ( opts ) {
opts = { ... opts , ... this . plugin }
2023-12-27 21:31:42 +00:00
this . el = document . createElement ( 'div' )
2023-12-27 17:25:49 +00:00
let html = this . html . generic ( opts )
for ( let i in opts ) {
if ( this . html [ i ] ) html += this . html [ i ] ( opts )
}
2023-12-27 21:31:42 +00:00
this . el . innerHTML = html
2023-12-28 09:22:54 +00:00
this . el . querySelector ( '#auth' ) . addEventListener ( 'change' , ( e ) => {
this . el . querySelector ( '#secret' ) . setAttribute ( 'placeholder' , ` enter ${ e . target . value . replace ( /.* / , '' ) } ` )
} )
2023-12-27 21:31:42 +00:00
window . notify ( ` ${ opts . name } is ${ opts . description } , it is the hottest internet technology available at this moment.<br>Read more about it <a href=" ${ opts . url } " target="_blank">here</a>.<br>You can basically make up a new channelname or use an existing one ` )
return this . el
} ,
reactToConnectionHrefs ( ) {
xrf . addEventListener ( 'href' , ( opts ) => {
let { mesh } = opts
if ( ! opts . click ) return
if ( mesh . userData . href . match ( this . protocol ) ) {
let parts = mesh . userData . href . replace ( this . plugin . protocol , '' )
if ( parts [ 0 ] == 'r' ) { // room
let server = parts . split ( "/" ) [ 1 ] . replace ( /:.*/ , '' )
let channel = parts . split ( "/" ) [ 1 ] . replace ( /.*:/ , '' )
2023-12-28 09:22:54 +00:00
$connections . show ( )
$connections . selectedChatnetwork = this . plugin . name
$connections . selectedScene = this . plugin . name
2023-12-27 21:31:42 +00:00
this . el . querySelector ( '#channel' ) . value = ` # ${ channel } : ${ server } `
this . el . querySelector ( '#server' ) . value = server
console . log ( "configured matrix" )
}
} else window . notify ( "malformed connection URI: " + mesh . userData . href )
2023-12-27 17:25:49 +00:00
} )
}
} ,
{
// auto-trigger events on changes
get ( data , k , receiver ) { return data [ k ] } ,
set ( data , k , v ) {
let from = data [ k ]
data [ k ] = v
2023-12-27 21:31:42 +00:00
//switch( k ){
// default: matrix.opts.scene.dispatchEvent({type:`matrix.${k}.change`, from, to:v})
//}
2023-12-27 17:25:49 +00:00
}
} )
document . addEventListener ( '$connections:ready' , ( e ) => {
matrix ( e . detail ) . init ( )
} )