|
20 | 20 | // Single Page Apps for GitHub Pages |
21 | 21 | // MIT License |
22 | 22 | // https://github.com/rafgraph/spa-github-pages |
23 | | - // This script checks to see if a redirect is present in the query string, |
24 | | - // converts it back into the correct url and adds it to the |
25 | | - // browser's history using window.history.replaceState(...), |
26 | | - // which won't cause the browser to attempt to load the new url. |
27 | | - // When the single page app is loaded further down in this file, |
28 | | - // the correct url will be waiting in the browser's history for |
29 | | - // the single page app to route accordingly. |
30 | 23 | (function(l) { |
31 | | - if (l.search[1] === '/' ) { |
| 24 | + if (l.search[1] === '/') { |
32 | 25 | var decoded = l.search.slice(1).split('&').map(function(s) { |
33 | | - return s.replace(/~and~/g, '&') |
| 26 | + return s.replace(/~and~/g, '&'); |
34 | 27 | }).join('?'); |
35 | | - window.history.replaceState(null, null, |
36 | | - l.pathname.slice(0, -1) + decoded + l.hash |
37 | | - ); |
| 28 | + |
| 29 | + // Construct the new URL using the current origin |
| 30 | + var newUrl = l.origin + l.pathname.slice(0, -1) + decoded + l.hash; |
| 31 | + |
| 32 | + try { |
| 33 | + var newUrlObj = new URL(newUrl); |
| 34 | + // Only allow update if the new URL matches the current origin |
| 35 | + if (newUrlObj.origin === window.location.origin) { |
| 36 | + window.history.replaceState(null, null, newUrlObj.href); |
| 37 | + } |
| 38 | + } catch (e) { |
| 39 | + console.warn('Invalid redirect URL', e); |
| 40 | + } |
38 | 41 | } |
39 | | - }(window.location)) |
| 42 | + }(window.location)); |
40 | 43 | </script> |
| 44 | + |
41 | 45 | <!-- End Single Page Apps for GitHub Pages --> |
42 | 46 | </head> |
43 | 47 |
|
|
0 commit comments