Skip to content

Commit 7ed7a06

Browse files
authored
Merge branch 'victor-dev' into dev
2 parents f6d2743 + ed56308 commit 7ed7a06

File tree

5 files changed

+108
-241
lines changed

5 files changed

+108
-241
lines changed

src/app/components/App.jsx

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,39 @@ class App extends Component {
5757
this.actionInPlay = this.actionInPlay.bind(this);
5858
this.handleBarChange = this.handleBarChange.bind(this);
5959
this.searchChange = this.searchChange.bind(this);
60+
this.resetApp = this.resetApp.bind(this);
6061
}
6162

6263
componentDidMount() {
6364
// adds listener to the effects that are gonna be sent from
6465
// our edited useReducer from the 'react' library.
65-
chrome.runtime.onConnect.addListener((portFromExtension) => {
66-
this.portToExtension = portFromExtension;
67-
68-
portFromExtension.onMessage.addListener((msg) => {
69-
const newData = {
70-
action: msg.action,
71-
state: msg.state,
72-
id: this.state.data.length,
73-
};
74-
this.setState((state) => ({
75-
data: [...state.data, newData],
76-
filteredData: [...state.data, newData],
77-
isPlayingIndex: state.data.length - 1,
78-
}));
79-
});
66+
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) => ({
77+
data: [...state.data, newData],
78+
filteredData: [...state.data, newData],
79+
}));
80+
});
81+
}
82+
});
83+
84+
// We listen to the message from devtools.js (sent originally from
85+
// background) to refresh our App whenever the user refreshes the webpage.
86+
// The msg from background will come with the ID of the current tab.
87+
// We only want to refresh our App instance of that specific tab.
88+
window.addEventListener('message', (msg) => {
89+
const { action, tabId } = msg.data;
90+
if (action !== 'refresh_devtool') return;
91+
const devtoolsId = chrome.devtools.inspectedWindow.tabId;
92+
if (tabId === devtoolsId) this.resetApp();
8093
});
8194
}
8295

@@ -238,6 +251,17 @@ class App extends Component {
238251
}
239252
}
240253

254+
resetApp() {
255+
this.setState({
256+
data: [],
257+
searchField: '',
258+
filteredData: [],
259+
isPlaying: false,
260+
isRecording: false,
261+
isPlayingIndex: 0,
262+
});
263+
}
264+
241265
render() {
242266
const {
243267
action,

src/app/parser.js

Lines changed: 0 additions & 172 deletions
This file was deleted.

src/browser/chrome/background.js

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const parseAndGenerate = require('./scripts/parser');
2-
3-
let portToDevtools;
4-
const msgsToPanel = [];
2+
const ports = [];
53

64
chrome.tabs.onUpdated.addListener((id, info, tab) => {
75
if (tab.status !== 'complete' || tab.url.startsWith('chrome')) return;
@@ -13,16 +11,18 @@ chrome.tabs.onUpdated.addListener((id, info, tab) => {
1311
runAt: 'document_end',
1412
});
1513

16-
// refresh devtool panel everytime we refresh webpage
17-
// console.log('port: ', portToDevtools);
18-
// if (portToDevtools) portToDevtools.postMessage({ action: 'refresh_devtool' });
19-
// else msgsToPanel.push({ action: 'refresh_devtool' });
14+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
15+
notifyPorts(
16+
{ action: 'refresh_devtool', tabId: tabs[0].id },
17+
'devtools',
18+
);
19+
});
2020
});
2121

2222

2323
let interceptedUrl = '';
2424
function handleRequest(request) {
25-
// TODO: filter the request from the webRequest call.
25+
// TODO: filter the request from the webRequest call.
2626
if (!interceptedUrl.startsWith(request.initiator)) return { cancel: false };
2727

2828
console.log('intercepting... ', request);
@@ -48,27 +48,20 @@ function handleRequest(request) {
4848
// The App on the devtools panel start a connection so that it can
4949
// tell us when to start intercepting the script requests.
5050
chrome.runtime.onConnect.addListener((port) => {
51-
portToDevtools = port;
52-
53-
// if (msgsToPanel.length > 0) {
54-
// for (let msg of msgsToPanel) port.postMessage(msg);
55-
// }
56-
// we change the port to null when we disconnect, so that when we refresh
57-
// the page by start recording, we can check if (!port) and not refresh
58-
// the devtools page.
59-
port.onDisconnect.addListener(() => {
60-
portToDevtools = null;
61-
});
51+
if (ports) ports.push(port);
6252

6353
port.onMessage.addListener((msg) => {
64-
if (!msg.turnOnDevtool) return;
65-
interceptedUrl = msg.url;
66-
addScriptInterception();
54+
if (msg.turnOnDevtool) {
55+
interceptedUrl = msg.url;
56+
addScriptInterception();
6757

68-
// after activating our interception script, we refresh the active tab
69-
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
70-
chrome.tabs.update(tabs[0].id, { url: tabs[0].url });
71-
});
58+
// after activating our interception script, we refresh the active tab
59+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
60+
chrome.tabs.update(tabs[0].id, { url: tabs[0].url });
61+
});
62+
} else {
63+
console.log('Got a msg not turnOnDevtool: ', msg);
64+
}
7265
});
7366
});
7467

@@ -88,3 +81,14 @@ function sendMessageToContent(codeString) {
8881
chrome.tabs.sendMessage(tabs[0].id, { codeString, index });
8982
});
9083
}
84+
85+
function notifyPorts(msg, portName) {
86+
ports.forEach((port) => {
87+
if (portName && (port.name !== portName)) return;
88+
try {
89+
port.postMessage(msg);
90+
} catch {
91+
console.log('notifyPorts has found some closed conections.');
92+
}
93+
});
94+
}

src/browser/chrome/devtools.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
chrome.devtools.panels.create('React Rewind',
22
null,
33
'devtools.html',
4-
(extensionPanel) => {
5-
const port = chrome.runtime.connect({ name: 'devtools' });
6-
port.onMessage.addListener((msg) => {
7-
// console.log('got msg: ', msg);
8-
if (msg.action === 'refresh_devtool') extensionPanel.setPage('devtools.html');
4+
(devtoolsPanel) => {
5+
const backgroundPageConnection = chrome.runtime.connect({
6+
name: 'devtools',
97
});
108

11-
extensionPanel.onShown.addListener((panelWindow) => {
12-
panelWindow.backgroundPort = port;
9+
devtoolsPanel.onShown.addListener(function tmp(panelWindow) {
10+
// Run once only
11+
devtoolsPanel.onShown.removeListener(tmp);
12+
13+
const windowP = panelWindow;
14+
windowP.backgroundPort = backgroundPageConnection;
15+
16+
backgroundPageConnection.onMessage.addListener((message) => {
17+
// When we get a msg from background telling us that we need
18+
// to refresh the App, we send it to App via window.PostMessage()
19+
if (message.action === 'refresh_devtool') windowP.postMessage(message);
20+
});
1321
});
22+
1423
});

0 commit comments

Comments
 (0)