|
4 | 4 | <head> |
5 | 5 | <meta charset="utf-8"> |
6 | 6 | <title>Single Page Apps for GitHub Pages</title> |
7 | | - <script type="text/javascript"> |
8 | | - // Single Page Apps for GitHub Pages |
9 | | - // MIT License |
10 | | - // https://github.com/rafgraph/spa-github-pages |
11 | | - // This script checks to see if a redirect is present in the query string, |
12 | | - // converts it back into the correct url and adds it to the |
13 | | - // browser's history using window.history.replaceState(...), |
14 | | - // which won't cause the browser to attempt to load the new url. |
15 | | - // When the single page app is loaded further down in this file, |
16 | | - // the correct url will be waiting in the browser's history for |
17 | | - // the single page app to route accordingly. |
18 | | - (function(l) { |
19 | | - if (l.search[1] === '/' ) { |
20 | | - var decoded = l.search.slice(1).split('&').map(function(s) { |
21 | | - return s.replace(/~and~/g, '&') |
22 | | - }).join('?'); |
23 | | - window.history.replaceState(null, null, |
24 | | - l.pathname.slice(0, -1) + decoded + l.hash |
25 | | - ); |
26 | | - } |
27 | | - }(window.location)) |
28 | | - </script> |
| 7 | + <script type="text/javascript"> |
| 8 | + // Single Page Apps for GitHub Pages |
| 9 | + // MIT License |
| 10 | + // https://github.com/rafgraph/spa-github-pages |
| 11 | + (function(l) { |
| 12 | + if (l.search[1] === '/') { |
| 13 | + var decoded = l.search.slice(1).split('&').map(function(s) { |
| 14 | + return s.replace(/~and~/g, '&'); |
| 15 | + }).join('?'); |
| 16 | + |
| 17 | + // Construct the new URL using the current origin |
| 18 | + var newUrl = l.origin + l.pathname.slice(0, -1) + decoded + l.hash; |
| 19 | + |
| 20 | + try { |
| 21 | + var newUrlObj = new URL(newUrl); |
| 22 | + // Only allow update if the new URL matches the current origin |
| 23 | + if (newUrlObj.origin === window.location.origin) { |
| 24 | + window.history.replaceState(null, null, newUrlObj.href); |
| 25 | + } |
| 26 | + } catch (e) { |
| 27 | + console.warn('Invalid redirect URL', e); |
| 28 | + } |
| 29 | + } |
| 30 | + }(window.location)); |
| 31 | + </script> |
| 32 | + |
29 | 33 | </head> |
30 | 34 | <body> |
31 | 35 | <script type="text/javascript"> |
|
0 commit comments