aboutsummaryrefslogtreecommitdiff
path: root/src/background/operators/impls/NavigateParentOperator.ts
blob: 652cfb8c648208c6db04d90bb7202f30c498e1a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);
  }
}