-
Notifications
You must be signed in to change notification settings - Fork 17
/
module.js
226 lines (184 loc) · 5.58 KB
/
module.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
224
225
226
module._init = function(){
document.addEventListener(this.name, this._listener.bind(this));
this.exports.init.bind(this.exports)();
}
module._listener = function (e){
var data = JSON.parse(e.detail);
switch(data.event) {
case 'update':
module.exports.update();
break;
case 'dispose':
module._dispose();
break;
case 'xhttp':
break;
default:
break;
}
if (data._cb){
if (module._cbEvents[data._cb]){
var cb = module._cbEvents[data._cb].cb;
if (cb){
cb(data);
}
delete module._cbEvents[data._cb];
}else{
console.error("Failed to fetch callback event: " + data._cb);
}
}
}
module._dispose = function(){
document.removeEventListener(this.name, this._listener);
}
module._guid = function(){
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
module._cbEvents = {}
module.dispatchEvent = function(data, cb){
data.module = this.name;
if (cb){
var guid = module._guid();
module._cbEvents[guid] = {
time: new Date().getTime(),
id: guid,
cb: cb
}
data._cb = guid;
}
var evt = new CustomEvent("_" + this.name, {
detail: JSON.stringify(data),
bubbles: true,
cancelable: true
});
document.dispatchEvent(evt);
}
module.getDeepValue = function(obj, path){
for (var i=0, path=path.split('.'), len=path.length; i<len; i++){
if (obj === undefined || obj[path[i]] === undefined){
return undefined;
}
obj = obj[path[i]];
};
return obj;
}
module.getScopeData = function(scopeName, objectPath, mustExistPathArr, cb){
var self = this;
this.wait.bind(this)(this.isScopeReady.bind(this, scopeName, objectPath, mustExistPathArr), 50, function(error){
if (error){
console.error(`condition failed for scope: ${scopeName}, path: ${objectPath}, mustExistArr: ${mustExistPathArr}`)
}else{
var scope = angular.element(document.getElementsByClassName(`${scopeName} ng-scope`)).scope();
cb(self.getDeepValue(scope, objectPath));
}
});
}
module.setScopeData = function(scope, objectPath, value, cb){
var script = document.createElement('script');
script.textContent = `angular.element(document.getElementsByClassName('${scope} ng-scope')).scope().${objectPath}=${value};`;
(document.body || document.head || document.documentElement).appendChild(script);
script.remove();
if(cb){
cb();
}
}
module.wait = function(condition, tries, cb){
var self = this;
if (condition()){
cb.bind(self)();
}else{
if (tries > 0){
setTimeout(function() { self.wait.bind(self)(condition, tries - 1, cb); }, 100);
}else{
cb.bind(self)("failed condition");
}
}
}
module.isScopeReady = function(scopeName, objectPath, mustExistPathArr){
var self = this;
var scope = angular.element(document.getElementsByClassName(`${scopeName} ng-scope`)).scope();
var object = self.getDeepValue(scope, objectPath);
var rootValid = (scope && object && Object.keys(object).length);
if (mustExistPathArr.length){
if (rootValid){
let ready = true;
mustExistPathArr.forEach(function(path){
var obj = self.getDeepValue(scope, path);
if (obj === undefined){
return ready = false;
} else if (obj instanceof Array && !obj.length){
return ready = false;
} else if (typeof obj === 'object' && !Object.keys(obj).length){
return ready = false;
}
});
return ready;
}
}
return rootValid;
}
module.ajaxCall = function(data, cb){
// Set tokens if it's a request to @screeps
if (data.url && data.url.startsWith("https://screeps.com/")){
var auth = JSON.parse(localStorage.getItem('auth'));
data.headers = {
'X-Token' : auth,
'X-Username' : auth
}
}
var request = $.ajax(data);
request.done(function(msg) {
if (cb){
cb(msg);
}
});
request.fail(function(jqXHR, msg) {
if (cb){
cb(undefined, jqXHR.status);
}
});
}
module.ajaxGet = function(url, cb){
module.ajaxCall({
url: url,
method: 'GET'
}, cb);
}
module.getCurrentShard = function(){
var url = window.location.href;
if (url.indexOf("shard") > -1){
var pathArray = window.location.href.split('/');
for (var i = 0; i < pathArray.length; i++) {
if (pathArray[i].startsWith("shard")){
return pathArray[i].split('?')[0];
}
}
}
return "";
}
module.sendConsoleCommand = function(command, cb, shard){
if (!shard){
shard = "shard0";
}
module.ajaxCall({
url: "https://screeps.com/api/user/console",
method: "POST",
data: {
expression: command,
shard: shard
}
}, cb);
}
module.exports = {
init: function(){
// To be overrided
console.warn("module.exports.init is not overrided.");
},
update:function(){
// To be overrided
console.warn("module.exports.update is not overrided.");
},
}