1
1
import handlers from "../lib/index" ;
2
- import { RedirectSettings } from "../types/mastodon" ;
3
- import { validateUrl } from "../utils" ;
2
+ import { getSettings , setSettings , validateUrl } from "../utils" ;
4
3
5
4
let currentlyHandling : string ;
6
5
const dialog = document . getElementById ( "code_dialog" ) as HTMLDialogElement ;
@@ -19,13 +18,15 @@ const handler = async (event: SubmitEvent) => {
19
18
20
19
const app = await handler . createApp ( instance ) ;
21
20
22
- await browser . storage . local . set ( {
23
- [ handler . type ] : {
24
- code : undefined ,
25
- instance : instance . toString ( ) ,
26
- client_id : app . client_id ,
27
- client_secret : app . client_secret ,
28
- } as RedirectSettings ,
21
+ await setSettings ( {
22
+ handlers : {
23
+ [ handler . type ] : {
24
+ code : undefined ,
25
+ instance : instance . toString ( ) ,
26
+ client_id : app . client_id ,
27
+ client_secret : app . client_secret ,
28
+ } ,
29
+ } ,
29
30
} ) ;
30
31
31
32
currentlyHandling = handler . type ;
@@ -36,17 +37,17 @@ const handler = async (event: SubmitEvent) => {
36
37
} ) ;
37
38
} ;
38
39
39
- const forms = document . querySelectorAll (
40
+ const handlerforms = document . querySelectorAll (
40
41
".handler form" ,
41
42
) as NodeListOf < HTMLFormElement > ;
42
- [ ...forms ] . forEach ( async ( x ) => {
43
+ [ ...handlerforms ] . forEach ( async ( x ) => {
43
44
x . addEventListener ( "submit" , handler ) ;
44
45
45
- const opts = ( await browser . storage . local . get ( ) ) [
46
+ const opts = ( await getSettings ( ) ) ?. handlers ?. [
46
47
x . getAttribute ( "data-type" ) !
47
- ] as RedirectSettings ;
48
+ ] ;
48
49
49
- if ( opts . instance )
50
+ if ( opts ? .instance )
50
51
( x . elements . namedItem ( "url" ) as HTMLInputElement ) . value = opts . instance ;
51
52
} ) ;
52
53
@@ -55,17 +56,36 @@ document
55
56
. addEventListener ( "submit" , async ( event ) => {
56
57
event . preventDefault ( ) ;
57
58
58
- const opts = ( await browser . storage . local . get ( ) ) [
59
- currentlyHandling
60
- ] as RedirectSettings ;
59
+ const opts = ( await getSettings ( ) ) ?. handlers ?. [ currentlyHandling ] ;
61
60
62
61
const data = new FormData ( event . target as HTMLFormElement ) ;
63
- await browser . storage . local . set ( {
64
- [ currentlyHandling ] : {
65
- ...opts ,
66
- code : data . get ( "code" ) ,
67
- } as RedirectSettings ,
62
+ await setSettings ( {
63
+ handlers : {
64
+ [ currentlyHandling ] : {
65
+ code : data . get ( "code" ) ?. toString ( ) ,
66
+ } ,
67
+ } ,
68
68
} ) ;
69
69
70
70
dialog . close ( ) ;
71
71
} ) ;
72
+
73
+ const settings = document . querySelectorAll (
74
+ "#settings input" ,
75
+ ) as NodeListOf < HTMLInputElement > ;
76
+ [ ...settings ] . forEach ( async ( x ) => {
77
+ const key = x . getAttribute ( "name" ) ;
78
+ if ( ! key ) return alert ( "you forgot to set the input name" ) ;
79
+
80
+ ( x as HTMLInputElement ) . addEventListener ( "change" , async ( event ) => {
81
+ const target = event . target as HTMLInputElement ;
82
+ let value : string | boolean = target . value ;
83
+ if ( target . getAttribute ( "type" ) == "checkbox" ) value = target . checked ;
84
+ await setSettings ( { [ key ] : value } ) ;
85
+ } ) ;
86
+
87
+ const settings = await getSettings ( ) ;
88
+ const value = settings [ key as keyof typeof settings ] ; // silly
89
+ if ( x . getAttribute ( "type" ) == "checkbox" ) x . checked = value as boolean ;
90
+ else x . value = value as any as string ;
91
+ } ) ;
0 commit comments