-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalready-seen.js
87 lines (75 loc) · 2.31 KB
/
already-seen.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
var jstiller = jstiller || {};
jstiller.modules = jstiller.modules || {};
jstiller.modules.alreadySeen = (function (dependency) {
var estimatedSettings,
defaultSettings = {
rules: [
{
location: '',
exceptions: []
}
],
prefix: 'visited-',
data: 'visited'
};
/**
* sets custom settings
*
* @param {object} deliveredSettings
* @return {object}
*/
function settings(deliveredSettings) {
estimatedSettings = dependency.window.Object.assign(defaultSettings, deliveredSettings);
return this;
}
/**
* tracks the delivered url
*
* @param {string} deliveredClient URL
* @return {object}
*/
function track(deliveredClient) {
estimatedSettings.rules.forEach(function (receivedTrack) {
var estimatedPermission = true;
if (deliveredClient.substr(0, receivedTrack.location.length) === receivedTrack.location) {
if (typeof receivedTrack.exceptions === 'object' && !dependency.window.Array.isArray(receivedTrack.exceptions)) {
receivedTrack.exceptions = [receivedTrack.exceptions];
}
receivedTrack.exceptions.forEach(function (receivedException) {
if (deliveredClient.substr(0, receivedException.location.length) === receivedException.location) {
estimatedPermission = false;
}
});
if (estimatedPermission === true) {
dependency.window.localStorage.setItem(estimatedSettings.prefix + dependency.window.location.pathname, 'true');
}
}
});
return this;
};
/**
*
* @param {string} deliveredLinks
* @return {object}
*/
function check(deliveredLinks) {
if (typeof deliveredLinks === 'object' && !dependency.window.Array.isArray(deliveredLinks)) {
deliveredLinks = dependency.window.Object.keys(deliveredLinks).map(function (key) {
return deliveredLinks[key]
});
}
deliveredLinks.forEach(function (receivedLink) {
if (receivedLink.host == dependency.window.location.host && dependency.window.localStorage.getItem(estimatedSettings.prefix + receivedLink.pathname)) {
receivedLink.dataset[estimatedSettings.data] = true;
}
});
return this;
};
return {
settings: settings,
track: track,
check: check,
};
}({
window: window,
}));