This repository has been archived by the owner on Jul 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
serpcounter.js
106 lines (83 loc) · 3.57 KB
/
serpcounter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function main() {
// Get SERPs
var elements = document.querySelectorAll('#search .yuRUbf');
var results = [...elements].filter(element => !element.closest(".ULSxyf")); // remove featured snippet box results
// Calculate results per page
var resultStats = document.querySelector('#result-stats');
if (resultStats !== null) {
var resultStats = resultStats.innerHTML;
// Regex to match (or single space) surrounded by digits
// Replaces thousand seperator space with a .
var resultStats = resultStats.replace(/(?<=[0-9]{1,3})( |\s{1})(?=[0-9]{1,3})/g, '.');
// Regex to get current page from #resultStats
// Get integers from two string for
// About 22.600.000 results (0,43 seconds) mats:
//
// (Two matches == first page)
//
// Page 2 of about 22.600.000 results (0,50 seconds)
// (First match is the _current_ page)
//
// Works for most known locales
//
//
var regExMatch = resultStats.match(/[0-9]+(\.|,|’)?[0-9]?(\.|,|’)?[0-9]?(\.|,|’)?[0-9]?(\.|,|’)?[0-9]?(\.|,|’)?[0-9]?(\.|,|’)?[0-9]?(\.|,|’)?[0-9]?(\.|,|’)?[0-9]?(\.|,|’)?[0-9]?/g);
// 2+ matches = first match is page number
var currentPage = 1;
if (typeof regExMatch[2] !== 'undefined') {
var currentPage = parseInt(regExMatch[0]);
}
var resultsPerPage = results.length;
} else {
// defaults for queries without resultsPerPage
var currentPage = 1;
var resultsPerPage = 10;
}
// Reset localstorage on page 1 for new searches
if (currentPage == 1) {
localStorage.removeItem('countLastPage');
localStorage.removeItem('lastPage');
}
// countActual = index. Starts at 0, resets every page
// countDisplay = Human friendly, stored in localStorage
var countDisplay = 1;
for (var countActual = 0; countActual < results.length; countActual++, countDisplay++) {
// Skip if URL is invisible (PAA box, other search features)
var height = window.getComputedStyle(results[countActual].querySelector('a h3')).height;
if (height == "auto") { var height = 0; }
if (height < 20) {
countDisplay--;
continue;
}
if (currentPage == 1) {
var count = countDisplay;
} else if (parseInt(localStorage.getItem('countLastPage')) !== null && (parseInt(localStorage.getItem('lastPage')) + 1) == currentPage) {
var count = (countDisplay + parseInt(localStorage.getItem('countLastPage')));
} else {
// Fallback for when localStorage is not set (no chronological navigation)
var count = (countActual + (currentPage * resultsPerPage)) - (resultsPerPage-1);
}
counter = document.createElement('div');
counter.className = 'js-counter';
counter.textContent = '#' + count;
results[countActual].append(counter);
}
localStorage.setItem('lastPage', currentPage);
localStorage.setItem('countLastPage', count);
return true;
}
// Listen to hash change
// window.onhashchange and similar functions don't work with instant search
oldHash = location.hash;
var hashchange = setInterval(function() {
currentHash = location.hash;
if (currentHash != oldHash) {
// hash has changed, run function again
setTimeout(main, 750);
// set curenthash again
// and keep listening
oldHash = currentHash;
}
}, 750);
// run on page load
setTimeout(main, 750);