Skip to content

Commit

Permalink
fix(AEX-2): Fix getBrowserAPI helper for cross-browser compatibility (
Browse files Browse the repository at this point in the history
  • Loading branch information
nduchak authored May 29, 2020
1 parent 6b8e6fe commit 98b0e29
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 5 additions & 4 deletions es/utils/aepp-wallet-communication/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { isMemoryAccount } from '../../account/selector'
const isWeb = () => location && location.protocol && location.protocol.startsWith('http')

export const getBrowserAPI = (force = false) => {
const window = getWindow(force)
// Chrome, Opera support
if (chrome === Object(chrome)) return chrome
if (window && window.chrome === Object(window.chrome)) return window.chrome
// Firefox support
if (browser === Object(browser)) return browser
if (window && window.browser === Object(window.browser)) return window.browser
if (!force) throw new Error('Browser is not detected')
return {}
}
Expand All @@ -24,8 +25,8 @@ export const isContentScript = () => isExtensionContext() && isWeb()

export const isInIframe = () => window !== window.parent

export const getWindow = () => {
if (!window) throw new Error('Browser is not detected')
export const getWindow = (force = false) => {
if (!window && !force) throw new Error('Browser is not detected')
return window
}

Expand Down
4 changes: 3 additions & 1 deletion test/integration/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,11 +636,13 @@ describe('Aepp<->Wallet', function () {
})
it('getBrowserAPI: chrome', () => {
global.chrome = { runtime: {}, chrome: true }
global.window = { location: { origin: '//test' }, chrome: global.chrome }
getBrowserAPI().chrome.should.be.equal(true)
})
it('getBrowserAPI: firefox', () => {
global.chrome = null
global.browser = { runtime: {}, firefox: true }
global.window = { location: { origin: '//test' }, browser: global.browser }
getBrowserAPI().firefox.should.be.equal(true)
})
it('isInIframe/getWindow', () => {
Expand Down Expand Up @@ -673,7 +675,7 @@ describe('Aepp<->Wallet', function () {

const getConnections = (direct) => {
global.chrome = { runtime: {} }
global.window = { location: { origin: '//test' } }
global.window = { location: { origin: '//test' }, chrome: global.chrome }
global.location = { protocol: 'http://test.com' }
return getFakeConnections(direct)
}

0 comments on commit 98b0e29

Please sign in to comment.