1- import LZString from 'lz-string'
1+ async function gzipCompress ( input : string ) : Promise < Uint8Array > {
2+ const encoder = new TextEncoder ( )
3+ const data = encoder . encode ( input )
4+
5+ const cs = new CompressionStream ( 'gzip' )
6+ const writer = cs . writable . getWriter ( )
7+ writer . write ( data )
8+ writer . close ( )
9+
10+ const reader = cs . readable . getReader ( )
11+ const chunks : Uint8Array [ ] = [ ]
12+ while ( true ) {
13+ const { done, value } = await reader . read ( )
14+ if ( done ) break
15+ chunks . push ( value )
16+ }
217
3- export function createShareUrl ( inputContent : string , tabName ?: string ) : { url : string ; length : number } {
4- // Compress only the input content
5- const compressed = LZString . compressToEncodedURIComponent ( inputContent )
18+ const totalLength = chunks . reduce ( ( sum , chunk ) => sum + chunk . length , 0 )
19+ const result = new Uint8Array ( totalLength )
20+ let offset = 0
21+ for ( const chunk of chunks ) {
22+ result . set ( chunk , offset )
23+ offset += chunk . length
24+ }
25+ return result
26+ }
27+
28+ function toBase64Url ( bytes : Uint8Array ) : string {
29+ let binary = ''
30+ for ( let i = 0 ; i < bytes . length ; i ++ ) {
31+ binary += String . fromCharCode ( bytes [ i ] )
32+ }
33+ return btoa ( binary ) . replace ( / \+ / g, '-' ) . replace ( / \/ / g, '_' ) . replace ( / = + $ / , '' )
34+ }
35+
36+ export async function createShareUrl ( inputContent : string , tabName ?: string ) : Promise < { url : string ; length : number } > {
37+ const compressed = await gzipCompress ( inputContent )
38+ const encoded = toBase64Url ( compressed )
639
7- // Create share URL
840 const baseUrl = window . location . origin + window . location . pathname
9- let shareUrl = `${ baseUrl } ?s =${ compressed } `
41+ let shareUrl = `${ baseUrl } ?c =${ encoded } `
1042
11- // Add tab name parameter if provided
1243 if ( tabName ) {
1344 shareUrl += `&t=${ encodeURIComponent ( tabName ) } `
1445 }
@@ -19,43 +50,6 @@ export function createShareUrl(inputContent: string, tabName?: string): { url: s
1950 }
2051}
2152
22- export function importFromUrl ( compressedData : string ) : string {
23- try {
24- // Decompress the data
25- const inputContent = LZString . decompressFromEncodedURIComponent ( compressedData )
26-
27- if ( ! inputContent ) {
28- throw new Error ( 'Invalid share link: Unable to decompress data' )
29- }
30-
31- return inputContent
32- } catch ( error ) {
33- console . error ( 'Error importing from URL:' , error )
34- throw new Error ( 'Failed to import shared content. Please check the link and try again.' )
35- }
36- }
37-
38- export function importFromBase64Url ( base64Data : string ) : string {
39- try {
40- // Convert base64url to standard base64
41- let base64 = base64Data . replace ( / - / g, '+' ) . replace ( / _ / g, '/' )
42- // Add padding if needed
43- while ( base64 . length % 4 !== 0 ) {
44- base64 += '='
45- }
46- const inputContent = decodeURIComponent ( escape ( atob ( base64 ) ) )
47-
48- if ( ! inputContent ) {
49- throw new Error ( 'Invalid share link: Unable to decode data' )
50- }
51-
52- return inputContent
53- } catch ( error ) {
54- console . error ( 'Error importing from base64 URL:' , error )
55- throw new Error ( 'Failed to import shared content. Please check the link and try again.' )
56- }
57- }
58-
5953export async function importFromCompressedUrl ( compressedData : string ) : Promise < string > {
6054 try {
6155 // Convert base64url to standard base64
@@ -120,7 +114,7 @@ export function copyToClipboard(text: string): Promise<void> {
120114 document . body . appendChild ( textArea )
121115 textArea . focus ( )
122116 textArea . select ( )
123-
117+
124118 try {
125119 document . execCommand ( 'copy' )
126120 resolve ( )
@@ -131,4 +125,4 @@ export function copyToClipboard(text: string): Promise<void> {
131125 }
132126 } )
133127 }
134- }
128+ }
0 commit comments