Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fake chrome.* API for Electron #152

Merged
merged 3 commits into from
Jun 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 14 additions & 28 deletions src/app/containers/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,15 @@ let initialMonitor;
let selectedTemplate;
let testTemplates;

// Electron: Not supported some chrome.* API
if (chrome.storage.local) {
chrome.storage.local.get({
['monitor' + monitorPosition]: 'InspectorMonitor',
'test-templates': null,
'test-templates-sel': null
}, options => {
initialMonitor = options['monitor' + monitorPosition];
selectedTemplate = options['test-templates-sel'];
testTemplates = options['test-templates'];
});
} else {
initialMonitor = localStorage.getItem('monitor' + monitorPosition) || 'InspectorMonitor';
selectedTemplate = localStorage.getItem('test-templates-sel');
testTemplates = localStorage.getItem('test-templates');
}

chrome.storage.local.get({
['monitor' + monitorPosition]: 'InspectorMonitor',
'test-templates': null,
'test-templates-sel': null
}, options => {
initialMonitor = options['monitor' + monitorPosition];
selectedTemplate = options['test-templates-sel'];
testTemplates = options['test-templates'];
});

@enhance
export default class App extends Component {
Expand All @@ -64,12 +56,7 @@ export default class App extends Component {
handleSelectMonitor = (event, index, value) => {
this.setState({ monitor: value });

// Electron: Not supported some chrome.* API
if (chrome.storage.local) {
chrome.storage.local.set({ ['monitor' + monitorPosition]: value });
} else {
localStorage.setItem('monitor' + monitorPosition, value);
}
chrome.storage.local.set({ ['monitor' + monitorPosition]: value });
};

handleSelectInstance = (event, index, value) => {
Expand All @@ -93,7 +80,6 @@ export default class App extends Component {
const { store } = this.props;
const instances = store.instances;
const { instance, monitor } = this.state;
const onElectron = navigator.userAgent.indexOf('Electron') !== -1;
return (
<div style={styles.container}>
<div style={styles.buttonBar}>
Expand All @@ -118,19 +104,19 @@ export default class App extends Component {
/>
}
<div style={styles.buttonBar}>
{!onElectron && monitorPosition !== 'left' &&
{!window.isElectron && monitorPosition !== 'left' &&
<Button
Icon={LeftIcon}
onClick={() => { this.openWindow('left'); }}
/>
}
{!onElectron && monitorPosition !== 'right' &&
{!window.isElectron && monitorPosition !== 'right' &&
<Button
Icon={RightIcon}
onClick={() => { this.openWindow('right'); }}
/>
}
{!onElectron && monitorPosition !== 'bottom' &&
{!window.isElectron && monitorPosition !== 'bottom' &&
<Button
Icon={BottomIcon}
onClick={() => { this.openWindow('bottom'); }}
Expand All @@ -142,7 +128,7 @@ export default class App extends Component {
<SliderButton isOpen={this.state.sliderIsOpen} onClick={this.toggleSlider} />
<ImportButton importState={store.liftedStore.importState} />
<ExportButton exportState={store.liftedStore.getState} />
{!onElectron &&
{!window.isElectron &&
<Button
Icon={RemoteIcon}
onClick={() => { this.openWindow('remote'); }}
Expand Down
37 changes: 13 additions & 24 deletions src/browser/extension/background/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,13 @@ function messaging(request, sender, sendResponse) {
return true;
}
if (request.type === 'ERROR') {
// Electron: Not supported some chrome.* API
if (chrome.notifications) {
chrome.notifications.create('app-error', {
type: 'basic',
title: 'An error occurred in the app',
message: request.message,
iconUrl: 'img/logo/48x48.png',
isClickable: false
});
}
chrome.notifications.create('app-error', {
type: 'basic',
title: 'An error occurred in the app',
message: request.message,
iconUrl: 'img/logo/48x48.png',
isClickable: false
});
return true;
}

Expand Down Expand Up @@ -155,22 +152,14 @@ function onConnect(port) {
}

chrome.runtime.onConnect.addListener(onConnect);
chrome.runtime.onConnectExternal.addListener(onConnect);
chrome.runtime.onMessage.addListener(messaging);
chrome.runtime.onMessageExternal.addListener(messaging);

// Electron: Not supported some chrome.* API
if (chrome.runtime.onConnectExternal) {
chrome.runtime.onConnectExternal.addListener(onConnect);
}
if (chrome.runtime.onMessageExternal) {
chrome.runtime.onMessageExternal.addListener(messaging);
}

if (chrome.notifications) {
chrome.notifications.onClicked.addListener(id => {
chrome.notifications.clear(id);
if (id === 'redux-error') openDevToolsWindow('devtools-right');
});
}
chrome.notifications.onClicked.addListener(id => {
chrome.notifications.clear(id);
if (id === 'redux-error') openDevToolsWindow('devtools-right');
});

export function toContentScript(type, action, id, state) {
const message = { type, action, state, id };
Expand Down
46 changes: 46 additions & 0 deletions src/browser/extension/electronMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Electron: Not supported some chrome.* API

window.isElectron = window.navigator &&
window.navigator.userAgent.indexOf('Electron') !== -1;

// Background page only
if (
window.isElectron &&
location.pathname === '/_generated_background_page.html'
) {
chrome.runtime.onConnectExternal = {
addListener() {}
};
chrome.runtime.onMessageExternal = {
addListener() {}
};
chrome.notifications = {
onClicked: {
addListener() {}
},
create() {},
clear() {}
};
}

if (window.isElectron) {
chrome.storage.local = {
set(obj, callback) {
Object.keys(obj).forEach(key => {
localStorage.setItem(key, obj[key]);
});
if (callback) {
callback();
}
},
get(obj, callback) {
const result = {};
Object.keys(obj).forEach(key => {
result[key] = localStorage.getItem(key) || obj[key];
});
if (callback) {
callback(result);
}
}
};
}
5 changes: 3 additions & 2 deletions webpack/base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import path from 'path';
import webpack from 'webpack';

const extpath = path.join(__dirname, '../src/browser/extension/');
const electronMock = `${extpath}electronMock`;

const baseConfig = (params) => ({
entry: params.input || {
background: [ `${extpath}background/index` ],
background: [ electronMock, `${extpath}background/index` ],
options: [ `${extpath}options/index` ],
window: [ `${extpath}window/index` ],
remote: [ `${extpath}window/remote` ],
devpanel: [ `${extpath}devpanel/index` ],
devpanel: [ electronMock, `${extpath}devpanel/index` ],
devtools: [ `${extpath}devtools/index` ],
content: [ `${extpath}inject/contentScript` ],
pagewrap: [ `${extpath}inject/pageScriptWrap` ],
Expand Down