2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								async  function  sendMessage ( message )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    try  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return  await  browser . runtime . sendMessage ( message ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    catch  ( e )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        console . log ( "Calling browser runtime failed:" ,  e ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        alert ( "Calling browser runtime failed. Refreshing page might help." ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  $showOptionsButton  =  $ ( "#show-options-button" ) ;  
						 
					
						
							
								
									
										
										
										
											2022-02-19 19:38:01 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const  $saveCroppedScreenShotButton  =  $ ( "#save-cropped-screenshot-button" ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  $saveWholeScreenShotButton  =  $ ( "#save-whole-screenshot-button" ) ;  
						 
					
						
							
								
									
										
										
										
											2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								const  $saveWholePageButton  =  $ ( "#save-whole-page-button" ) ;  
						 
					
						
							
								
									
										
										
										
											2021-02-22 01:40:07 +06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const  $saveTabsButton  =  $ ( "#save-tabs-button" ) ;  
						 
					
						
							
								
									
										
										
										
											2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$showOptionsButton . on ( "click" ,  ( )  =>  browser . runtime . openOptionsPage ( ) ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-19 19:38:01 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$saveCroppedScreenShotButton . on ( "click" ,  ( )  =>  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    sendMessage ( { name :  'save-cropped-screenshot' } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    window . close ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$saveWholeScreenShotButton . on ( "click" ,  ( )  =>  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    sendMessage ( { name :  'save-whole-screenshot' } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    window . close ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
									
										
										
										
											2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$saveWholePageButton . on ( "click" ,  ( )  =>  sendMessage ( { name :  'save-whole-page' } ) ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-22 01:40:07 +06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$saveTabsButton . on ( "click" ,  ( )  =>  sendMessage ( { name :  'save-tabs' } ) ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-03-31 22:23:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const  $saveLinkWithNoteWrapper  =  $ ( "#save-link-with-note-wrapper" ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  $textNote  =  $ ( "#save-link-with-note-textarea" ) ;  
						 
					
						
							
								
									
										
										
										
											2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$textNote . on ( 'keypress' ,  function  ( event )  {  
						 
					
						
							
								
									
										
										
										
											2021-03-31 22:23:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if  ( ( event . which  ===  10  ||  event . which  ===  13 )  &&  event . ctrlKey )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        saveLinkWithNote ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        return  false ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return  true ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-03-31 22:23:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$ ( "#save-link-with-note-button" ) . on ( "click" ,  ( )  =>  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    $saveLinkWithNoteWrapper . show ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    $textNote [ 0 ] . focus ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$ ( "#cancel-button" ) . on ( "click" ,  ( )  =>  {  
						 
					
						
							
								
									
										
										
										
											2021-03-31 22:23:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    $saveLinkWithNoteWrapper . hide ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    $textNote . val ( "" ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    window . close ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-03-31 22:23:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								async  function  saveLinkWithNote ( )  {  
						 
					
						
							
								
									
										
										
										
											2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    const  textNoteVal  =  $textNote . val ( ) . trim ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    let  title ,  content ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-03-31 22:23:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if  ( ! textNoteVal )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        title  =  '' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        content  =  '' ; 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    else  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-03-31 22:23:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        const  match  =  /^(.*?)([.?!]\s|\n)/ . exec ( textNoteVal ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if  ( match )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            title  =  match [ 0 ] . trim ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            content  =  textNoteVal . substr ( title . length ) . trim ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            title  =  textNoteVal ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            content  =  '' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    content  =  escapeHtml ( content ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-03-31 22:23:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const  result  =  await  sendMessage ( { name :  'save-link-with-note' ,  title ,  content } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( result )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        $textNote . val ( '' ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        window . close ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-03-31 22:23:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$ ( "#save-button" ) . on ( "click" ,  saveLinkWithNote ) ;  
						 
					
						
							
								
									
										
										
										
											2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$ ( "#show-help-button" ) . on ( "click" ,  ( )  =>  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    window . open ( "https://github.com/zadam/trilium/wiki/Web-clipper" ,  '_blank' ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function  escapeHtml ( string )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const  pre  =  document . createElement ( 'pre' ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const  text  =  document . createTextNode ( string ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    pre . appendChild ( text ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const  htmlWithPars  =  pre . innerHTML . replace ( /\n/g ,  "</p><p>" ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return  '<p>'  +  htmlWithPars  +  '</p>' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  $connectionStatus  =  $ ( "#connection-status" ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  $needsConnection  =  $ ( ".needs-connection" ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								browser . runtime . onMessage . addListener ( request  =>  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( request . name  ===  'trilium-search-status' )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        const  { triliumSearch }  =  request ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        let  statusText  =  triliumSearch . status ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        let  isConnected ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if  ( triliumSearch . status  ===  'not-found' )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            statusText  =  ` <span style="color: red">Not found</span> ` ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            isConnected  =  false ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-20 12:17:59 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        else  if  ( triliumSearch . status  ===  'version-mismatch' )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            const  whatToUpgrade  =  triliumSearch . extensionMajor  >  triliumSearch . triliumMajor  ?  "Trilium Notes"  :  "this extension" ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            statusText  =  ` <span style="color: orange">Trilium instance found, but it is not compatible with this extension version. Please update  ${ whatToUpgrade }  to the latest version.</span> ` ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            isConnected  =  true ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-19 20:35:53 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        else  if  ( triliumSearch . status  ===  'found-desktop' )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            statusText  =  ` <span style="color: green">Connected on port  ${ triliumSearch . port } </span> ` ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            isConnected  =  true ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        else  if  ( triliumSearch . status  ===  'found-server' )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            statusText  =  ` <span style="color: green" title="Connected to  ${ triliumSearch . url } ">Connected to the server</span> ` ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            isConnected  =  true ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        $connectionStatus . html ( statusText ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if  ( isConnected )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            $needsConnection . removeAttr ( "disabled" ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            $needsConnection . removeAttr ( "title" ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            $needsConnection . attr ( "disabled" ,  "disabled" ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            $needsConnection . attr ( "title" ,  "This action can't be performed without active connection to Trilium." ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  $checkConnectionButton  =  $ ( "#check-connection-button" ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$checkConnectionButton . on ( "click" ,  ( )  =>  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    browser . runtime . sendMessage ( { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        name :  "trigger-trilium-search" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-03-31 22:23:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$ ( ( )  =>  browser . runtime . sendMessage ( { name :  "send-trilium-search-status" } ) ) ;