/
index.js
61 lines (54 loc) · 1.63 KB
/
index.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
/**
* prefetcher will automate prefecthing of hrefs
* on a specified target class
*
* @author rabidmachine9 (<https://github.com/rabidmachine9>)
* @see <http://rabidmachine9.github.io/prefetcher/>
**/
module.exports = prefetcher;
/**
*@desc gives back href links with specified class name
*@param string className the name of our target html class
*@return array - all href values of target class
**/
function hrefLinks(className){
elementsToPrefetch = document.getElementsByClassName(className);
var hrefs = [];
Array.prototype.forEach.call(elementsToPrefetch, function(el){
hrefs.push(el.getAttribute('href'));
});
return unique(hrefs);
}
/**
*@desc prefetches-prerenders target links by inserting them to <head>
*@param string className the name of target html class
*
**/
function prefetcher(className){
var hrefs = hrefLinks(className);
Array.prototype.forEach.call(hrefs, function(el){
var prefetchLink = document.createElement('link');
var head = document.getElementsByTagName('head').item(0);
prefetchLink.setAttribute('rel', 'prerender prefetch');
prefetchLink.setAttribute('href', el);
head.appendChild(prefetchLink);
});
}
/**
*@desc remove duplicate values from an array
*@param array someArray the array to be filtered
*@return array the result array without duplicates
**/
function unique(someArray){
var theArray = someArray;
var arrayLength = theArray.length;
var uniqueArray = [];
for(var i=0;i<arrayLength;i++){
var value = someArray.shift();
if(someArray.hasOwnProperty(value)){
continue;
}
uniqueArray.push(value);
}
return uniqueArray;
}