diff --git a/README.md b/README.md index c163901..d77291d 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Firefox by WebExtensions API. - [ ] navigations - [ ] yank/paste page - [x] pagenation - - [ ] open parent page + - [x] open parent page - [ ] hints - [x] open a link - [ ] open a link in new tab diff --git a/src/background/keys.js b/src/background/keys.js index 2549c8d..992b42d 100644 --- a/src/background/keys.js +++ b/src/background/keys.js @@ -32,6 +32,7 @@ const defaultKeymap = { 'L': { type: operations.NAVIGATE_HISTORY_NEXT }, '[[': { type: operations.NAVIGATE_LINK_PREV }, ']]': { type: operations.NAVIGATE_LINK_NEXT }, + 'gu': { type: operations.NAVIGATE_PARENT }, }; const asKeymapChars = (keys) => { 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 }; diff --git a/src/operations/index.js b/src/operations/index.js index bb19df8..493e866 100644 --- a/src/operations/index.js +++ b/src/operations/index.js @@ -15,6 +15,7 @@ export default { NAVIGATE_HISTORY_NEXT: 'navigate.history.next', NAVIGATE_LINK_PREV: 'navigate.link.prev', NAVIGATE_LINK_NEXT: 'navigate.link.next', + NAVIGATE_PARENT: 'navigate.parent', // Background TABS_CLOSE: 'tabs.close', diff --git a/test/content/navigates.test.js b/test/content/navigates.test.js index 21c2a23..cf20435 100644 --- a/test/content/navigates.test.js +++ b/test/content/navigates.test.js @@ -2,9 +2,6 @@ import { expect } from "chai"; import * as navigates from '../../src/content/navigates'; describe('navigates module', () => { - beforeEach(() => { - }); - describe('#linkPrev', () => { it('clicks prev link by text content', (done) => { document.body.innerHTML = 'xprevx go to prev'; @@ -45,6 +42,15 @@ describe('navigates module', () => { }, 0); }); }); + + describe('#parent', () => { + // NOTE: not able to test location + it('removes hash', () => { + window.location.hash = "#section-1"; + navigates.parent(window); + expect(document.location.hash).to.be.empty; + }); + }); });