Skip to content

Commit

Permalink
refactor: prefer using chrome.runtime extension messages
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaddock committed Jul 1, 2020
1 parent 79fdc45 commit c10005d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
14 changes: 6 additions & 8 deletions packages/metastream-app/src/components/Webview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EventEmitter } from 'events'
import { PopupWindow } from './Popup'
import { WebviewError } from './lobby/overlays/WebviewError'
import styles from './Webview.css'
import { dispatchExtensionMessage } from 'utils/extension'

const INITIALIZE_TIMEOUT_DURATION = 1000
const NAVIGATION_TIMEOUT_DURATION = 5000
Expand Down Expand Up @@ -290,14 +291,11 @@ export class Webview extends Component<Props, State> {

dispatchRemoteEvent<T>(type: string, payload?: T, options: { allFrames?: boolean } = {}): void {
if (this.tabId === -1 || this.frameId === -1) return
window.postMessage(
{
type: 'metastream-webview-event',
payload: { type, payload },
tabId: this.tabId,
frameId: options.allFrames ? undefined : this.frameId
},
location.origin

dispatchExtensionMessage(
'metastream-webview-event',
{ type, payload },
{ tabId: this.tabId, frameId: options.allFrames ? undefined : this.frameId }
)
}

Expand Down
10 changes: 6 additions & 4 deletions packages/metastream-app/src/utils/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ export const getIsInstalled = (): boolean =>
typeof document !== 'undefined' &&
typeof document.documentElement.dataset.extensionInstalled !== 'undefined'

export const dispatchExtensionMessage = (type: string, payload?: any) => {
export const dispatchExtensionMessage = (type: string, payload?: any, extra?: any) => {
if (!type.startsWith('metastream-')) {
throw new Error('Extension messages must start with metastream-')
}

const message = { type, payload }
const message = { type, payload, ...extra }

const chrome = (window as any).chrome
if (typeof chrome === 'object' && typeof chrome.runtime === 'object') {
const extensionId = document.documentElement.dataset.extensionId
chrome.runtime.sendMessage(extensionId, message)
return
if (extensionId) {
chrome.runtime.sendMessage(extensionId, message)
return
}
}

window.postMessage(message, location.origin)
Expand Down
6 changes: 2 additions & 4 deletions packages/metastream-app/src/utils/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CoreOptions, RequestResponse } from 'request'
import { dispatchExtensionMessage } from './extension'

type FetchResponse = Response & {
body: any
Expand Down Expand Up @@ -63,10 +64,7 @@ const mainFetch = (url: string, options: FetchOptions = {}): Promise<FetchRespon
}

window.addEventListener('message', handler, false)
window.postMessage(
{ type: 'metastream-fetch', payload: { requestId, url, options } },
location.origin
)
dispatchExtensionMessage('metastream-fetch', { requestId, url, options })
})
}

Expand Down

0 comments on commit c10005d

Please sign in to comment.