-
Notifications
You must be signed in to change notification settings - Fork 52
/
Builder.hx
183 lines (130 loc) · 4.79 KB
/
Builder.hx
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
package ;
import js.Node.*;
import js.node.Fs;
import js.html.*;
import js.Browser.*;
import haxe.Serializer;
import haxe.extern.Rest;
import haxe.Unserializer;
import haxe.ds.StringMap;
import haxe.DynamicAccess;
import haxe.Constraints.Function;
import Controller.Data;
import Controller.Payload;
using StringTools;
using haxe.io.Path;
class Builder {
public static var storage:Storage = window.sessionStorage;
private var max:Int = 2;
private var counter:Int = 0;
private var waitFor:Int = 100; // wait 100ms.
private var timestamp:Float = 0;
private var timeoutId:Null<Int>;
private var maxDuration:Int = 750; // wait 750ms.
private var observer:MutationObserver;
private var electron:Dynamic;
private var ipcRenderer:{on:String->Function->Dynamic, once:String->Function->Dynamic, send:String->Rest<Dynamic>->Void};
private var scripts:Array<String> = [];
private var completedScripts:Map<String, Bool> = new Map();
public static function main() {
var con:Builder = new Builder();
}
public function new() {
trace( 'running script' );
electron = require('electron');
observer = new MutationObserver(mutation);
observer.observe(window.document, {childList:true, subtree:true});
ipcRenderer = electron.ipcRenderer;
ipcRenderer.once('data:payload', function(event:String, arg:String) {
var data:Data = tink.Json.parse( arg );
storage.setItem( 'data', arg );
scripts = data.scripts;
processHtml( data.html );
processJson( data.payload );
});
}
public function processHtml(data:String) {
console.log( 'processing html' );
var node = window.document.querySelector('#markdown');
if (node != null) {
var template:TemplateElement = cast window.document.createElement('template');
template.innerHTML = data;
node.parentNode.replaceChild( window.document.importNode(template.content, true), node );
window.document.dispatchEvent( new CustomEvent('DocumentHtmlData', {detail:true, bubbles:true}) );
}
counter++;
if (counter >= max && timeoutId != null) timeoutId = cast setTimeout( preCheck, waitFor );
}
public function processJson(data:Payload) {
window.document.dispatchEvent( new CustomEvent('DocumentJsonData', {detail:data, bubbles:true}) );
console.log( 'processing json', data );
counter++;
if (counter >= max && timeoutId != null) timeoutId = cast setTimeout( preCheck, waitFor );
}
public function clean():Void {
for (link in window.document.querySelectorAll( 'link[rel="import"]' )) {
link.parentNode.removeChild( link );
}
}
public function preCheck():Void {
timestamp = haxe.Timer.stamp() - timestamp;
if (timestamp < maxDuration) {
if ([for (k in completedScripts.keys()) k].length == 0) {
if (scripts.length > 0) for (script in scripts) {
var name = script.withoutExtension();
require( '$__dirname/$script'.normalize() );
completedScripts.set( name, false );
window.document.addEventListener( '$name:complete', handleScriptCompletion );
} else {
timeoutId = cast setTimeout( save, waitFor );
}
} else {
var match = true;
for (key in completedScripts.keys()) {
match = completedScripts.get( key );
if (!match) break;
}
timeoutId = cast setTimeout( save, waitFor );
}
} else {
timeoutId = cast setTimeout( preCheck, waitFor );
}
}
public function save():Void {
// Wait until `maxDuration` has passed of no dom changes before continuing.
clearTimeout( cast timeoutId );
observer.disconnect();
counter = -1;
clean();
var node = window.document.doctype;
var doctype = node != null ? "<!DOCTYPE "
+ node.name
+ (node.publicId != null ? ' PUBLIC "' + node.publicId + '"' : '')
+ (node.publicId == null && node.systemId != null ? ' SYSTEM' : '')
+ (node.systemId != null ? ' "' + node.systemId + '"' : '')
+ '>\n' : '';
var html = window.document.documentElement.outerHTML;
if (html != null && html != '<html><head></head><body></body></html>') {
ipcRenderer.send('save', doctype + html);
} else {
ipcRenderer.send('save', 'failed');
}
}
private function handleScriptCompletion(event:CustomEvent):Void {
completedScripts.set( event.detail, true );
if (timeoutId != null) clearTimeout( cast timeoutId );
timestamp = haxe.Timer.stamp() - timestamp;
timeoutId = cast setTimeout( preCheck, waitFor );
}
private function mutation(changes:Array<MutationRecord>, observer:MutationObserver):Void {
for (change in changes) switch change.type {
case 'attributes':
case 'characterData':
case 'childList':
if (timeoutId != null) clearTimeout( cast timeoutId );
timestamp = haxe.Timer.stamp() - timestamp;
timeoutId = cast setTimeout( preCheck, waitFor );
case _:
}
}
}