Skip to content

Commit

Permalink
Allow to send and receive postMessage from all origins
Browse files Browse the repository at this point in the history
This allows us to send and receive focusElement and focusElementEditor messages from the preview window to the elements window and vice versa.
  • Loading branch information
tvdeyen committed Nov 2, 2020
1 parent 6c02503 commit 966ef50
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ Alchemy.ElementEditors =
@onSaveElement(e, data)
# Listen to postMessage messages from the preview frame
window.addEventListener 'message', (e) =>
if e.origin == window.location.origin
@onMessage(e.data)
else
console.warn 'Unsafe message origin!', e.origin
@onMessage(e.data)
true
return

Expand Down
3 changes: 0 additions & 3 deletions app/assets/javascripts/alchemy/alchemy.preview.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ Alchemy.initAlchemyPreviewMode = ->

init: ->
window.addEventListener "message", (event) =>
if event.origin != window.location.origin
console.warn 'Unsafe message origin!', event.origin
return
switch event.data.message
when "Alchemy.blurElements" then @blurElements()
when "Alchemy.focusElement" then @focusElement(event.data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Alchemy.PreviewWindow =

postMessage: (data) ->
frameWindow = @currentWindow[0].contentWindow
frameWindow.postMessage(data, window.location.origin)
frameWindow.postMessage(data, "*")

_showSpinner: ->
@reload = $('#reload_preview_button')
Expand Down

0 comments on commit 966ef50

Please sign in to comment.