Skip to content

Commit bcc73d9

Browse files
victorvrvkiacolbert
authored andcommitted
Refreshes the devtools instance when user refreshes tab (#56)
* Fixed bug that made devtool refresh when we started recording. * added react-json-view to package-lock * Data stays filtered after new actions get posted * fixed time travel forward when already at tail of linked list.
1 parent 7ed7a06 commit bcc73d9

File tree

3 files changed

+130
-43
lines changed

3 files changed

+130
-43
lines changed

src/app/components/App.jsx

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class App extends Component {
4848
};
4949

5050
this.portToExtension = null;
51+
this.justStartedRecording = false;
5152

5253
this.addActionToView = this.addActionToView.bind(this);
5354
this.toTheFuture = this.toTheFuture.bind(this);
@@ -64,21 +65,31 @@ class App extends Component {
6465
// adds listener to the effects that are gonna be sent from
6566
// our edited useReducer from the 'react' library.
6667
chrome.runtime.onConnect.addListener((port) => {
67-
if (port.name === 'injected-app') {
68-
this.portToExtension = port;
69-
70-
port.onMessage.addListener((msg) => {
71-
const newData = {
72-
action: msg.action,
73-
state: msg.state,
74-
id: this.state.data.length,
75-
};
76-
this.setState((state) => ({
68+
if (port.name !== 'injected-app') return;
69+
70+
this.portToExtension = port;
71+
72+
port.onMessage.addListener((msg) => {
73+
const newData = {
74+
action: msg.action,
75+
state: msg.state,
76+
id: this.state.data.length,
77+
};
78+
79+
const { searchField } = this.state;
80+
const newDataActionType = newData.action.type.toLowerCase();
81+
82+
if (newDataActionType.includes(searchField.toLowerCase())) {
83+
this.setState(state => ({
7784
data: [...state.data, newData],
7885
filteredData: [...state.data, newData],
7986
}));
80-
});
81-
}
87+
} else {
88+
this.setState(state => ({
89+
data: [...state.data, newData],
90+
}));
91+
}
92+
});
8293
});
8394

8495
// We listen to the message from devtools.js (sent originally from
@@ -93,8 +104,6 @@ class App extends Component {
93104
});
94105
}
95106

96-
97-
98107
// functionality to change 'play' button to 'stop'
99108
setIsPlaying() {
100109
if (this.state.isPlayingIndex > this.state.data.length - 1) {
@@ -110,9 +119,11 @@ class App extends Component {
110119
}
111120
}
112121

113-
// functionality to change 'record' button to 'pause'
114122
setIsRecording() {
115-
console.log('setIsRecording:', this.state.isRecording)
123+
// This variable will prevent the app from refreshing when we refresh
124+
// the userpage.
125+
this.justStartedRecording = true;
126+
116127
this.setState(state => ({
117128
isRecording: !state.isRecording,
118129
}));
@@ -206,6 +217,9 @@ class App extends Component {
206217

207218
// function to travel to the FUTURE
208219
toTheFuture() {
220+
const { data, isPlayingIndex } = this.state;
221+
if (isPlayingIndex === data.length - 1) return;
222+
209223
if (!this.portToExtension) return console.error('No connection on stored port.');
210224
this.portToExtension.postMessage({
211225
type: 'TIMETRAVEL',
@@ -214,7 +228,6 @@ class App extends Component {
214228

215229
// if (isPlayingIndex >= this.state.data.length - 1) isPlayingIndex = 0;
216230

217-
const { data, isPlayingIndex } = this.state;
218231
const { id, action, state } = data[isPlayingIndex + 1];
219232
this.setState(prev => ({
220233
...prev,
@@ -229,29 +242,32 @@ class App extends Component {
229242

230243
// function to travel to the PAST
231244
toThePast() {
245+
const { data, isPlayingIndex } = this.state;
246+
if (isPlayingIndex === 0) return;
247+
232248
if (!this.portToExtension) return console.error('No connection on stored port.');
233249
this.portToExtension.postMessage({
234250
type: 'TIMETRAVEL',
235251
direction: 'backwards',
236252
});
237253

238-
const { data, isPlayingIndex } = this.state;
239-
240-
if (isPlayingIndex === 0) {
241-
console.log('is playingIdx in toThePast is 0');
242-
} else {
243-
const { id, action, state } = data[isPlayingIndex - 1];
244-
this.setState(prev => ({
245-
...prev,
246-
id,
247-
action,
248-
state,
249-
isPlayingIndex: isPlayingIndex - 1,
250-
}));
251-
}
254+
const { id, action, state } = data[isPlayingIndex - 1];
255+
this.setState(prev => ({
256+
...prev,
257+
id,
258+
action,
259+
state,
260+
isPlayingIndex: isPlayingIndex - 1,
261+
}));
252262
}
253263

254264
resetApp() {
265+
if (this.justStartedRecording) {
266+
console.log('not reseting...');
267+
this.justStartedRecording = false;
268+
return;
269+
}
270+
console.log('reseting...');
255271
this.setState({
256272
data: [],
257273
searchField: '',

src/app/package-lock.json

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/browser/chrome/background.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ function handleRequest(request) {
2525
// TODO: filter the request from the webRequest call.
2626
if (!interceptedUrl.startsWith(request.initiator)) return { cancel: false };
2727

28-
console.log('intercepting... ', request);
2928
if (request.type === 'script' && !request.url.startsWith('chrome')
3029
&& request.frameId === 0) {
3130
// TODO: adjust comment
@@ -72,6 +71,59 @@ function addScriptInterception() {
7271
{ urls: ['<all_urls>'] },
7372
['blocking'],
7473
);
74+
75+
// chrome.webRequest.onBeforeSendHeaders.addListener(
76+
// (request) => {
77+
// if (!interceptedUrl.startsWith(request.initiator)) return { cancel: false };
78+
79+
// if (request.type !== 'script' || request.url.startsWith('chrome')
80+
// || request.frameId !== 0) return;
81+
82+
// request.requestHeaders.push({
83+
// name: 'Access-Control-Allow-Credentials',
84+
// value: '*',
85+
// });
86+
87+
// request.requestHeaders.push({
88+
// name: 'Accept',
89+
// value: 'application/javascript',
90+
// });
91+
92+
// request.requestHeaders.push({
93+
// name: 'ABC',
94+
// value: 'abc',
95+
// });
96+
97+
// for (let i = 0; i < request.requestHeaders.length; i++) {
98+
// const header = request.requestHeaders[i];
99+
// if (header.name === 'Origin') {
100+
// console.log('found one');
101+
// delete request.requestHeaders[i];
102+
// }
103+
// }
104+
105+
// console.log('intercepting fom beforesendheaders: ', request);
106+
// },
107+
// { urls: ['<all_urls>'] },
108+
// ['blocking', 'requestHeaders'],
109+
// );
110+
111+
// chrome.webRequest.onHeadersReceived.addListener((request) => {
112+
// if (!interceptedUrl.startsWith(request.initiator)) return { cancel: false };
113+
114+
// if (request.type !== 'script' || request.url.startsWith('chrome')
115+
// || request.frameId !== 0) return;
116+
117+
// const syncRequest = new XMLHttpRequest();
118+
// syncRequest.open('GET', request.url, false);
119+
// syncRequest.send(null);
120+
121+
// console.log('Got req onHeadersReceived!!!! ', request);
122+
123+
// return { redirectUrl: 'data:application/javascript; charset=utf-8,'.concat(syncRequest.responseText) };
124+
// },
125+
// { urls: ['<all_urls>'] },
126+
// ['blocking', 'responseHeaders']);
75127
}
76128

77129
let reqIndex = 0;

0 commit comments

Comments
 (0)