aboutsummaryrefslogtreecommitdiff
path: root/src/content
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2017-11-30 19:48:46 +0900
committerGitHub <noreply@github.com>2017-11-30 19:48:46 +0900
commitb18674de73aa57d3727c62e05e8dac82a568fe7d (patch)
treeb7f97ba9a96b6f143975ff05ce5abb36c0b34574 /src/content
parentd5517345a81831135aaad2ebfac2ebb1a427b55d (diff)
parentee3cd3faabbe64265746ef39d419f63db24d11ba (diff)
Merge pull request #240 from chocolateboy/pagination-tweaks
Pagination tweaks and fixes
Diffstat (limited to 'src/content')
-rw-r--r--src/content/navigates.js49
1 files changed, 27 insertions, 22 deletions
diff --git a/src/content/navigates.js b/src/content/navigates.js
index 3e12a6f..c9baa30 100644
--- a/src/content/navigates.js
+++ b/src/content/navigates.js
@@ -1,18 +1,18 @@
-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) => {
- const links = win.document.getElementsByTagName('a');
- return Array.prototype.find.call(links, (link) => {
- return patterns.some(ptn => ptn.test(link.textContent));
- });
+const REL_PATTERN = {
+ prev: /^(?:prev(?:ious)?|older)\b|\u2039|\u2190|\xab|\u226a|<</i,
+ next: /^(?:next|newer)\b|\u203a|\u2192|\xbb|\u226b|>>/i,
+};
+
+// Return the last element in the document matching the supplied selector
+// and the optional filter, or null if there are no matches.
+const selectLast = (win, selector, filter) => {
+ let nodes = win.document.querySelectorAll(selector);
+
+ if (filter) {
+ nodes = Array.from(nodes).filter(filter);
+ }
+
+ return nodes.length ? nodes[nodes.length - 1] : null;
};
const historyPrev = (win) => {
@@ -23,16 +23,21 @@ const historyNext = (win) => {
win.history.forward();
};
-const linkCommon = (win, rel, patterns) => {
- let link = win.document.querySelector(`link[rel~=${rel}][href]`);
+// Code common to linkPrev and linkNext which navigates to the specified page.
+const linkRel = (win, rel) => {
+ let link = selectLast(win, `link[rel~=${rel}][href]`);
if (link) {
- win.location = link.getAttribute('href');
+ win.location = link.href;
return;
}
- link = win.document.querySelector(`a[rel~=${rel}]`) ||
- findLinkByPatterns(win, patterns);
+ const pattern = REL_PATTERN[rel];
+
+ link = selectLast(win, `a[rel~=${rel}][href]`) ||
+ // `innerText` is much slower than `textContent`, but produces much better
+ // (i.e. less unexpected) results
+ selectLast(win, 'a[href]', lnk => pattern.test(lnk.innerText));
if (link) {
link.click();
@@ -40,11 +45,11 @@ const linkCommon = (win, rel, patterns) => {
};
const linkPrev = (win) => {
- linkCommon(win, 'prev', PREV_LINK_PATTERNS);
+ linkRel(win, 'prev');
};
const linkNext = (win) => {
- linkCommon(win, 'next', NEXT_LINK_PATTERNS);
+ linkRel(win, 'next');
};
const parent = (win) => {