@@ -58,16 +58,23 @@ if (json) {
5858 } ) ;
5959 } ) ;
6060
61- const setHash = debounce ( hash => {
62- if ( hash ) {
63- window . location . hash = hash ;
64- } else if ( window . location . hash ) {
61+ let iframeHash = null ;
62+
63+ const setHash = ( hash , replace ) => {
64+ if ( hash && window . location . hash !== hash ) {
65+ if ( replace ) {
66+ history . replaceState ( '' , document . title , window . location . pathname + window . location . search + hash ) ;
67+ } else {
68+ window . location . hash = hash ;
69+ }
70+ } else if ( ! hash && window . location . hash ) {
6571 history . pushState ( '' , document . title , window . location . pathname + window . location . search ) ;
6672 }
67- } , 300 ) ;
73+ iframeHash = hash ;
74+ } ;
6875
6976 const onMessage = event => {
70- setHash ( event . data . hash ) ;
77+ setHash ( event . data . hash , event . data . replace ) ;
7178
7279 if ( event . data && event . data . openSettings ) {
7380 if ( chrome . runtime . openOptionsPage ) {
@@ -81,9 +88,11 @@ if (json) {
8188 window . addEventListener ( 'message' , onMessage ) ;
8289
8390 const onHashChange = ( ) => {
84- iframe . contentWindow . postMessage ( {
85- hash : window . location . hash
86- } , '*' ) ;
91+ if ( iframeHash !== null && iframeHash !== window . location . hash ) {
92+ iframe . contentWindow . postMessage ( {
93+ hash : window . location . hash
94+ } , '*' ) ;
95+ }
8796 } ;
8897
8998 window . addEventListener ( 'hashchange' , onHashChange ) ;
@@ -93,26 +102,3 @@ if (json) {
93102 window . removeEventListener ( 'hashchange' , onHashChange ) ;
94103 } ) ;
95104}
96-
97- /**
98- * Debounce
99- * @param {Function } func
100- * @param {number } wait
101- * @returns {Function }
102- */
103- function debounce ( func , wait ) {
104- let timer = null ;
105-
106- return function ( ...args ) {
107- const onComplete = ( ) => {
108- func . apply ( this , args ) ;
109- timer = null ;
110- } ;
111-
112- if ( timer ) {
113- clearTimeout ( timer ) ;
114- }
115-
116- timer = setTimeout ( onComplete , wait ) ;
117- } ;
118- }
0 commit comments