aboutsummaryrefslogtreecommitdiff
path: root/src/content
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2017-09-17 13:27:35 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2017-09-17 13:27:35 +0900
commit78233b25b76df55d519f1d9082267aba876b4835 (patch)
tree05cea028a87f067a83d02638f187b92b3ec6ecd9 /src/content
parentae317113e76b593949385102e6bea16e50c16ce5 (diff)
parentac8f7e65dc90327e05fb30fd5b20d56c3799f3d8 (diff)
Merge branch 'pagenation-and-navigation'
Diffstat (limited to 'src/content')
-rw-r--r--src/content/histories.js8
-rw-r--r--src/content/index.js20
-rw-r--r--src/content/navigates.js70
3 files changed, 84 insertions, 14 deletions
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 };