diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-09-17 13:27:35 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-09-17 13:27:35 +0900 |
commit | 78233b25b76df55d519f1d9082267aba876b4835 (patch) | |
tree | 05cea028a87f067a83d02638f187b92b3ec6ecd9 /src | |
parent | ae317113e76b593949385102e6bea16e50c16ce5 (diff) | |
parent | ac8f7e65dc90327e05fb30fd5b20d56c3799f3d8 (diff) |
Merge branch 'pagenation-and-navigation'
Diffstat (limited to 'src')
-rw-r--r-- | src/background/keys.js | 8 | ||||
-rw-r--r-- | src/content/histories.js | 8 | ||||
-rw-r--r-- | src/content/index.js | 20 | ||||
-rw-r--r-- | src/content/navigates.js | 70 | ||||
-rw-r--r-- | src/operations/index.js | 8 |
5 files changed, 96 insertions, 18 deletions
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, + /</, /\u2039/, /\u2190/, /\xab/, /\u226a/, /<</ +]; +const NEXT_LINK_PATTERNS = [ + /\bnext\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', |