Skip to content

Commit 4a278c6

Browse files
committed
Delete closed ports from array
1 parent e121ee2 commit 4a278c6

File tree

4 files changed

+103
-65
lines changed

4 files changed

+103
-65
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"eslint-plugin-jsx-a11y": "^6.2.1",
2828
"esprima": "^4.0.1",
2929
"estraverse": "^4.2.0",
30-
"lodash": "^4.17.11"
30+
"lodash": "^4.17.11",
31+
"lodash.clonedeep": "^4.5.0"
3132
}
3233
}

src/app/components/App.jsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class App extends Component {
4545
isPlaying: false,
4646
isRecording: false,
4747
isPlayingIndex: 0,
48+
id: 0,
49+
action: {},
50+
state: {},
4851
};
4952

5053
this.portToExtension = null;
@@ -123,7 +126,7 @@ class App extends Component {
123126

124127
setIsRecording() {
125128
// This variable will prevent the app from refreshing when we refresh
126-
// the userpage.
129+
// the userpage.
127130
this.justStartedRecording = true;
128131

129132
this.setState(state => ({
@@ -219,8 +222,6 @@ class App extends Component {
219222
direction: 'forward',
220223
});
221224

222-
// if (isPlayingIndex >= this.state.data.length - 1) isPlayingIndex = 0;
223-
224225
const { id, action, state } = data[isPlayingIndex + 1];
225226
this.setState(prev => ({
226227
...prev,
@@ -268,6 +269,9 @@ class App extends Component {
268269
isPlaying: false,
269270
isRecording: false,
270271
isPlayingIndex: 0,
272+
id: 0,
273+
action: {},
274+
state: {},
271275
});
272276
}
273277

@@ -277,12 +281,11 @@ class App extends Component {
277281
id,
278282
state,
279283
data,
280-
setIsPlaying,
281284
isPlaying,
282-
setIsRecording,
283285
isRecording,
284286
filteredData,
285287
searchField,
288+
isPlayingIndex,
286289
} = this.state;
287290

288291
return (
@@ -315,7 +318,7 @@ class App extends Component {
315318
toTheFuture={this.toTheFuture}
316319
toThePast={this.toThePast}
317320
isPlaying={isPlaying}
318-
isPlayingIndex={this.state.isPlayingIndex}
321+
isPlayingIndex={isPlayingIndex}
319322
isRecording={isRecording}
320323
setIsPlaying={this.setIsPlaying}
321324
setIsRecording={this.setIsRecording}
@@ -327,4 +330,4 @@ class App extends Component {
327330
}
328331
}
329332

330-
export default App;
333+
export default App;

src/browser/chrome/background.js

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const parseAndGenerate = require('./scripts/parser');
2-
const ports = [];
1+
const parseAndGenerate = require('./scripts/parser2');
2+
3+
let ports = [];
34

45
chrome.tabs.onUpdated.addListener((id, info, tab) => {
56
if (tab.status !== 'complete' || tab.url.startsWith('chrome')) return;
@@ -12,6 +13,7 @@ chrome.tabs.onUpdated.addListener((id, info, tab) => {
1213
});
1314

1415
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
16+
interceptedUrl = '';
1517
notifyPorts(
1618
{ action: 'refresh_devtool', tabId: tabs[0].id },
1719
'devtools',
@@ -26,7 +28,9 @@ function handleRequest(request) {
2628
if (!interceptedUrl.startsWith(request.initiator)) return { cancel: false };
2729

2830
if (request.type === 'script' && !request.url.startsWith('chrome')
29-
&& request.frameId === 0) {
31+
&& request.frameId === 0 && ((request.url.slice(-3) === '.js')
32+
|| (request.url.slice(-4) === '.jsx'))) {
33+
console.log('Got one: ', request.url, request);
3034
// TODO: adjust comment
3135
// Else we need to check wether or not this contains the react
3236
// library. If it does, we need to send the edit javascript to
@@ -40,6 +44,9 @@ function handleRequest(request) {
4044

4145
sendMessageToContent(parseAndGenerate(syncRequest.responseText));
4246

47+
// const code = encodeURIComponent(parseAndGenerate(syncRequest.responseText));
48+
// console.log('CODE - LENGTH: ', code.length);
49+
// return { redirectUrl: 'data:application/javascript; charset=utf-8,'.concat(code) };
4350
return { redirectUrl: 'javascript:' };
4451
}
4552
}
@@ -78,52 +85,50 @@ function addScriptInterception() {
7885

7986
// if (request.type !== 'script' || request.url.startsWith('chrome')
8087
// || 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-
88+
// console.log('onBeforeHeaders = ', request.url);
89+
// // request.requestHeaders.push({
90+
// // name: 'Access-Control-Allow-Credentials',
91+
// // value: '*',
92+
// // });
93+
94+
// // request.requestHeaders.push({
95+
// // name: 'Accept',
96+
// // value: 'application/javascript',
97+
// // });
98+
99+
// // request.requestHeaders.push({
100+
// // name: 'ABC',
101+
// // value: 'abc',
102+
// // });
103+
// const filteredHeaders = [{name: 'TTT', value: 'OK'}];
97104
// for (let i = 0; i < request.requestHeaders.length; i++) {
98105
// const header = request.requestHeaders[i];
99-
// if (header.name === 'Origin') {
100-
// console.log('found one');
101-
// delete request.requestHeaders[i];
106+
// if (header.name !== 'Origin') {
107+
// filteredHeaders.push({
108+
// name: header.name,
109+
// value: header.value,
110+
// });
102111
// }
103112
// }
104-
105-
// console.log('intercepting fom beforesendheaders: ', request);
113+
// return { requestHeaders: filteredHeaders };
106114
// },
107115
// { urls: ['<all_urls>'] },
108116
// ['blocking', 'requestHeaders'],
109117
// );
110118

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);
119+
// chrome.webRequest.onSendHeaders.addListener(
120+
// (request) => {
121+
// console.log('onSendHeaders: ', request);
122+
// },
123+
// { urls: ['<all_urls>'] },
124+
// ['requestHeaders'],
125+
// );
122126

123-
// return { redirectUrl: 'data:application/javascript; charset=utf-8,'.concat(syncRequest.responseText) };
124-
// },
125-
// { urls: ['<all_urls>'] },
126-
// ['blocking', 'responseHeaders']);
127+
// chrome.webRequest.onHeadersReceived.addListener(
128+
// handleRequest,
129+
// { urls: ['<all_urls>'] },
130+
// ['blocking', 'responseHeaders'],
131+
// );
127132
}
128133

129134
let reqIndex = 0;
@@ -135,12 +140,17 @@ function sendMessageToContent(codeString) {
135140
}
136141

137142
function notifyPorts(msg, portName) {
138-
ports.forEach((port) => {
139-
if (portName && (port.name !== portName)) return;
140-
try {
141-
port.postMessage(msg);
142-
} catch {
143-
console.log('notifyPorts has found some closed conections.');
143+
let index = 0;
144+
while (index < ports.length) {
145+
if (portName && (ports[index].name !== portName)) index++;
146+
else {
147+
try {
148+
ports[index].postMessage(msg);
149+
index++;
150+
} catch {
151+
// remove closed port from array
152+
ports = [...ports.slice(0, index), ...ports.slice(index + 1)];
153+
}
144154
}
145-
});
155+
}
146156
}

0 commit comments

Comments
 (0)