const electronPkg = require ( "electron" ) ; export default function getPageTitle ( url ) { if ( electronPkg != null ) { const { remote , ipcRenderer } = electronPkg ; const { BrowserWindow , ipcMain } = remote ; return new Promise < string > ( ( resolve ) => { try { const window = new BrowserWindow ( { width : 1000 , height : 600 , webPreferences : { webSecurity : false , nodeIntegration : true , images : false , } , show : false , } ) ; window . webContents . on ( "did-finish-load" , async ( ) => { try { ipcRenderer . send ( 'pageloaded' , window . webContents . getTitle ( ) ) ; } catch ( ex ) { resolve ( "Title Unknown" ) ; } } ) ; window . loadURL ( url ) ; ipcMain . on ( "pageloaded" , ( _event , title ) => { window . destroy ( ) ; if ( title != null && title != '' ) { resolve ( title ) ; } else { resolve ( "Title Unknown" ) ; } } ) ; } catch ( ex ) { resolve ( "Site Unreachable" ) ; } } ) ; } else { return new Promise < string > ( ( resolve ) => { var corsed = `https://api.allorigins.win/get?url=${ encodeURIComponent ( url ) }` ; fetch ( corsed ) . then ( ( response ) => { return response . text ( ) ; } ) . then ( ( html ) => { const doc = new DOMParser ( ) . parseFromString ( html , "text/html" ) ; const title = doc . querySelectorAll ( "title" ) [ 0 ] ; if ( title == null || title . innerText . length == 0 ) { var noTitle = title . getAttr ( "no-title" ) ; if ( noTitle != null ) return noTitle ; return resolve ( "Title Unknown" ) ; } return resolve ( title . innerText ) ; } ) . catch ( ( error ) => { console . error ( error ) ; return resolve ( "Site Unreachable" ) ; } ) ; } ) ; } }