This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
studioJS.js
58 lines (50 loc) · 1.67 KB
/
studioJS.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
(function($, Mobify) {
if (!window.enableStudioJS) return;
var _tagIndex = 0;
var indexTags = function(html) {
var tagIndex = /<![\s\S]*?>|[^<]+|<!--[\s\S]*?-->|(<script)([\s\S]*?<\/script>)|(<\w+)((?:[^>'"]*|'[^']*?'|"[^"]*?")*\/?>)/gi
, result = html.replace(tagIndex, function(all, scriptName, scriptTail, name, tail) {
name = name || scriptName;
tail = tail || scriptTail;
if (!name) return all;
return name + ' mobifyjsindex="src' + tagIndex++ + '"' + tail;
});
return result;
};
var get = function(key, callback) {
var handler = function(ev) {
if ((ev.data.command === 'html')
&& (ev.data.dest === 'page')
&& (ev.data.key === key)
&& (ev.source === window)) {
window.removeEventListener("message", handler, false);
callback(ev.data.value, ev.data.key);
}
}
window.addEventListener("message", handler, false);
};
var set = function(key, value) {
window.postMessage({
dest : 'extension',
command : 'html',
key: key,
value: value
}, '*');
};
var oldEmitMarkup = Mobify.emitMarkup;
Mobify.emitMarkup = function(markup) {
Mobify.studioJS.get('renderHTML', function(markup) {
oldEmitMarkup(markup);
});
Mobify.studioJS.set('resultHTML', markup);
};
var oldExtract = Mobify.html.extract;
Mobify.html.extract = function() {
var captured = oldExtract.call(Mobify.html, markup);
$.each(captured, function(key, value) {
captured[key] = indexTags(value);
});
Mobify.studioJS.set('sourceHTML', captured.html());
return result;
};
})(Mobify.$, Mobify);