-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathinject.js
More file actions
159 lines (148 loc) · 5.16 KB
/
inject.js
File metadata and controls
159 lines (148 loc) · 5.16 KB
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
//======================================================================
// Define variables
//======================================================================
window.vpBase = window.vpBase? window.vpBase: '';
//======================================================================
// Define functions
//======================================================================
function vp_log(msg) {
console.log('[vp] ', ...arguments);
}
function vp_init() {
// require config
vp_config_require();
}
function vp_inject(path) {
var s = document.createElement('script');
s.src = path;
(document.head || document.documentElement).appendChild(s);
s.remove();
}
function __VP_CSS_LOADER__(path) {
return 'css!' + path;
}
function __VP_TEXT_LOADER__(path) {
return 'text!' + path + '!strip';
}
function __VP_RAW_LOADER__(path) {
return 'text!' + path;
}
function vp_config_require() {
// Configure requirejs
try {
if (require === undefined || require == null) {
// remove inject script
let injectedScript = document.querySelector(`script[src="${vpBase}inject.js"]`);
if (injectedScript) {
injectedScript.remove();
}
return;
}
} catch (ex) {
// remove inject script
let injectedScript = document.querySelector(`script[src="${vpBase}inject.js"]`);
if (injectedScript) {
injectedScript.remove();
}
return;
}
require.config({
baseUrl: vpBase,
paths:{
'vp_base' : 'visualpython',
'text' : 'visualpython/lib/require/text',
'css' : 'visualpython/lib/require/css.min',
'jquery' : 'visualpython/lib/jquery/jquery-3.6.0.min',
'jquery-ui' : 'visualpython/lib/jquery/jquery-ui.min',
'codemirror': 'visualpython/lib/codemirror',
'marked' : 'visualpython/lib/marked/marked',
'mathjaxutils' : 'visualpython/lib/mathjax/mathjaxutils',
'fontawesome' : 'visualpython/lib/fontawesome/fontawesome.min'
},
shim: {
"jquery-ui": {
exports: "$",
deps: ['jquery']
}
},
config: {
text: {
// allow CORS
useXhr: function(url, protocol, hostname, port) {
// console.log('allow xhr');
return true;
},
onXhr: function(xhr, url) {
// console.log(xhr);
}
}
},
map: {
'*': {
css : 'visualpython/lib/require/css.min'
}
},
packages: [{
name: "codemirror",
location: "visualpython/lib/codemirror/",
main: "lib/codemirror"
}]
});
// Load vp
define('vp/injectScript', [
'text',
'css',
'jquery',
'jquery-ui',
// 'css!vp_base/lib/jquery/jquery-ui.min',
__VP_CSS_LOADER__('vp_base/lib/jquery/jquery-ui.min'),
'codemirror/lib/codemirror',
__VP_CSS_LOADER__('codemirror/lib/codemirror'),
'vp_base/js/loadVisualpython'
], function(text, css, $, ui, uiCss, codemirror, cmCss, loadVisualpython) {
// codemirror
window.codemirror = codemirror;
loadVisualpython.initVisualpython();
});
}
//======================================================================
// Event listener
//======================================================================
var _vpcommHandler = function(e) {
let detailObj = e.detail;
if (detailObj && detailObj.type) {
switch (detailObj.type) {
case 'sendBase':
vp_log('received from inject - ', e.detail.type, e);
// get base url of its extension
vpBase = detailObj.data;
// check if it has vp_wrapper
if (document.getElementById('vp_wrapper') == null) {
// initialize vp environment
vp_init();
} else {
// send event to toggle vp
let detailObj = { type: 'toggle', data: 'hi' };
let evt = new CustomEvent('vpcomm', { bubbles: true, detail: detailObj });
document.dispatchEvent(evt);
}
break;
// CHROME: TODO: 2: use vp frame.toggle for toggle it
// case 'toggle':
// // toggle vp_wrapper
// if (window.vpBase != '' && window.$) {
// vp_toggle();
// } else {
// vp_log('No jquery...');
// // init again
// vp_init();
// }
// break;
default:
break;
}
}
};
document.removeEventListener('vpcomm', _vpcommHandler);
document.addEventListener('vpcomm', _vpcommHandler);
// End of file