Skip to content

Commit 9c3c468

Browse files
victorvrvkiacolbert
authored andcommitted
Victor dev (#29)
* commit to handle merges * Delete devtools_bundle.js * Delete devtools_bundle.js.map * added doubly linked list data structure * Added buttons back and forth time travel functionality * Merge last PR with the current chrome dev tools version. * removed console.log from extension * deleted console.log * Minor change to background, extension and inject_script. Commiting before pulling parser functionality * Connecting parser and chrome dev tools. Code is full of console.logs. * Connected dev tools with page. Injected script working, parsing working, time travel working * fixed wrong type for expected Number on TimeSlider * Created long lived port between App and background. This is gonna be user for launching the devtool only on specific pages * Added turn on button * Update App.jsx
1 parent d131cce commit 9c3c468

File tree

7 files changed

+562
-183
lines changed

7 files changed

+562
-183
lines changed

.DS_Store

-6 KB
Binary file not shown.

package-lock.json

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

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818
"babel-eslint": "^10.0.1",
1919
"eslint": "^5.14.1",
2020
"eslint-plugin-import": "^2.16.0",
21-
"eslint-plugin-react": "^7.12.4"
21+
"eslint-plugin-react": "^7.12.4",
22+
"watchify": "^3.11.1"
2223
},
2324
"dependencies": {
2425
"escodegen": "^1.11.1",
2526
"eslint-config-airbnb": "^17.1.0",
2627
"eslint-plugin-jsx-a11y": "^6.2.1",
2728
"esprima": "^4.0.1",
2829
"estraverse": "^4.2.0",
29-
"lodash": "^4.17.11",
30-
"react": "^16.8.1",
31-
"react-dom": "^16.8.1"
30+
"lodash": "^4.17.11"
3231
}
3332
}

src/app/components/App.jsx

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class App extends Component {
4747
isPlayingIndex: 0,
4848
};
4949

50-
this.port = null;
50+
this.portToExtension = null;
51+
5152
this.addActionToView = this.addActionToView.bind(this);
5253
this.toTheFuture = this.toTheFuture.bind(this);
5354
this.toThePast = this.toThePast.bind(this);
@@ -59,15 +60,10 @@ class App extends Component {
5960
}
6061

6162
componentDidMount() {
62-
// *******************************************************
63-
// need to impletement setState for filteredData to same value as data
64-
// this.setState({ data, filteredData: data });
65-
// *******************************************************
66-
6763
// adds listener to the effects that are gonna be sent from
6864
// our edited useReducer from the 'react' library.
6965
chrome.runtime.onConnect.addListener((portFromExtension) => {
70-
this.port = portFromExtension;
66+
this.portToExtension = portFromExtension;
7167

7268
portFromExtension.onMessage.addListener((msg) => {
7369
const newData = {
@@ -76,11 +72,11 @@ class App extends Component {
7672
id: this.state.data.length,
7773
};
7874
this.setState((state) => ({
79-
data: [...state.data, newData]
75+
data: [...state.data, newData],
76+
filteredData: [...state.data, newData],
8077
}));
8178
});
8279
});
83-
}
8480

8581
// functionality to change 'play' button to 'stop'
8682
setIsPlaying() {
@@ -103,6 +99,18 @@ class App extends Component {
10399
this.setState(state => ({
104100
isRecording: !state.isRecording,
105101
}));
102+
103+
// we query the active window so we can send it to the background script
104+
// so it knows on which URL to run our devtool.
105+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
106+
const { url } = tabs[0];
107+
108+
// backgroundPort is a variable made avaiable by the devtools.js
109+
backgroundPort.postMessage({
110+
turnOnDevtool: true,
111+
url,
112+
});
113+
});
106114
}
107115

108116
actionInPlay() {
@@ -170,17 +178,17 @@ class App extends Component {
170178

171179
// function to travel to the FUTURE
172180
toTheFuture() {
173-
if (!this.port) return console.error('No connection on stored port.');
174-
this.port.postMessage({
181+
if (!this.portToExtension) return console.error('No connection on stored port.');
182+
this.portToExtension.postMessage({
175183
type: 'TIMETRAVEL',
176184
direction: 'forward',
177185
});
178186
}
179187

180188
// function to travel to the PAST
181189
toThePast() {
182-
if (!this.port) return console.error('No connection on stored port.');
183-
this.port.postMessage({
190+
if (!this.portToExtension) return console.error('No connection on stored port.');
191+
this.portToExtension.postMessage({
184192
type: 'TIMETRAVEL',
185193
direction: 'backwards',
186194
});

src/browser/chrome/background.js

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,58 @@ chrome.tabs.onUpdated.addListener((id, info, tab) => {
1010
});
1111
});
1212

13-
let reqIndex = 0;
14-
const urlCache = {};
15-
chrome.webRequest.onBeforeRequest.addListener(
16-
(request) => {
17-
if (request.type === 'script' && !request.url.startsWith('chrome')
18-
&& request.frameId === 0) {
19-
// TODO: adjust comment
20-
// Else we need to check wether or not this contains the react
21-
// library. If it does, we need to send the edit javascript to
22-
// out content script, so it can inject into the page. If it doesnt,
23-
// we need to send the url to our content script so that it can
24-
// add it to the page <script src=URL> AND add it to our cache, so
25-
// that when we intercept it, we dont block it.
26-
const syncRequest = new XMLHttpRequest();
27-
syncRequest.open('GET', request.url, false);
28-
syncRequest.send(null);
29-
console.log(`Status: ${syncRequest.status} - Size of response: ${syncRequest.responseText.length}`);
30-
31-
sendMessageToContent(parseAndGenerate(syncRequest.responseText));
32-
33-
return { redirectUrl: 'javascript:' };
34-
}
35-
},
36-
{ urls: ['<all_urls>'] },
37-
['blocking'],
38-
);
3913

14+
let interceptedUrl = '';
15+
function handleRequest(request) {
16+
// TODO: filter the request from the webRequest call.
17+
if (!interceptedUrl.startsWith(request.initiator)) return { cancel: false };
18+
19+
console.log('intercepting... ', request);
20+
if (request.type === 'script' && !request.url.startsWith('chrome')
21+
&& request.frameId === 0) {
22+
// TODO: adjust comment
23+
// Else we need to check wether or not this contains the react
24+
// library. If it does, we need to send the edit javascript to
25+
// out content script, so it can inject into the page. If it doesnt,
26+
// we need to send the url to our content script so that it can
27+
// add it to the page <script src=URL> AND add it to our cache, so
28+
// that when we intercept it, we dont block it.
29+
const syncRequest = new XMLHttpRequest();
30+
syncRequest.open('GET', request.url, false);
31+
syncRequest.send(null);
32+
console.log(`Status: ${syncRequest.status} - Size of response: ${syncRequest.responseText.length}`);
33+
34+
sendMessageToContent(parseAndGenerate(syncRequest.responseText));
35+
36+
return { redirectUrl: 'javascript:' };
37+
}
38+
}
39+
40+
// The App on the devtools panel start a connection so that it can
41+
// tell us when to start intercepting the script requests.
42+
chrome.runtime.onConnect.addListener((port) => {
43+
port.onMessage.addListener((msg) => {
44+
if (!msg.turnOnDevtool) return;
45+
interceptedUrl = msg.url;
46+
addScriptInterception();
47+
48+
// after activating our interception script, we refresh the active tab
49+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
50+
chrome.tabs.update(tabs[0].id, { url: tabs[0].url });
51+
});
52+
});
53+
});
54+
55+
function addScriptInterception() {
56+
chrome.webRequest.onBeforeRequest.removeListener(handleRequest);
57+
chrome.webRequest.onBeforeRequest.addListener(
58+
handleRequest,
59+
{ urls: ['<all_urls>'] },
60+
['blocking'],
61+
);
62+
}
63+
64+
let reqIndex = 0;
4065
function sendMessageToContent(codeString) {
4166
const index = reqIndex++;
4267
console.log(`Sending request ${index}.`);

src/browser/chrome/devtools.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
chrome.devtools.panels.create('React Rewind', null, 'devtools.html');
1+
chrome.devtools.panels.create('React Rewind',
2+
null,
3+
'devtools.html',
4+
(extensionPanel) => {
5+
const port = chrome.runtime.connect({ name: 'devtools' });
6+
port.onMessage.addListener(() => {});
7+
extensionPanel.onShown.addListener((panelWindow) => {
8+
panelWindow.backgroundPort = port;
9+
});
10+
});

src/browser/chrome/manifest.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "React Rewind",
3+
"version": "1.0",
4+
"description": "Time travel",
5+
"manifest_version": 2,
6+
"page_action": {
7+
"default_popup": "page_action.html"
8+
},
9+
"background": {
10+
"scripts": ["devtools_bundle/bg_bundle.js"]
11+
},
12+
"content_scripts": [
13+
{
14+
"matches": ["<all_urls>"],
15+
"js": ["scripts/inject_script_tags.js"],
16+
"run_at":"document_start"
17+
}
18+
],
19+
"permissions": [
20+
"<all_urls>",
21+
"tabs",
22+
"storage",
23+
"webRequest",
24+
"webRequestBlocking"
25+
],
26+
"web_accessible_resources": [
27+
"scripts/linked_list.js",
28+
"scripts/time_travel.js"
29+
],
30+
"devtools_page": "devtools.html"
31+
}

0 commit comments

Comments
 (0)