diff --git a/README.md b/README.md index 84cf2a2..bc17cf6 100644 --- a/README.md +++ b/README.md @@ -38,8 +38,9 @@ Firefox by WebExtensions API. - [ ] find a keyword in the page - [ ] navigations - [ ] yank/paste page - - [ ] pagenation - - [ ] open parent page + - [x] pagenation + - [x] open parent page + - [x] open root 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 9121e0f..34483a0 100644 --- a/src/background/keys.js +++ b/src/background/keys.js @@ -28,8 +28,12 @@ const defaultKeymap = { 'zz': { type: operations.ZOOM_NEUTRAL }, 'f': { type: operations.FOLLOW_START, newTab: false }, 'F': { type: operations.FOLLOW_START, newTab: true }, - 'H': { type: operations.HISTORY_PREV }, - 'L': { type: operations.HISTORY_NEXT }, + 'H': { type: operations.NAVIGATE_HISTORY_PREV }, + 'L': { type: operations.NAVIGATE_HISTORY_NEXT }, + '[[': { type: operations.NAVIGATE_LINK_PREV }, + ']]': { type: operations.NAVIGATE_LINK_NEXT }, + 'gu': { type: operations.NAVIGATE_PARENT }, + 'gU': { type: operations.NAVIGATE_ROOT }, }; const asKeymapChars = (keys) => { diff --git a/src/content/histories.js b/src/content/histories.js deleted file mode 100644 index 9c5665d..0000000 --- a/src/content/histories.js +++ /dev/null @@ -1,8 +0,0 @@ -const prev = (win) => { - win.history.back(); -}; -const next = (win) => { - win.history.forward(); -}; - -export { prev, next }; diff --git a/src/content/index.js b/src/content/index.js index 91f5420..a9ccd63 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,7 +1,7 @@ import '../console/console-frame.scss'; import * as consoleFrames from '../console/frames'; import * as scrolls from '../content/scrolls'; -import * as histories from '../content/histories'; +import * as navigates from '../content/navigates'; import Follow from '../content/follow'; import operations from '../operations'; import messages from '../messages'; @@ -15,7 +15,7 @@ window.addEventListener('keypress', (e) => { browser.runtime.sendMessage({ type: messages.KEYDOWN, code: e.which, - ctrl: e.ctrl + ctrl: e.ctrlKey }); }); @@ -35,10 +35,18 @@ const execOperation = (operation) => { return scrolls.scrollRight(window); case operations.FOLLOW_START: return new Follow(window.document, operation.newTab); - case operations.HISTORY_PREV: - return histories.prev(window); - case operations.HISTORY_NEXT: - return histories.next(window); + case operations.NAVIGATE_HISTORY_PREV: + return navigates.historyPrev(window); + case operations.NAVIGATE_HISTORY_NEXT: + return navigates.historyNext(window); + case operations.NAVIGATE_LINK_PREV: + return navigates.linkPrev(window); + case operations.NAVIGATE_LINK_NEXT: + return navigates.linkNext(window); + case operations.NAVIGATE_PARENT: + return navigates.parent(window); + case operations.NAVIGATE_ROOT: + return navigates.root(window); } }; diff --git a/src/content/navigates.js b/src/content/navigates.js new file mode 100644 index 0000000..64e5fc0 --- /dev/null +++ b/src/content/navigates.js @@ -0,0 +1,70 @@ +const PREV_LINK_PATTERNS = [ + /\bprev\b/i, /\bprevious\b/i, /\bback\b/i, + //, /\u203a/, /\u2192/, /\xbb/, /\u226b/, />>/ +]; + +const findLinkByPatterns = (win, patterns) => { + let links = win.document.getElementsByTagName('a'); + return Array.prototype.find.call(links, (link) => { + return patterns.some(ptn => ptn.test(link.textContent)); + }); +}; + +const historyPrev = (win) => { + win.history.back(); +}; + +const historyNext = (win) => { + win.history.forward(); +}; + +const linkPrev = (win) => { + let link = win.document.querySelector('a[rel=prev]'); + if (link) { + return link.click(); + } + link = findLinkByPatterns(win, PREV_LINK_PATTERNS); + if (link) { + link.click(); + } +}; + +const linkNext = (win) => { + let link = win.document.querySelector('a[rel=next]'); + if (link) { + return link.click(); + } + link = findLinkByPatterns(win, NEXT_LINK_PATTERNS); + if (link) { + link.click(); + } +}; + +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, '/'); + } +}; + +const root = (win) => { + win.location = win.location.origin; +}; + +export { historyPrev, historyNext, linkPrev, linkNext, parent, root }; diff --git a/src/operations/index.js b/src/operations/index.js index c2db007..a40123a 100644 --- a/src/operations/index.js +++ b/src/operations/index.js @@ -11,8 +11,12 @@ export default { SCROLL_LEFT: 'scroll.left', SCROLL_RIGHT: 'scroll.right', FOLLOW_START: 'follow.start', - HISTORY_PREV: 'history.prev', - HISTORY_NEXT: 'history.next', + NAVIGATE_HISTORY_PREV: 'navigate.history.prev', + NAVIGATE_HISTORY_NEXT: 'navigate.history.next', + NAVIGATE_LINK_PREV: 'navigate.link.prev', + NAVIGATE_LINK_NEXT: 'navigate.link.next', + NAVIGATE_PARENT: 'navigate.parent', + NAVIGATE_ROOT: 'navigate.root', // Background TABS_CLOSE: 'tabs.close', diff --git a/test/content/navigates.test.js b/test/content/navigates.test.js new file mode 100644 index 0000000..cf20435 --- /dev/null +++ b/test/content/navigates.test.js @@ -0,0 +1,56 @@ +import { expect } from "chai"; +import * as navigates from '../../src/content/navigates'; + +describe('navigates module', () => { + describe('#linkPrev', () => { + it('clicks prev link by text content', (done) => { + document.body.innerHTML = 'xprevx go to prev'; + navigates.linkPrev(window); + setTimeout(() => { + expect(document.location.hash).to.equal('#prev'); + done(); + }, 0); + }); + + it('clicks a[rel=prev] element preferentially', (done) => { + document.body.innerHTML = 'prev '; + navigates.linkPrev(window); + setTimeout(() => { + expect(document.location.hash).to.equal('#prev'); + done(); + }, 0); + }); + }); + + + describe('#linkNext', () => { + it('clicks next link by text content', (done) => { + document.body.innerHTML = 'xnextx go to next'; + navigates.linkNext(window); + setTimeout(() => { + expect(document.location.hash).to.equal('#next'); + done(); + }, 0); + }); + + it('clicks a[rel=next] element preferentially', (done) => { + document.body.innerHTML = 'next '; + navigates.linkNext(window); + setTimeout(() => { + expect(document.location.hash).to.equal('#next'); + done(); + }, 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; + }); + }); +}); + +