forked from ONLYOFFICE/sdkjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacros.js
504 lines (456 loc) · 12.7 KB
/
macros.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
* (c) Copyright Ascensio System SIA 2010-2024
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-6 Ernesta Birznieka-Upish
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
"use strict";
(/**
* @param {Window} window
* @param {undefined} undefined
*/
function (window, undefined)
{
/** @constructor */
function CDocumentMacros()
{
this.Id = "_macrosGlobalId";//AscCommon.g_oIdCounter.Get_NewId();
this.Lock = new AscCommon.CLock();
this.Data = "";
AscCommon.g_oTableId.Add(this, this.Id);
}
CDocumentMacros.prototype.SetData = function(sData)
{
AscCommon.History.Add(new CChangesDocumentMacrosData(this, this.Data, sData));
this.Data = sData;
};
CDocumentMacros.prototype.GetData = function()
{
return this.Data;
};
CDocumentMacros.prototype.Get_Id = function()
{
return this.Id;
};
CDocumentMacros.prototype.CheckLock = function()
{
this.Lock.Check(this.Id);
};
CDocumentMacros.prototype.Write_ToBinary2 = function(Writer)
{
Writer.WriteLong(AscDFH.historyitem_type_DocumentMacros);
// String2 : Id
// String2 : Data
Writer.WriteString2("" + this.Id);
Writer.WriteString2(this.Data);
};
CDocumentMacros.prototype.Read_FromBinary2 = function(Reader)
{
// String2 : Id
// String2 : Data
this.Id = Reader.GetString2();
this.Data = Reader.GetString2();
};
CDocumentMacros.prototype.Refresh_RecalcData = function()
{
};
CDocumentMacros.prototype.runAuto = function()
{
try
{
var obj = JSON.parse(this.Data);
if (!obj["macrosArray"])
return;
for (var i = 0; i < obj["macrosArray"].length; i++)
{
if (true !== obj["macrosArray"][i]["autostart"])
continue;
AscCommon.safePluginEval(obj["macrosArray"][i]["value"]);
}
}
catch (err)
{
console.error(err);
}
};
CDocumentMacros.prototype.run = function(sGuid)
{
try
{
var obj = JSON.parse(this.Data);
if (!obj["macrosArray"])
return;
for (var i = 0; i < obj["macrosArray"].length; i++)
{
if (sGuid === obj["macrosArray"][i]["guid"])
{
AscCommon.safePluginEval(obj["macrosArray"][i]["value"]);
break;
}
}
}
catch (err)
{
console.error(err);
}
};
CDocumentMacros.prototype.getGuidByName = function(sName)
{
try
{
var obj = JSON.parse(this.Data);
if (!obj["macrosArray"])
return;
for (var i = 0; i < obj["macrosArray"].length; i++)
{
if (sName === obj["macrosArray"][i]["name"])
{
return obj["macrosArray"][i]["guid"];
}
}
}
catch (err)
{
}
return "";
};
CDocumentMacros.prototype.getNameByGuid = function(sGuid)
{
try
{
var obj = JSON.parse(this.Data);
if (!obj["macrosArray"])
return;
for (var i = 0; i < obj["macrosArray"].length; i++)
{
if (sGuid === obj["macrosArray"][i]["guid"])
{
return obj["macrosArray"][i]["name"];
}
}
}
catch (err)
{
}
return "";
};
CDocumentMacros.prototype.getAllNames = function()
{
try
{
var obj = JSON.parse(this.Data);
if (!obj["macrosArray"])
return [];
var oNamesMap = {};
var aNames = [];
for (var i = 0; i < obj["macrosArray"].length; i++)
{
var sName = obj["macrosArray"][i]["name"];
if(!oNamesMap[sName])
{
oNamesMap[sName] = true;
aNames.push(sName);
}
}
return aNames;
}
catch (err)
{
}
return [];
};
CDocumentMacros.prototype.isExistAuto = function()
{
try
{
var obj = JSON.parse(this.Data);
if (!obj["macrosArray"])
return;
for (var i = 0; i < obj["macrosArray"].length; i++)
{
if (true === obj["macrosArray"][i]["autostart"])
return true;
}
}
catch (err)
{
}
return false;
};
function VbaProject() {
this.filename = null;
this.vbaXml = null;
this.vbaDataXml = null;
}
VbaProject.prototype.toStream = function (s) {
s.WriteUChar(AscCommon.g_nodeAttributeStart);
s._WriteString2(0, this.filename);
s.WriteUChar(AscCommon.g_nodeAttributeEnd);
s.WriteRecord2(0, this.vbaXml, function(val){
s.WriteString2(val);
});
s.WriteRecord2(1, this.vbaDataXml, function(val){
s.WriteString2(val);
});
};
VbaProject.prototype.fromStream = function (s) {
var _len = s.GetULong();
var _start_pos = s.cur;
var _end_pos = _len + _start_pos;
var _at;
// attributes
s.GetUChar();
while (true)
{
_at = s.GetUChar();
if (_at === AscCommon.g_nodeAttributeEnd)
break;
switch (_at)
{
case 0: {
this.filename = s.GetString2();
break;
}
default:
s.Seek2(_end_pos);
return;
}
}
//members
var _type;
while (true)
{
if (s.cur >= _end_pos)
break;
_type = s.GetUChar();
switch (_type)
{
case 0:
{
s.GetULong();//len
this.vbaXml = s.GetString2();
break;
}
case 1:
{
s.GetULong();//len
this.vbaDataXml = s.GetString2();
break;
}
default:
{
s.SkipRecord();
break;
}
}
}
s.Seek2(_end_pos);
};
AscDFH.changesFactory[AscDFH.historyitem_DocumentMacros_Data] = CChangesDocumentMacrosData;
AscDFH.changesRelationMap[AscDFH.historyitem_DocumentMacros_Data] = [AscDFH.historyitem_DocumentMacros_Data];
/**
* @constructor
* @extends {AscDFH.CChangesBaseStringProperty}
*/
function CChangesDocumentMacrosData(Class, Old, New)
{
AscDFH.CChangesBaseStringProperty.call(this, Class, Old, New);
}
CChangesDocumentMacrosData.prototype = Object.create(AscDFH.CChangesBaseStringProperty.prototype);
CChangesDocumentMacrosData.prototype.constructor = CChangesDocumentMacrosData;
CChangesDocumentMacrosData.prototype.Type = AscDFH.historyitem_DocumentMacros_Data;
CChangesDocumentMacrosData.prototype.private_SetValue = function(Value)
{
this.Class.Data = Value;
};
function customXMLHttpRequest() {
this._headers = [];
var t = this;
this.open = function(method, url, async, user, password) {
this._url = url;
this._method = method;
this._async = async;
this._user = user;
this._password = password;
};
this.setRequestHeader = function(name, value) {
this._headers.push({name: name, value: value});
};
this.send = function(body) {
setTimeout(function() {
window.g_asc_plugins.api.asc_getUserPermissionToMakeRequestFromMacros(t._url, sendRequest);
function sendRequest (permission) {
if (permission) {
var xhr = new XMLHttpRequest();
if (t.timeout)
xhr.timeout = t.timeout;
if (t.responseType)
xhr.responseType = t.responseType;
if ( t.hasOwnProperty('withCredentials') )
xhr.withCredentials = t.withCredentials;
xhr.open(t._method, t._url, t._async, t._user, t._password);
t._headers.forEach(function(el) {
xhr.setRequestHeader(el.name, el.value);
});
xhr.onload = function() {
t.status = xhr.status;
t.statusText = xhr.statusText;
t.response = xhr.response;
t.responseText = xhr.responseText;
t.responseURL = xhr.responseURL;
t.responseXML = xhr.responseXML;
t.onload && t.onload();
};
xhr.onprogress = function(event) {
t.onprogress && t.onprogress(event);
};
xhr.onreadystatechange = function() {
t.readyState = this.readyState;
t.onreadystatechange && t.onreadystatechange();
};
xhr.onerror = function(error) {
t.onerror && t.onerror(error || "User doesn't allow this request.");
};
xhr.ontimeout = function(event) {
t.ontimeout && t.ontimeout(event);
};
xhr.onloadstart = function(event) {
t.onloadstart && t.onloadstart(event);
};
xhr.onloadend = function(event) {
t.onloadend && t.onloadend(event);
};
xhr.onabort = function(event) {
t.onabort && t.onabort(event);
};
if (typeof t.upload == 'object') {
xhr.upload.onabort = function(event) {
t.upload.onabort && t.upload.onabort(event);
};
xhr.upload.onerror = function(event) {
t.upload.onerror && t.upload.onerror(event);
};
xhr.upload.onload = function(event) {
t.upload.onload && t.upload.onload(event);
};
xhr.upload.onloadend = function(event) {
t.upload.onloadend && t.upload.onloadend(event);
};
xhr.upload.onloadstart = function(event) {
t.upload.onloadstart && t.upload.onloadstart(event);
};
xhr.upload.onprogress = function(event) {
t.upload.onprogress && t.upload.onprogress(event);
};
xhr.upload.ontimeout = function(event) {
t.upload.ontimeout && t.upload.ontimeout(event);
};
}
t.getResponseHeader = function(name) {
return xhr.getResponseHeader(name);
};
t.getAllResponseHeaders = function() {
return xhr.getAllResponseHeaders();
};
t.abort = function() {
xhr.abort();
}
xhr.send(body || null);
} else if (t.onerror) {
t.onerror("User doesn't allow this request.");
}
}
});
};
}
window['AscCommon'] = window['AscCommon'] || {};
window["AscCommon"].CDocumentMacros = CDocumentMacros;
window['AscCommon'].VbaProject = VbaProject;
var _safe_eval_closure = new Function("Function", "Api", "window", "AscDesktopEditor", "alert", "document", "XMLHttpRequest", "self", "globalThis", "setTimeout", "setInterval", "value", "return eval(\"\\\"use strict\\\";\\r\\n\" + value)");
function _safePluginEval(value) {
let protoFunc = Object.getPrototypeOf(function(){});
// for minimization we use eval!!!
let protoFuncGen = null;
try {
protoFuncGen = eval("Object.getPrototypeOf(function*(){})");
} catch (err) {
protoFuncGen = null;
}
const normalConstructor = protoFunc.constructor;
const generatorNext = protoFuncGen ? protoFuncGen.prototype.next : null;
protoFunc.constructor = function(){};
if (protoFuncGen)
protoFuncGen.prototype.next = function(){};
const timeout = function(cb, delay) {
var args = Array.prototype.slice.call(arguments, 2);
return setTimeout(function() {
cb.apply({}, args);
}, delay);
};
const interval = function(cb, delay) {
var args = Array.prototype.slice.call(arguments, 2);
return setInterval(function() {
cb.apply({}, args);
}, delay);
};
const Api = window.g_asc_plugins.api;
// clear this field on each run
delete Api.parsedJSDoc;
// check if we add a custom function into this macros we will parse a jsdoc
if (value.includes('AddCustomFunction') && value.includes('@customfunction')) {
// calculate how any times the function of adding will be called
const countOfAdding = (value.match(/\.AddCustomFunction\(/g) || []).length;
// parse JSDOC and put it to Api
Api.parsedJSDoc = AscCommon.parseJSDoc(value);
// remove extra parsed JSDOC
if (Api.parsedJSDoc.length > countOfAdding)
Api.parsedJSDoc.length = countOfAdding;
}
const result = _safe_eval_closure.call(null, {}, Api, {}, {}, function(){}, {}, customXMLHttpRequest, {}, {}, timeout, interval, value);
protoFunc.constructor = normalConstructor;
if (protoFuncGen)
protoFuncGen.prototype.next = generatorNext;
return result;
};
window['AscCommon'].safePluginEval = function(value){
let result;
let isCheckSymbols = AscFonts.IsCheckSymbols;
AscFonts.IsCheckSymbols = true;
try
{
result = _safePluginEval(value);
}
catch (err)
{
result = undefined;
console.error(err);
}
AscFonts.IsCheckSymbols = isCheckSymbols;
return result;
};
})(window);