-
Notifications
You must be signed in to change notification settings - Fork 276
Documentation: Fast Forward
Michał edited this page Feb 6, 2017
·
2 revisions
[Href]
page
start
startpage
paged
[Class]
next
paging-next
[Id]
next
[Text]
; Special ones
→=50
▶=50
›=50
»=49
; We don't want to fast-forward on chan's quotes!
>=10
; Afrikaans
volgende
volg
verder
; Bulgarian
напред
следва
следна
нататък
; Chinese (traditional and simplified)
下一
; Danish
næste
; Deutsch
nächste
weiter
vorwärts
naechste
vorwaerts
; Dutch
volgende
; English
next
; Esperanto
venonta
; Estonian
järgmine
; Finnish
seuraava
; French
suivant
prochaine
page suivante
page prochaine
suivants
suivantes
; Greek
επόμενη
; Icelandic
næst
; Italian
succ
; Japanese
次へ
次の
; Korean
다음
; Lithuanian
toliau
kitas
sekantis
; Norwegian
neste
; Polish
dalej
następna
następne
następny
więcej
; Portuguese
próximo
página seguinte
; Russian
следующая
дальше
вперед
; Spanish
siguiente
próxima
próximos
; Swedish
nästa
; Turkish
sonraki
(function(isSelectingTheBestLink, hrefTokens, classTokens, idTokens, textTokens)
{
const REL_SCORE = 9000;
const TEXT_SCORE = 100;
const ID_SCORE = 11;
const CLASS_SCORE = 10;
const HREF_SCORE = 1;
const THRESHOLD = 10;
const DEBUG = false;
function calculateScore(value, tokens, defaultScore)
{
var score = 0;
if (value)
{
for (var i = (tokens.length - 1); i >= 0; --i)
{
if (value.indexOf(tokens[i].value) != -1)
{
score += tokens[i].score || defaultScore;
}
}
}
return score;
}
function calculateScoreForValues(values, tokens, defaultScore)
{
var score = 0;
if (values && values.length > 0)
{
for (var i = (values.length - 1); i >= 0; --i)
{
score += calculateScore(values[i].toUpperCase(), tokens, defaultScore);
}
}
return score;
}
var links = document.querySelectorAll('a:not([href^="javascript:"]):not([href="#"])');
var scoredLinks = [];
if (DEBUG)
{
console.log('FastForward DEBUG: Checking', links.length, 'links');
}
for (var i = (links.length - 1); i >= 0; --i)
{
var score = 0;
if (links[i].rel && links[i].rel.indexOf('next') != -1)
{
score += REL_SCORE;
}
score += calculateScore([links[i].innerText, links[i].getAttribute('aria-label'), links[i].getAttribute('alt'), links[i].title].join(' ').toUpperCase(), textTokens, TEXT_SCORE);
score += calculateScore(links[i].id.toUpperCase(), idTokens, ID_SCORE);
score += calculateScoreForValues(links[i].classList, classTokens, CLASS_SCORE);
var url = links[i].pathname.split('/');
if (links[i].search)
{
links[i].search.substr(1).split('&').every(function(item)
{
url.push(item.split('=')[0]);
});
}
score += calculateScoreForValues(url, hrefTokens, HREF_SCORE);
//Is link worthy?
if (score > THRESHOLD)
{
if (isSelectingTheBestLink)
{
scoredLinks.push({score: score, link: links[i]});
}
else
{
if (DEBUG)
{
console.log('FastForward DEBUG: Found at least one link for FastForward. Score:', score, links[i].href);
}
return true;
}
}
}
if (scoredLinks.length > 0)
{
scoredLinks.sort(function(first, second)
{
return (second.score - first.score);
});
if (DEBUG)
{
for (var i = 0; i < scoredLinks.length; ++i)
{
console.log('FastForward DEBUG: ', scoredLinks[i].score, 'Url:', scoredLinks[i].link.outerHTML);
}
console.log('FastForward DEBUG: Choosing link with score', scoredLinks[0].score, scoredLinks[0].link.href);
}
var link = document.createElement('a');
link.href = scoredLinks[0].link.href;
return link.href;
}
if (DEBUG)
{
console.log('FastForward DEBUG: No candidate links found!');
}
return null;
})({isSelectingTheBestLink}, {hrefTokens}, {classTokens}, {idTokens}, {textTokens})