-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoip-redirect.js
223 lines (205 loc) · 6.21 KB
/
geoip-redirect.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/****************************************************
* project: geoip detect and redirect *
* description: main script *
* author: [email protected] *
* url: github.com/horans/geoip-detect-and-redirect *
* update: 210311 *
****************************************************/
/* global $ _ */
/* eslint no-var: 0 */
// github.com/horans/get-current-path
function gcp (name) {
var cs = document.currentScript
var cl
if (cs) {
cl = cs.src
} else {
var ss = document.querySelectorAll('script[src' + (name ? ('*="' + name + '"') : '') + ']')
cs = ss[ss.length - 1]
cl = cs.getAttribute.length === undefined ? cs.getAttribute('src', -1) : cs.src
}
return cl.substring(0, cl.lastIndexOf('/') + 1)
}
// namespace
var gdnr = {
delay: 2,
path: gcp(),
current: {
lang: '',
navi: '',
geoip: '',
need: false
},
api: {
// geoip: 'https://i.wondershare.com/api/v1/geoip/country',
geoip: 'geoip-dummy.json', // change to your own api
code: 'geoip-code.json',
link: 'geoip-link.json',
l10n: 'geoip-l10n.json',
modal: 'geoip-modal.html',
old: 'geoip-modal-old.html'
},
language: {
default: 'english',
current: '',
redirect: ''
},
pool: [],
code: {},
link: {},
redirect: '',
l10n: {}
}
// cancel and remember
// redirect
$('body').on('click', '.gdnr-cancel', function () {
window.localStorage.setItem('gdnr-no', true)
})
// redirect
$('body').on('click', '.gdnr-confirm', function () {
window.location.href = gdnr.redirect
})
// show modal
$('body').on('gdnr.get.modal', function () {
setTimeout(function () {
$('.gdnr-modal').modal('show')
}, gdnr.delay * 1000)
})
// get modal
$('body').on('gdnr.need.redirect', function () {
// stackoverflow.com/questions/21369029/
$.get(gdnr.path + ($.fn.tooltip.Constructor.VERSION.charAt(0) === '4' ? gdnr.api.modal : gdnr.api.old), function (res) {
$('body').append(res)
$('.gdnr-current').text(_.capitalize(gdnr.language.current))
$('.gdnr-redirect').text(_.capitalize(gdnr.language.redirect))
var l = $('.gdnr-list')
var t = l.html()
_.each(gdnr.link[gdnr.current.navi], function (o, k) {
l.append(function () {
return t.replace(l.find('li:first').text(), '<a href="' + o + '">' + gdnr.code[k].label + '</a>')
})
})
$('.gdnr-more').tooltip({ container: 'body' })
$('body').trigger('gdnr.get.modal')
}).fail(function () {
window.console.log('[gdnr] cannot find modal')
})
})
// determine redirect
$('body').on('gdnr.check.navi', function () {
var p = ''
_.each(gdnr.code, function (o) { p += o.geoip + ',' })
gdnr.pool = _.uniq(_.split(_.trim(_.replace(_.replace(p, / /g, ''), /,,/g, ','), ','), ','))
var r = true
if (_.indexOf(gdnr.pool, gdnr.current.geoip) < 0) {
r = false
} else {
if ((gdnr.language.current !== gdnr.language.default) && (gdnr.language.current === gdnr.language.redirect)) r = false
}
if (gdnr.language.current === gdnr.language.default) {
if (_.indexOf(gdnr.pool, gdnr.current.geoip) < 0) r = false
} else {
if (gdnr.language.current === gdnr.language.redirect) r = false
}
if (r) {
gdnr.redirect = gdnr.link[gdnr.current.navi][gdnr.language.redirect]
$('body').trigger('gdnr.need.redirect')
console.log('[gdnr] redirect to ' + gdnr.language.redirect)
} else {
console.log('[gdnr] no need to redirect')
}
})
// check navigation
$('body').on('gdnr.check.lang', function () {
if (gdnr.link[gdnr.current.navi]) {
$('body').trigger('gdnr.check.navi')
} else {
window.console.log('[gdnr] navigation not included')
}
})
// check language
$('body').on('gdnr.get.link', function () {
gdnr.language.current = _.findKey(gdnr.code, function (o) { return o.code === gdnr.current.lang })
gdnr.language.redirect = _.findKey(gdnr.code, function (o) { return o.geoip.indexOf(gdnr.current.geoip) > -1 }) || gdnr.language.default
if (gdnr.language.current) {
$('body').trigger('gdnr.check.lang')
} else {
window.console.log('[gdnr] language not included')
}
})
// get link
$('body').on('gdnr.get.code', function () {
$.getJSON(gdnr.path + gdnr.api.link, function (res) {
gdnr.link = res
$('body').trigger('gdnr.get.link')
}).fail(function () {
window.console.log('[gdnr] cannot find link')
})
})
// get code
$('body').on('gdnr.get.geoip', function () {
$.getJSON(gdnr.path + gdnr.api.code, function (res) {
gdnr.code = res
$('body').trigger('gdnr.get.code')
}).fail(function () {
window.console.log('[gdnr] cannot find code')
})
})
// get geoip
$('body').on('gdnr.get.navi', function () {
var g = window.localStorage.getItem('gdnr-geoip')
if (g) {
gdnr.current.geoip = g
$('body').trigger('gdnr.get.geoip')
} else {
$.getJSON(gdnr.api.geoip, function (res) {
if (res && (res.code === 200)) {
gdnr.current.geoip = res.data.country
window.localStorage.setItem('gdnr-geoip', res.data.country)
$('body').trigger('gdnr.get.geoip')
} else {
window.console.log('[gdnr] cannot find geoip')
}
}).fail(function () {
window.console.log('[gdnr] cannot find geoip')
})
}
})
// get navigation
$('body').on('gdnr.get.lang', function () {
gdnr.current.navi = $('body').data('nav') || $('main#body').data('nav')
if (gdnr.current.navi) {
$('body').trigger('gdnr.get.navi')
} else {
window.console.log('[gdnr] cannot find navigation')
}
})
// get language
$('body').on('gdnr.get.path', function () {
gdnr.current.lang = $('body').data('lan') || $('main#body').data('lan')
if (gdnr.current.lang) {
$('body').trigger('gdnr.get.lang')
} else {
window.console.log('[gdnr] cannot find language')
}
})
// get path
$('body').on('gdnr.check.lodash', function () {
$('body').trigger('gdnr.get.path')
})
// check lodash
$('body').on('gdnr.check.storage', function () {
if (typeof _ === 'undefined') {
window.console.log('[gdnr] cannot find lodash')
} else {
$('body').trigger('gdnr.check.lodash')
}
})
// check jquery
$(function () {
if (window.localStorage.getItem('gdnr-no')) {
window.console.log('[gdnr] user canceled')
} else {
$('body').trigger('gdnr.check.storage')
}
})