forked from ONLYOFFICE/sdkjs-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluginBase.js
97 lines (84 loc) · 2.22 KB
/
pluginBase.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
(function(window, undefined){
window.Asc = window.Asc || {};
window.Asc.plugin = {};
function CopyObj(Obj, ObjectExt)
{
if( !Obj || !("object" == typeof(Obj) || "array" == typeof(Obj)) )
{
return Obj;
}
var c = (ObjectExt === undefined) ? {} : ObjectExt;
var p, v;
for(p in Obj)
{
if(Obj.hasOwnProperty(p))
{
v = Obj[p];
if (v && "object" === typeof v)
{
c[p] = CopyObj(v);
}
else
{
c[p] = v;
}
}
}
return c;
}
window.onload = function()
{
var xhr = new XMLHttpRequest();
xhr.open("get", "./config.json", true);
xhr.responseType = "json";
xhr.onload = function()
{
if (xhr.status == 200)
{
var objConfig = xhr.response;
if ((typeof objConfig) == "string")
objConfig = JSON.parse(objConfig);
// extend window.Asc.plugin object
CopyObj(objConfig, window.Asc.plugin);
var obj = {
type : "initialize",
guid : window.Asc.plugin.guid
};
window.parent.postMessage(JSON.stringify(obj), "*");
}
};
xhr.send();
};
function onMessage(event)
{
if (!window.Asc.plugin)
return;
if (window.plugin_onMessage)
{
window.plugin_onMessage(event);
return;
}
if (typeof(event.data) == "string")
{
var pluginData = {};
try
{
pluginData = JSON.parse(event.data);
}
catch(err)
{
pluginData = {};
}
if (pluginData.type == "plugin_init")
eval(pluginData.data);
}
}
if (window.addEventListener)
{
window.addEventListener("message", onMessage, false);
}
else
{
window.attachEvent("onmessage", onMessage);
}
})(window, undefined);