Commit 4dff8d67 authored by Juan Corona's avatar Juan Corona Committed by Juan Corona

Pass the config object from the top to the lower sub-frame levels

This means that sub-frames will get injected with the same set of whitelisted config values from the top level.
It’s good that there’s a whitelisting of the config before reaching this point, so there’s less of a security concern with the injection

This was done so I can have the sub-frame guests read the `config.showHighlights` option in order to set the initial highlight visibility at that level.
parent 2fc675f6
...@@ -83,8 +83,7 @@ module.exports = class Guest extends Delegator ...@@ -83,8 +83,7 @@ module.exports = class Guest extends Delegator
this.anchors = [] this.anchors = []
cfOptions = cfOptions =
enableMultiFrameSupport: config.enableMultiFrameSupport config: config
embedScriptUrl: config.embedScriptUrl
on: (event, handler) => on: (event, handler) =>
this.subscribe(event, handler) this.subscribe(event, handler)
emit: (event, args...) => emit: (event, args...) =>
......
...@@ -22,6 +22,7 @@ module.exports = class CrossFrame extends Plugin ...@@ -22,6 +22,7 @@ module.exports = class CrossFrame extends Plugin
constructor: (elem, options) -> constructor: (elem, options) ->
super super
config = options.config
opts = extract(options, 'server') opts = extract(options, 'server')
discovery = new Discovery(window, opts) discovery = new Discovery(window, opts)
...@@ -36,7 +37,7 @@ module.exports = class CrossFrame extends Plugin ...@@ -36,7 +37,7 @@ module.exports = class CrossFrame extends Plugin
bridge.createChannel(source, origin, token) bridge.createChannel(source, origin, token)
discovery.startDiscovery(onDiscoveryCallback) discovery.startDiscovery(onDiscoveryCallback)
if options.enableMultiFrameSupport if config.enableMultiFrameSupport
frameObserver.observe(_injectToFrame, _iframeUnloaded); frameObserver.observe(_injectToFrame, _iframeUnloaded);
this.destroy = -> this.destroy = ->
...@@ -60,8 +61,11 @@ module.exports = class CrossFrame extends Plugin ...@@ -60,8 +61,11 @@ module.exports = class CrossFrame extends Plugin
_injectToFrame = (frame) -> _injectToFrame = (frame) ->
if !FrameUtil.hasHypothesis(frame) if !FrameUtil.hasHypothesis(frame)
# Take the embed script location from the config
# until an alternative solution comes around.
embedScriptUrl = config.embedScriptUrl
FrameUtil.isLoaded frame, () -> FrameUtil.isLoaded frame, () ->
FrameUtil.injectHypothesis(frame, options.embedScriptUrl) FrameUtil.injectHypothesis(frame, embedScriptUrl, config)
_iframeUnloaded = (frame) -> _iframeUnloaded = (frame) ->
# TODO: Bridge call here not yet implemented, placeholder for now # TODO: Bridge call here not yet implemented, placeholder for now
......
...@@ -16,6 +16,7 @@ describe 'CrossFrame', -> ...@@ -16,6 +16,7 @@ describe 'CrossFrame', ->
createCrossFrame = (options) -> createCrossFrame = (options) ->
defaults = defaults =
config: {}
on: sandbox.stub() on: sandbox.stub()
emit: sandbox.stub() emit: sandbox.stub()
element = document.createElement('div') element = document.createElement('div')
......
...@@ -39,8 +39,10 @@ describe('CrossFrame multi-frame scenario', function () { ...@@ -39,8 +39,10 @@ describe('CrossFrame multi-frame scenario', function () {
document.body.appendChild(container); document.body.appendChild(container);
options = { options = {
enableMultiFrameSupport: true, config: {
embedScriptUrl: 'data:,', // empty data uri enableMultiFrameSupport: true,
embedScriptUrl: 'data:,', // empty data uri
},
on: sandbox.stub(), on: sandbox.stub(),
emit: sandbox.stub(), emit: sandbox.stub(),
}; };
...@@ -110,7 +112,7 @@ describe('CrossFrame multi-frame scenario', function () { ...@@ -110,7 +112,7 @@ describe('CrossFrame multi-frame scenario', function () {
isLoaded(frame, function () { isLoaded(frame, function () {
var scriptElement = frame.contentDocument.querySelector('script[src]'); var scriptElement = frame.contentDocument.querySelector('script[src]');
assert(scriptElement, 'expected embed script to be injected'); assert(scriptElement, 'expected embed script to be injected');
assert.equal(scriptElement.src, options.embedScriptUrl, assert.equal(scriptElement.src, options.config.embedScriptUrl,
'unexpected embed script source'); 'unexpected embed script source');
resolve(); resolve();
}); });
......
...@@ -12,20 +12,21 @@ function hasHypothesis (iframe) { ...@@ -12,20 +12,21 @@ function hasHypothesis (iframe) {
} }
// Inject embed.js into the iframe // Inject embed.js into the iframe
function injectHypothesis (iframe, scriptUrl) { function injectHypothesis (iframe, scriptUrl, config) {
var config = document.createElement('script'); var configElement = document.createElement('script');
config.className = 'js-hypothesis-config'; configElement.className = 'js-hypothesis-config';
config.type = 'application/json'; configElement.type = 'application/json';
config.innerText = '{"enableMultiFrameSupport": true, "subFrameInstance": true}'; var configObject = Object.assign({}, config, {subFrameInstance: true});
configElement.innerText = JSON.stringify(configObject);
var src = scriptUrl; var src = scriptUrl;
var embed = document.createElement('script'); var embedElement = document.createElement('script');
embed.className = 'js-hypothesis-embed'; embedElement.className = 'js-hypothesis-embed';
embed.async = true; embedElement.async = true;
embed.src = src; embedElement.src = src;
iframe.contentDocument.body.appendChild(config); iframe.contentDocument.body.appendChild(configElement);
iframe.contentDocument.body.appendChild(embed); iframe.contentDocument.body.appendChild(embedElement);
} }
// Check if we can access this iframe's document // Check if we can access this iframe's document
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment