From e9863299abf9498c67660e8a97c70ddb090baffe Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 17 Sep 2017 12:52:24 +0900 Subject: implement go-parent command --- src/content/index.js | 2 ++ src/content/navigates.js | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) (limited to 'src/content') diff --git a/src/content/index.js b/src/content/index.js index 9fd46be..4751cde 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -43,6 +43,8 @@ const execOperation = (operation) => { return navigates.linkPrev(window); case operations.NAVIGATE_LINK_NEXT: return navigates.linkNext(window); + case operations.NAVIGATE_PARENT: + return navigates.parent(window); } }; diff --git a/src/content/navigates.js b/src/content/navigates.js index b68052d..692b7be 100644 --- a/src/content/navigates.js +++ b/src/content/navigates.js @@ -44,4 +44,23 @@ const linkNext = (win) => { } }; -export { historyPrev, historyNext, linkPrev, linkNext }; +const parent = (win) => { + let loc = win.location; + if (loc.hash !== '') { + loc.hash = ''; + return; + } else if (loc.search !== '') { + loc.search = ''; + return; + } + + const basenamePattern = /\/[^/]+$/; + const lastDirPattern = /\/[^/]+\/$/; + if (basenamePattern.test(loc.pathname)) { + loc.pathname = loc.pathname.replace(basenamePattern, '/'); + } else if (lastDirPattern.test(loc.pathname)) { + loc.pathname = loc.pathname.replace(lastDirPattern, '/'); + } +}; + +export { historyPrev, historyNext, linkPrev, linkNext, parent }; -- cgit v1.2.3