Skip to content

Commit 3567f0a

Browse files
committed
Fix parsing payload in devpanel
1 parent f8f8c4e commit 3567f0a

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

src/browser/extension/background/messaging.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { onConnect, onMessage, sendToTab } from 'crossmessaging';
2-
import { parse } from 'circular-json';
2+
import parseJSON from '../utils/parseJSON';
33
import syncOptions from '../options/syncOptions';
44
import createMenu from './contextMenus';
55
import openDevToolsWindow from './openWindow';
@@ -22,15 +22,6 @@ onConnect((tabId) => {
2222
};
2323
}, {}, connections);
2424

25-
function parseJSON(data) {
26-
try {
27-
return parse(data);
28-
} catch (e) {
29-
// console.error(data + 'is not a valid JSON', e);
30-
return null;
31-
}
32-
}
33-
3425
// Receive message from content script and relay to the devTools page
3526
function messaging(request, sender, sendResponse) {
3627
const tabId = sender.tab ? sender.tab.id : sender.id;
@@ -63,15 +54,15 @@ function messaging(request, sender, sendResponse) {
6354
return true;
6455
}
6556

66-
const payload = typeof request.payload === 'string' ? parseJSON(request.payload) : request.payload;
57+
const payload = parseJSON(request.payload);
6758
if (!payload) return true;
6859
store.liftedStore.setState(payload);
6960
if (request.init) {
7061
store.id = tabId;
7162
createMenu(sender.url, tabId);
7263
}
7364
if (tabId in connections) {
74-
connections[ tabId ].postMessage({payload: payload});
65+
connections[tabId].postMessage({ payload: request.payload });
7566
}
7667

7768
// Notify when errors occur in the app

src/browser/extension/devpanel/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from 'react';
33
import { render } from 'react-dom';
44
import DevTools from '../../../app/containers/DevTools';
55
import createDevStore from '../../../app/store/createDevStore';
6+
import parseJSON from '../utils/parseJSON';
67

78
const backgroundPageConnection = connect();
89

@@ -39,7 +40,9 @@ backgroundPageConnection.onMessage.addListener((message) => {
3940
);
4041
rendered = false;
4142
} else if (message.payload) {
42-
store.liftedStore.setState(message.payload);
43+
const payload = parseJSON(message.payload);
44+
if (!payload) return;
45+
store.liftedStore.setState(payload);
4346
showDevTools();
4447
} else if (message.action) {
4548
dispatch(message.action);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { parse } from 'circular-json';
2+
3+
export default function parseJSON(data) {
4+
if (typeof data !== 'string') return data;
5+
try {
6+
return parse(data);
7+
} catch (e) {
8+
if (process.env.NODE_ENV !== 'production') console.error(data + 'is not a valid JSON', e);
9+
return null;
10+
}
11+
}

0 commit comments

Comments
 (0)