@@ -12,6 +12,16 @@ import * as htmlToImage from 'html-to-image'
1212import fs from 'fs/promises'
1313import path from 'path'
1414
15+ interface Data {
16+ title : string
17+ favicon : string
18+ }
19+
20+ let data : Data = {
21+ title : '' ,
22+ favicon : '' ,
23+ }
24+
1525if ( import . meta. env . MODE === 'development' ) {
1626 // Ensure that all modules are loaded
1727 console . log ( 'Development mode' )
@@ -37,25 +47,87 @@ if (import.meta.env.MODE === 'development') {
3747 * Collect and send back information from the document context
3848 * Only done during the initial bridging
3949 */
40- document . addEventListener ( 'DOMContentLoaded' , initiateBridge )
50+ document . addEventListener ( 'DOMContentLoaded' , init )
51+ // document.addEventListener('load', initiateBridge)
4152
42- function initiateBridge ( ) {
43- const data = {
44- title : document . title ,
45- favicon :
46- 'https://www.google.com/s2/favicons?domain=' + window . location . href ,
47- }
53+ async function init ( ) {
54+ // Communicate back to the Electron app
55+ await initiateBridge ( )
56+ }
57+
58+ async function getPageData ( ) {
59+ data . title = await getPageTitle ( )
60+ console . log ( 'Title:' , data . title )
4861
62+ data . favicon =
63+ 'https://www.google.com/s2/favicons?domain=' + window . location . href
64+ console . log ( 'Favicon:' , data . favicon )
65+ }
66+
67+ function getPageTitle ( ) : Promise < string > {
68+ return new Promise ( async ( resolve , reject ) => {
69+ let title : string | undefined = document . title
70+
71+ if ( title ) {
72+ resolve ( title )
73+ } else {
74+ console . log ( 'Waiting for <title> to be added' )
75+
76+ // If no title after 15s, just return as empty
77+ const timer = setTimeout ( ( ) => {
78+ reject ( 'No title found' )
79+ } , 2500 )
80+
81+ // Wait for a <title> to be added
82+ const observer = new MutationObserver ( ( mutations ) => {
83+ mutations . forEach ( ( mutation ) => {
84+ if ( ! mutation . addedNodes ) return
85+
86+ for ( let i = 0 ; i < mutation . addedNodes . length ; i ++ ) {
87+ let node = mutation . addedNodes [ i ]
88+ if ( node . nodeName !== 'TITLE' ) continue
89+ // Only look for <title> node
90+ if ( node . nodeName === 'TITLE' ) {
91+ // Title set!
92+ title = document . title
93+
94+ // Cancel the timer
95+ clearTimeout ( timer )
96+
97+ // Kill the observer
98+ observer . disconnect ( )
99+
100+ // Return the final title
101+ resolve ( title )
102+ }
103+ }
104+ } )
105+ } )
106+
107+ // Watch the <head> for the <title> to be added
108+ observer . observe ( document . head , {
109+ childList : true ,
110+ subtree : true ,
111+ characterData : true ,
112+ } )
113+ }
114+ } )
115+ }
116+
117+ async function initiateBridge ( ) {
49118 // Listen for initial connection to the frame
50- ipcRenderer . once ( 'bridgeToFrame' , ( event , args ) => {
119+ ipcRenderer . on ( 'bridgeToFrame' , async ( event , args ) => {
51120 // We get the ID of the artboard we're connected to
52121 console . log ( 'Passed from parent:' , args )
53122 const { id, syncFns } = args
54123 state . id = id
55124
125+ // Get the page title and favicon
126+ await getPageData ( )
127+
56128 // Respond to the parent component
57129 ipcRenderer . sendToHost ( 'initiateBridge' , data )
58- document . removeEventListener ( 'DOMContentLoaded' , initiateBridge )
130+ // document.removeEventListener('DOMContentLoaded', initiateBridge)
59131
60132 // Run our functions
61133 startSync ( )
0 commit comments