@@ -4,8 +4,9 @@ import { uniqueId } from 'lodash'
4
4
import styles from '../style/components/IFrame.module.css'
5
5
interface Props {
6
6
src : string
7
- onPageChanged : ( page : string , hash : string ) => void
7
+ onPageChanged : ( page : string , hash : string , title ?: string ) => void
8
8
onHashChanged : ( hash : string ) => void
9
+ onTitleChanged : ( title : string ) => void
9
10
onNotFound : ( ) => void
10
11
}
11
12
@@ -69,6 +70,10 @@ export default function IFrame(props: Props): JSX.Element {
69
70
'hashchange' ,
70
71
hashChangeEventListener
71
72
)
73
+ iFrameRef . current . contentWindow ?. addEventListener (
74
+ 'titlechange' ,
75
+ titleChangeEventListener
76
+ )
72
77
73
78
const parts = url . split ( '/doc/' ) . slice ( 1 ) . join ( '/doc/' ) . split ( '/' )
74
79
const urlPageAndHash = parts . slice ( 2 ) . join ( '/' )
@@ -77,8 +82,9 @@ export default function IFrame(props: Props): JSX.Element {
77
82
: urlPageAndHash . length
78
83
const urlPage = urlPageAndHash . slice ( 0 , hashIndex )
79
84
const urlHash = urlPageAndHash . slice ( hashIndex )
85
+ const title = iFrameRef . current ?. contentDocument ?. title
80
86
81
- props . onPageChanged ( urlPage , urlHash )
87
+ props . onPageChanged ( urlPage , urlHash , title )
82
88
}
83
89
84
90
const hashChangeEventListener = ( ) : void => {
@@ -102,6 +108,20 @@ export default function IFrame(props: Props): JSX.Element {
102
108
props . onHashChanged ( hash )
103
109
}
104
110
111
+ const titleChangeEventListener = ( ) : void => {
112
+ if ( iFrameRef . current == null ) {
113
+ console . error ( 'titleChangeEvent from iframe but iFrameRef is null' )
114
+ return
115
+ }
116
+
117
+ const title = iFrameRef . current ?. contentDocument ?. title
118
+ if ( title == null ) {
119
+ return
120
+ }
121
+
122
+ props . onTitleChanged ( title )
123
+ }
124
+
105
125
return (
106
126
< iframe
107
127
ref = { iFrameRef }
0 commit comments