-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
132 lines (108 loc) · 3.06 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var fs = require('fs')
, url = require('url')
, querystring = require('querystring')
, yaml = require('js-yaml')
, path = require('path')
;
module.exports = Referer
var dataFile = fs.readFileSync(path.join(__dirname, 'data', 'referers.yml'))
var REFERERS = loadReferers(yaml.load(dataFile.toString()))
function loadReferers (source) {
var referers_dict = {}
for(var medium in source) {
var conf_list = source[medium]
for(var referer_name in conf_list) {
var config = conf_list[referer_name]
var params = null
if(config.parameters) {
params = config.parameters.map(function(p) { return p.toLowerCase() })
}
config.domains.forEach(function(domain){
referers_dict[domain] = {
'name': referer_name,
'medium': medium
}
if(params){
referers_dict[domain]['params'] = params
}
})
}
}
return referers_dict
}
function Referer (referer_url, current_url, referers) {
this.known = false
this.referer = null
this.medium = 'unknown'
this.search_parameter = null
this.search_term = null
this.referers = referers || REFERERS
var ref_uri = url.parse(referer_url)
var ref_host = ref_uri.hostname
this.known = Boolean(~['http:', 'https:'].indexOf(ref_uri.protocol))
this.uri = ref_uri
if(!this.known) return
if(current_url){
var curr_uri = url.parse(current_url)
var curr_host = curr_uri.hostname
if(curr_host == ref_host) {
this.medium = 'internal'
return
}
}
var referer = this._lookup_referer(ref_host, ref_uri.pathname, true)
if(!referer){
referer = this._lookup_referer(ref_host, ref_uri.pathname, false)
if(!referer){
this.medium = 'unknown'
return
}
}
this.referer = referer['name']
this.medium = referer['medium']
if(referer['medium'] == 'search') {
if(!referer['params']) return
var pqs = querystring.parse(ref_uri.query)
for(var param in pqs) {
var val = pqs[param]
if(referer['params'].indexOf(param.toLowerCase()) !== -1) {
this.search_parameter = param
this.search_term = val
}
}
}
}
Referer.prototype._lookup_referer = function(ref_host, ref_path, include_path) {
// console.log(ref_host, ref_path, include_path)
var referer = null
if(include_path)
referer = this.referers[ref_host + ref_path]
else
referer = this.referers[ref_host]
if(!referer) {
if(include_path){
var path_parts = ref_path.split('/')
if(path_parts.length > 1) {
try {
referer = this.referers[ref_host + '/' + path_parts[1]]
} catch (e) {
}
}
}
}
if(!referer) {
try{
var idx = ref_host.indexOf('.')
if(idx === -1) return null
var slicedHost = ref_host.slice(idx + 1)
return this._lookup_referer(
slicedHost,
ref_path, include_path
)
} catch (e) {
return null
}
} else return referer
};
// var r = new Referer("http://www.google.com/search?q=gateway+oracle+cards+denise+linn&hl=en&client=safari")
// console.log(r.uri)