diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-11-18 12:40:49 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-18 12:40:49 +0900 |
commit | af3a2cbf30f6aa6bc8a1d0d338f8660ae9f96ea9 (patch) | |
tree | f4212af476f38a08f99ec20aed596159690eda61 /src | |
parent | 7e506f5f212ce57362bb644c2c2d7a9f763282cb (diff) | |
parent | 4a446793212b97dc16f87c99f99def6fb5fcd1d2 (diff) |
Merge pull request #196 from chocolateboy/rel-improvements
improve #linkPrev and #linkNext
Diffstat (limited to 'src')
-rw-r--r-- | src/content/navigates.js | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/src/content/navigates.js b/src/content/navigates.js index 64e5fc0..3e12a6f 100644 --- a/src/content/navigates.js +++ b/src/content/navigates.js @@ -2,13 +2,14 @@ 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'); + const links = win.document.getElementsByTagName('a'); return Array.prototype.find.call(links, (link) => { return patterns.some(ptn => ptn.test(link.textContent)); }); @@ -22,30 +23,32 @@ const historyNext = (win) => { win.history.forward(); }; -const linkPrev = (win) => { - let link = win.document.querySelector('a[rel=prev]'); +const linkCommon = (win, rel, patterns) => { + let link = win.document.querySelector(`link[rel~=${rel}][href]`); + if (link) { - return link.click(); + win.location = link.getAttribute('href'); + return; } - link = findLinkByPatterns(win, PREV_LINK_PATTERNS); + + link = win.document.querySelector(`a[rel~=${rel}]`) || + findLinkByPatterns(win, patterns); + if (link) { link.click(); } }; +const linkPrev = (win) => { + linkCommon(win, 'prev', PREV_LINK_PATTERNS); +}; + 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(); - } + linkCommon(win, 'next', NEXT_LINK_PATTERNS); }; const parent = (win) => { - let loc = win.location; + const loc = win.location; if (loc.hash !== '') { loc.hash = ''; return; |