@@ -17,15 +17,54 @@ export async function download(text: string, filename: string) {
1717 ? URL . createObjectURL ( new Blob ( [ text ] , { type : 'text/markdown;charset=utf-8' } ) )
1818 : 'data:text/markdown;charset=utf-8,' + encodeURIComponent ( text ) ;
1919
20- try {
21- await chrome . downloads . download ( {
20+ return new Promise < void > ( ( resolve ) => {
21+ chrome . downloads . download ( {
2222 url,
2323 filename : filename + '.md' ,
2424 saveAs : false
25+ } , ( downloadId ) => {
26+ if ( isFirefox ) {
27+ chrome . downloads . onChanged . addListener ( function cleanup ( delta ) {
28+ if ( delta . id === downloadId && delta . state ?. current === 'complete' ) {
29+ chrome . downloads . onChanged . removeListener ( cleanup ) ;
30+ URL . revokeObjectURL ( url ) ;
31+ resolve ( ) ;
32+ }
33+ } ) ;
34+ } else {
35+ resolve ( ) ;
36+ }
2537 } ) ;
26- } finally {
27- if ( isFirefox ) URL . revokeObjectURL ( url ) ;
28- }
38+ } ) ;
39+ }
40+
41+ /**
42+ * Send the Markdown file to a webhook
43+ * @param text content of the Markdown file
44+ * @param filename name of the file
45+ */
46+ export async function sendToWebhook ( text : string , filename : string ) {
47+ chrome . storage . sync . get ( 'webhookUrl' , async ( data : { webhookUrl : any ; } ) => {
48+ const webhookUrl = data . webhookUrl ;
49+ if ( ! webhookUrl ) {
50+ console . info ( 'The webhook URL has not been configured.' ) ; // No se ha configurado la URL del webhook.
51+ return ;
52+ }
53+ console . log ( 'Sending file to webhook:' , webhookUrl ) ; // Enviando archivo al webhook:
54+ const formData = new FormData ( ) ;
55+ formData . append ( 'file' , new Blob ( [ text ] , { type : 'text/markdown' } ) , filename + '.md' ) ;
56+
57+ try {
58+ await axios . post ( webhookUrl , formData , {
59+ headers : {
60+ 'Content-Type' : 'multipart/form-data'
61+ }
62+ } ) ;
63+ console . log ( 'File successfully sent to webhook' ) ; // Archivo enviado exitosamente al webhook
64+ } catch ( error ) {
65+ console . error ( 'Error sending the file to the webhook:' , error ) ; // Error al enviar el archivo al webhook:
66+ }
67+ } ) ;
2968}
3069
3170// --- Content-script methods ---
@@ -68,32 +107,3 @@ export function linksToObsidian(content: string | number | boolean) {
68107 const encoded = encodeURIComponent ( content ) ;
69108 window . open ( `obsidian://advanced-uri?data=${ encoded } &mode=append` , '_blank' ) ;
70109}
71-
72- /**
73- * Send the Markdown file to a webhook
74- * @param text content of the Markdown file
75- * @param filename name of the file
76- */
77- export async function sendToWebhook ( text : string , filename : string ) {
78- chrome . storage . sync . get ( 'webhookUrl' , async ( data ) => {
79- const webhookUrl = data . webhookUrl ;
80- if ( ! webhookUrl ) {
81- console . info ( 'The webhook URL has not been configured.' ) ; // No se ha configurado la URL del webhook.
82- return ;
83- }
84- console . log ( 'Sending file to webhook:' , webhookUrl ) ; // Enviando archivo al webhook:
85- const formData = new FormData ( ) ;
86- formData . append ( 'file' , new Blob ( [ text ] , { type : 'text/markdown' } ) , filename + '.md' ) ;
87-
88- try {
89- await axios . post ( webhookUrl , formData , {
90- headers : {
91- 'Content-Type' : 'multipart/form-data'
92- }
93- } ) ;
94- console . log ( 'File successfully sent to webhook' ) ; // Archivo enviado exitosamente al webhook
95- } catch ( error ) {
96- console . error ( 'Error sending the file to the webhook:' , error ) ; // Error al enviar el archivo al webhook:
97- }
98- } ) ;
99- }
0 commit comments