improve #linkPrev and #linkNext:

- add support for <link> elements
- match if there's more than one rel e.g. <a rel="nofollow next">...</a>
- more tests
This commit is contained in:
chocolateboy 2017-11-17 19:02:13 +00:00
parent 7e506f5f21
commit 4a44679321
2 changed files with 136 additions and 51 deletions

View file

@ -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;