@@ -3,45 +3,33 @@ const CONTEXT_MENUS = {
3
3
SEND_WITH_CLIPBOARD : "send_with_clipboard" ,
4
4
} ;
5
5
6
- const defaultMethod = "POST" ;
7
- const toVRC = ( url : string , method : string = defaultMethod ) => {
8
- fetch ( "http://localhost:11400/url" , {
9
- method : method ,
10
- mode : "cors" ,
11
- credentials : "omit" ,
12
- cache : "no-cache" ,
13
- headers : { "Content-Type" : "application/json" } ,
14
- body : JSON . stringify ( { url : url } ) ,
15
- } )
16
- . then ( ( res ) => {
17
- if ( ! res . ok && method !== "PUT" ) {
18
- toVRC ( url , "PUT" ) ;
19
- }
6
+ const ERROR_NOTIFICATION_OPTIONS : chrome . notifications . NotificationOptions < true > =
7
+ {
8
+ type : "basic" ,
9
+ iconUrl : "./icon.png" ,
10
+ title : "send_vrc" ,
11
+ message : "please start send_vrc_desktop." ,
12
+ } as const ;
20
13
21
- if ( ! res . ok && method === "PUT" ) {
22
- chrome . notifications . create ( {
23
- type : "basic" ,
24
- iconUrl : "./icon.png" ,
25
- title : "send_vrc" ,
26
- message : "please start send_vrc_desktop." ,
27
- } ) ;
28
- }
29
- } )
30
- . catch ( ( e ) => {
31
- console . log ( e , method ) ;
32
- // old version compatible.
33
- if ( method !== "PUT" ) {
34
- toVRC ( url , "PUT" ) ;
35
- }
36
- if ( method === "PUT" ) {
37
- chrome . notifications . create ( {
38
- type : "basic" ,
39
- iconUrl : "./icon.png" ,
40
- title : "send_vrc" ,
41
- message : "please start send_vrc_desktop." ,
42
- } ) ;
43
- }
14
+ const sendUrlToVRC = async ( url : string ) => {
15
+ try {
16
+ const res = await fetch ( "http://localhost:11400/url" , {
17
+ method : "POST" ,
18
+ mode : "cors" ,
19
+ credentials : "omit" ,
20
+ cache : "no-cache" ,
21
+ headers : { "Content-Type" : "application/json" } ,
22
+ body : JSON . stringify ( { url : url } ) ,
44
23
} ) ;
24
+
25
+ if ( ! res . ok ) {
26
+ console . log ( res . statusText ) ;
27
+ chrome . notifications . create ( ERROR_NOTIFICATION_OPTIONS ) ;
28
+ }
29
+ } catch ( e ) {
30
+ console . log ( e ) ;
31
+ chrome . notifications . create ( ERROR_NOTIFICATION_OPTIONS ) ;
32
+ }
45
33
} ;
46
34
47
35
const getClipboard = async ( tabId : number ) => {
@@ -87,28 +75,31 @@ chrome.contextMenus.create({
87
75
chrome . contextMenus . onClicked . addListener ( async ( info , tab ) => {
88
76
switch ( info . menuItemId ) {
89
77
case CONTEXT_MENUS . SEND_THIS_PAGE : {
90
- if ( ! info || ! info [ " pageUrl" ] ) {
78
+ if ( ! info . pageUrl ) {
91
79
return ;
92
80
}
93
- const pageURL = info [ "pageUrl" ] ;
94
- toVRC ( pageURL ) ;
81
+
82
+ const url = canonicalizeUrl ( info . pageUrl ) ;
83
+ sendUrlToVRC ( url ) ;
95
84
break ;
96
85
}
97
86
case CONTEXT_MENUS . SEND_WITH_CLIPBOARD : {
98
- if ( ! tab || tab . id === undefined ) {
87
+ if ( tab ? .id === undefined ) {
99
88
return ;
100
89
}
90
+
101
91
const clipboardText = await getClipboard ( tab . id ) ;
102
- toVRC ( clipboardText ) ;
92
+ sendUrlToVRC ( clipboardText ) ;
103
93
break ;
104
94
}
105
95
}
106
96
} ) ;
107
97
108
98
chrome . action . onClicked . addListener ( ( e ) => {
109
- if ( ! e || ! e [ " url" ] ) {
99
+ if ( ! e . url ) {
110
100
return ;
111
101
}
112
- const url = canonicalizeUrl ( e [ "url" ] ) ;
113
- toVRC ( url ) ;
102
+
103
+ const url = canonicalizeUrl ( e . url ) ;
104
+ sendUrlToVRC ( url ) ;
114
105
} ) ;
0 commit comments