Skip to content

Commit 0b7962f

Browse files
committed
Keeps conexion open from App to the original extension script on every refresh
1 parent ba07383 commit 0b7962f

File tree

6 files changed

+46
-52
lines changed

6 files changed

+46
-52
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
"eslint-config-airbnb": "^17.1.0",
2727
"eslint-plugin-jsx-a11y": "^6.2.1",
2828
"esprima": "^4.0.1",
29-
"estraverse": "^4.2.0",
30-
"lodash": "^4.17.11",
31-
"lodash.clonedeep": "^4.5.0"
29+
"estraverse": "^4.2.0"
3230
}
3331
}

src/app/components/App.jsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class App extends Component {
7171
// adds listener to the effects that are gonna be sent from
7272
// our edited useReducer from the 'react' library.
7373
chrome.runtime.onConnect.addListener((port) => {
74-
if (port.name !== 'injected-app') return;
74+
if (port.name !== 'injected-app' || this.portToExtension) return;
7575

7676
this.portToExtension = port;
7777

@@ -90,7 +90,7 @@ class App extends Component {
9090
// search field
9191
const { searchField } = this.state;
9292
const newDataActionType = newData.action.type.toLowerCase();
93-
93+
9494
// get the date everytime an action fires and add it to state
9595

9696
const eventTime = Date.now();
@@ -140,18 +140,18 @@ class App extends Component {
140140
}
141141

142142
setIsRecording() {
143-
// This variable will prevent the app from refreshing when we refresh
144-
// the userpage.
145-
this.justStartedRecording = true;
146-
const { isRecording, hasInjectedScript } = this.state;
143+
const { isRecording } = this.state;
147144
this.setState(state => ({
148145
isRecording: !state.isRecording,
149146
}));
150147

151148
// if we are hitting the pause or re-starting the record session
152-
if (isRecording || hasInjectedScript) return;
149+
if (isRecording || this.hasInjectedScript) return;
153150

154-
this.setState({ hasInjectedScript: true });
151+
// This variable will prevent the app from refreshing when we refresh
152+
// the userpage.
153+
this.justStartedRecording = true;
154+
this.hasInjectedScript = true;
155155

156156
// we query the active window so we can send it to the background script
157157
// so it knows on which URL to run our devtool.
@@ -260,6 +260,7 @@ class App extends Component {
260260
if (isPlayingIndex === 0) return;
261261

262262
if (!this.portToExtension) return console.error('No connection on stored port.');
263+
console.log('Sending timetravel PAST to extension');
263264
this.portToExtension.postMessage({
264265
type: 'TIMETRAVEL',
265266
direction: 'backwards',
@@ -278,11 +279,12 @@ class App extends Component {
278279

279280
resetApp() {
280281
if (this.justStartedRecording) {
281-
console.log('not reseting...');
282282
this.justStartedRecording = false;
283283
return;
284284
}
285-
console.log('reseting...');
285+
286+
this.justStartedRecording = false;
287+
this.hasInjectedScript = false;
286288
this.setState({
287289
data: [],
288290
searchField: '',
@@ -294,6 +296,7 @@ class App extends Component {
294296
action: {},
295297
state: {},
296298
prevState: {},
299+
eventTimes: [],
297300
});
298301
}
299302

src/browser/chrome/background.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const parseAndGenerate = require('./scripts/parser');
22
const injectBundleStr = require('./scripts/inject_bundle');
33

44
let ports = [];
5-
let interceptedUrl = '';
65
let reqIndex = 0;
6+
const interceptedURLs = {};
77

88
chrome.tabs.onUpdated.addListener((id, info, tab) => {
99
if (tab.status !== 'complete' || tab.url.startsWith('chrome')) return;
@@ -15,19 +15,26 @@ chrome.tabs.onUpdated.addListener((id, info, tab) => {
1515
runAt: 'document_end',
1616
});
1717

18-
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
19-
interceptedUrl = '';
18+
if (interceptedURLs.hasOwnProperty(tab.url)) {
19+
delete interceptedURLs[tab.url];
2020
reqIndex = 0;
21-
notifyPorts(
22-
{ action: 'refresh_devtool', tabId: tabs[0].id },
23-
'devtools',
24-
);
25-
});
21+
}
22+
23+
notifyPorts(
24+
{ action: 'refresh_devtool', tabId: tab.id },
25+
'devtools',
26+
);
27+
2628
});
2729

2830
function handleRequest(request) {
2931
// TODO: filter the request from the webRequest call.
30-
if (!interceptedUrl.startsWith(request.initiator)) return { cancel: false };
32+
// We check wether or not the URL should have its requests intercepted
33+
let shouldInterceptUrl = false;
34+
Object.keys(interceptedURLs).forEach((url) => {
35+
if (url.startsWith(request.initiator)) shouldInterceptUrl = true;
36+
});
37+
if (!shouldInterceptUrl) return { cancel: false };
3138

3239
if (request.type === 'script' && !request.url.startsWith('chrome')
3340
&& request.frameId === 0 && ((request.url.slice(-3) === '.js')
@@ -57,18 +64,15 @@ chrome.runtime.onConnect.addListener((port) => {
5764
if (ports) ports.push(port);
5865

5966
port.onMessage.addListener((msg) => {
60-
if (msg.turnOnDevtool) {
61-
interceptedUrl = msg.url;
62-
addScriptInterception();
63-
64-
// after activating our interception script, we refresh the active tab
65-
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
66-
chrome.tabs.update(tabs[0].id, { url: tabs[0].url });
67-
});
68-
69-
} else {
70-
console.log('Got a msg not turnOnDevtool: ', msg);
71-
}
67+
if (!msg.turnOnDevtool) return;
68+
console.log('got turn on: ', msg);
69+
interceptedURLs[msg.url] = true;
70+
addScriptInterception();
71+
72+
// after activating our interception script, we refresh the active tab
73+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
74+
chrome.tabs.update(tabs[0].id, { url: tabs[0].url });
75+
});
7276
});
7377
});
7478

src/browser/chrome/extension.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,13 @@
66
port.onMessage.addListener((msg) => {
77
// This is where we get messages from the App component.
88
// We get an object { type: 'TIMETRAVEL', direction: 'forward' }
9+
console.log('Got msg to timetravel: ', msg);
910
window.postMessage(msg);
1011
});
1112

12-
port.onDisconnect.addListener(() => console.log('Disconecting...'));
13-
1413
window.addEventListener('message', (msg) => {
15-
// TODO: fix comments. Are we gonna receive msgs from reactDOM here??
16-
// When our injected scripts post messages (both from the 'react'
17-
// and 'react-dom'), we receive it here and send it to our app loaded
18-
// on the DevTool. If storage.isAppTurnedOff is false, it means that
19-
// the user started the application, but stopped recording. So even
20-
// though our injected scripts keep posting messages, we don't want to
21-
// send them over to the App anymore.
22-
chrome.storage.sync.get(['isAppTurnedOn'], (status) => {
23-
// if (!status.isAppTurnedOn) return;
24-
if (msg.data.type === 'DISPATCH') port.postMessage(msg.data.data);
25-
});
14+
// When our injected scripts post messages (from useRedute in 'react'),
15+
// we receive it here and send it to our app loaded on the DevTool.
16+
if (msg.data.type === 'DISPATCH') port.postMessage(msg.data.data);
2617
});
2718
}());

src/browser/chrome/manifest.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@
2121
"tabs",
2222
"storage",
2323
"webRequest",
24-
"webRequestBlocking",
25-
"webNavigation"
24+
"webRequestBlocking"
2625
],
2726
"web_accessible_resources": [
28-
"scripts/linked_list.js",
29-
"scripts/time_travel.js",
3027
"scripts/deepclone_bundle.js"
3128
],
3229
"devtools_page": "devtools.html"

src/browser/chrome/page_action.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
</head>
99
<body>
1010
Page Action
11-
<button id="color_btn">CHANGE COLOR</button>
11+
<a href="http://www.google.com">Test - goto google</a>
12+
<button id="color_btn">CHANGE COLOR!</button>
1213
<script src="page_action.js"></script>
1314
</body>
1415
</html>

0 commit comments

Comments
 (0)