diff options
Diffstat (limited to 'src/background/operators/impls/NavigateParentOperator.ts')
-rw-r--r-- | src/background/operators/impls/NavigateParentOperator.ts | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/background/operators/impls/NavigateParentOperator.ts b/src/background/operators/impls/NavigateParentOperator.ts new file mode 100644 index 0000000..652cfb8 --- /dev/null +++ b/src/background/operators/impls/NavigateParentOperator.ts @@ -0,0 +1,25 @@ +import Operator from "../Operator"; +import TabPresenter from "../../presenters/TabPresenter"; + +export default class NavigateParentOperator implements Operator { + constructor(private readonly tabPresenter: TabPresenter) {} + + async run(): Promise<void> { + const tab = await this.tabPresenter.getCurrent(); + const url = new URL(tab.url!); + if (url.hash.length > 0) { + url.hash = ""; + } else if (url.search.length > 0) { + url.search = ""; + } else { + const basenamePattern = /\/[^/]+$/; + const lastDirPattern = /\/[^/]+\/$/; + if (basenamePattern.test(url.pathname)) { + url.pathname = url.pathname.replace(basenamePattern, "/"); + } else if (lastDirPattern.test(url.pathname)) { + url.pathname = url.pathname.replace(lastDirPattern, "/"); + } + } + await this.tabPresenter.open(url.href); + } +} |