-
Notifications
You must be signed in to change notification settings - Fork 2
/
gbot.js
85 lines (65 loc) · 2.12 KB
/
gbot.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
angular.module('googleBot', [])
.service('gBot', ['$window', '$state', function ($window, $state) {
var self = this;
self.initiated = false;
self.active = false;
self.redirect = [];
self.isActive = function(e){
if(!self.initiated) {
self.init();
self.initiated = true;
}
if(!self.active) return false;
else {
e.preventDefault();
self.active = false;
$state.go(self.redirect[0], self.redirect[1]);
return true;
}
};
self.init = function(){
//var returned = false;
if($window.location.search.search("_escaped_fragment_=") > -1){
var params = $window.location.search.replace(/^\?/, "").split("&");
angular.forEach(params, function(item, ind){
if(item.split("=")[0] === "_escaped_fragment_"){
//activate
var result = {
url: item.split("=")[1],
target: []
};
angular.forEach(result.url.split("/"), function(itemb, indb){
if(itemb) result.target.push(itemb);
});
//search
angular.forEach($state.get(), function(state, state_ind){
if(!state.abstract){
var state_result = { target: [], params: [] };
angular.forEach(state.url.replace(/^\^/, "").split("/"), function(itemb, indb){
if(itemb.search(":") > -1) state_result.params.push(itemb.replace("?", "").replace(":", ""));
else if(itemb) state_result.target.push(itemb);
});
//compare
var check = result.target.length === state_result.target.length + state_result.params.length;
angular.forEach(state_result.target, function(target, index){
if(result.target[index] !== target) check = false;
});
//success
if(check){
var final_params = {};
angular.forEach(state_result.params, function(param, index){
final_params[param] = result.target[state_result.target.length + index];
});
//returned = [state.name, final_params];
self.redirect = [state.name, final_params];
self.active = true;
return false;
}
}
});
}
});
}
return;
};
}]);