Skip to content

Commit ed56308

Browse files
committed
Refresh devtools instance of specific that specific tab when that tab refreshes
1 parent 1193141 commit ed56308

File tree

5 files changed

+108
-245
lines changed

5 files changed

+108
-245
lines changed

src/app/components/App.jsx

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +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() {
63-
// *******************************************************
64-
// need to impletement setState for filteredData to same value as data
65-
// this.setState({ data, filteredData: data });
66-
// *******************************************************
67-
6864
// adds listener to the effects that are gonna be sent from
6965
// our edited useReducer from the 'react' library.
70-
chrome.runtime.onConnect.addListener((portFromExtension) => {
71-
this.portToExtension = portFromExtension;
72-
73-
portFromExtension.onMessage.addListener((msg) => {
74-
const newData = {
75-
action: msg.action,
76-
state: msg.state,
77-
id: this.state.data.length,
78-
};
79-
this.setState((state) => ({
80-
data: [...state.data, newData],
81-
filteredData: [...state.data, newData],
82-
}));
83-
});
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();
8493
});
8594
}
8695

@@ -203,6 +212,17 @@ class App extends Component {
203212
});
204213
}
205214

215+
resetApp() {
216+
this.setState({
217+
data: [],
218+
searchField: '',
219+
filteredData: [],
220+
isPlaying: false,
221+
isRecording: false,
222+
isPlayingIndex: 0,
223+
});
224+
}
225+
206226
render() {
207227
const {
208228
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)