diff --git a/src/containers/OAuthWrapper.tsx b/src/containers/OAuthWrapper.tsx index 9fdc257..808950d 100644 --- a/src/containers/OAuthWrapper.tsx +++ b/src/containers/OAuthWrapper.tsx @@ -4,6 +4,7 @@ import * as React from 'react' import { parseURLSearch, run } from 'utils/general' import { useLoadedContext } from 'utils/hooks/useLoadedContext' import { useStateIO } from 'utils/hooks/useStateIO' +import { sanitizedLocation } from 'utils/URLHelper' import { SideBarStateContext } from './SideBarState' /** @@ -55,7 +56,7 @@ async function getAccessTokenWithCode(code: string) { window.history.replaceState( {}, 'removed search param', - window.location.pathname.replace(window.location.search, '?' + search.toString()), + sanitizedLocation.pathname.replace(window.location.search, '?' + search.toString()), ) return accessToken } diff --git a/src/platforms/GitHub/URLHelper.ts b/src/platforms/GitHub/URLHelper.ts index 9945ecf..a4c3644 100644 --- a/src/platforms/GitHub/URLHelper.ts +++ b/src/platforms/GitHub/URLHelper.ts @@ -1,9 +1,9 @@ import { raiseError } from 'analytics' +import { sanitizedLocation } from 'utils/URLHelper' export function parse(): Partial> & { path: string[] } { - const { pathname } = window.location const [ , // ignore content before the first '/' @@ -11,7 +11,7 @@ export function parse(): Partial & { path: string[] } { - const { pathname } = window.location const [ , // ignore content before the first '/' @@ -9,7 +9,7 @@ export function parse(): Partial & { path: string[] } { repoName, type, ...path // should be [...branchName.split('/'), ...filePath.split('/')] - ] = unescape(decodeURIComponent(pathname)).split('/') + ] = unescape(decodeURIComponent(sanitizedLocation.pathname)).split('/') return { userName, repoName, diff --git a/src/platforms/Gitee/URLHelper.ts b/src/platforms/Gitee/URLHelper.ts index e5c100f..37f4647 100644 --- a/src/platforms/Gitee/URLHelper.ts +++ b/src/platforms/Gitee/URLHelper.ts @@ -1,7 +1,7 @@ import { raiseError } from 'analytics' +import { sanitizedLocation } from 'utils/URLHelper' export function parse(): Partial & { path: string[] } { - const { pathname } = window.location const [ , // ignore content before the first '/' @@ -9,7 +9,7 @@ export function parse(): Partial & { path: string[] } { repoName, type, ...path // should be [...branchName.split('/'), ...filePath.split('/')] - ] = unescape(decodeURIComponent(pathname)).split('/') + ] = unescape(decodeURIComponent(sanitizedLocation.pathname)).split('/') return { userName, repoName, diff --git a/src/utils/URLHelper.ts b/src/utils/URLHelper.ts new file mode 100644 index 0000000..bdd74d8 --- /dev/null +++ b/src/utils/URLHelper.ts @@ -0,0 +1,17 @@ +export const sanitizedLocation = { + get href() { + const { href, pathname } = window.location + const hasDupSlashes = pathname.includes('//') + const needSanitize = hasDupSlashes + if (!needSanitize) return href + + const url = new URL(href) + url.pathname = sanitizedLocation.pathname + return url.href + }, + get pathname() { + const { pathname } = window.location + // remove duplicated slashes + return pathname.replace(/^\/\/+/g, '/') + }, +}