-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathinject.js
73 lines (67 loc) · 2.29 KB
/
inject.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
const domParse = require('./dom-parse.js');
const nodeStore = require('./nodeStore.js');
const assert = require('./assert.js');
let topNode;
let firstPass = true;
// importing React from example app
function injector(React, parentNode) {
topNode = parentNode;
startTraverse(parentNode);
const func = React.Component.prototype.setState;
React.Component.prototype.setState = function(...args) {
// set timeout to delay traverse so that it is appended to original setState
startTraverse(this);
return func.apply(this, args);
}
// listens for messages from backgroundjs -> content script -> webpage
window.addEventListener('message', function(event) {
// only accept messges to self
if (event.source != window) return;
// filter out other messages floating around in existing context
if (event.data.type === 'assertion') {
if (event.data.flag === 'onload') {
event.data.message.forEach(item => {
assert.addAssert(item);
});
startTraverse(parentNode);
} else if (event.data.flag === 'delete') {
assert.deleteBlock(event.data.message);
} else {
assert.addAssert(event.data.message);
}
}
}, false);
}
function startTraverse(self, reactDom) {
const nodePackage = {};
setTimeout(()=> {
let travPromise = throttle(domParse.parser, 25);
travPromise.then((result) => {
// Conditional to display feedback for react router incompatibility
if (result === 'react-router') {
window.postMessage({ type: 'virtualdom', data: 'react-router' }, "*");
} else {
nodePackage.virtualDom = result;
nodePackage.nodeStore = nodeStore.storage;
let title = document.title;
// specify message type to target specific message
window.postMessage({ type: 'virtualdom', data: nodePackage, topNode: topNode.constructor.name, title: title, first: firstPass}, "*");
firstPass = false;
}
});
}, 0);
}
function throttle(func, wait) {
let waiting = false;
return new Promise((resolve, reject) => {
if (waiting) reject();
waiting = true;
setTimeout(() => {
waiting = false;
let result = func(topNode);
resolve(result);
}, wait);
return func(topNode);
});
}
module.exports = injector;