Extract isBrowserExtension() into separate function

parent eb2ea096
...@@ -24,15 +24,7 @@ function configFrom(window_) { ...@@ -24,15 +24,7 @@ function configFrom(window_) {
annotations: settings.annotations(window_.location.href), annotations: settings.annotations(window_.location.href),
}; };
var chromeExt = 'chrome-extension://'; if (settings.isBrowserExtension(config)) {
var mozExt = 'moz-extension://';
var edgeExt = 'ms-browser-extension://';
// If the client is injected by the browser extension, ignore
// the rest of the host page config.
if (config.app.indexOf(chromeExt) === 0 ||
config.app.indexOf(mozExt) === 0||
config.app.indexOf(edgeExt) === 0) {
return config; return config;
} }
......
...@@ -102,9 +102,28 @@ function configFuncSettingsFrom(window_) { ...@@ -102,9 +102,28 @@ function configFuncSettingsFrom(window_) {
return window_.hypothesisConfig(); return window_.hypothesisConfig();
} }
/**
* Return true if the client is from a browser extension.
*
* @returns {boolean} true if this instance of the Hypothesis client is one
* distributed in a browser extension, false if it's one embedded in a
* website.
*
*/
function isBrowserExtension(config) {
if (config.app.indexOf('chrome-extension://') === 0 ||
config.app.indexOf('moz-extension://') === 0 ||
config.app.indexOf('ms-browser-extension://') === 0) {
return true;
}
return false;
}
module.exports = { module.exports = {
app: app, app: app,
annotations: annotations, annotations: annotations,
query: query, query: query,
configFuncSettingsFrom: configFuncSettingsFrom, configFuncSettingsFrom: configFuncSettingsFrom,
isBrowserExtension: isBrowserExtension,
}; };
...@@ -28,6 +28,7 @@ describe('annotator.config', function() { ...@@ -28,6 +28,7 @@ describe('annotator.config', function() {
fakeSettings.annotations = sinon.stub().returns(null); fakeSettings.annotations = sinon.stub().returns(null);
fakeSettings.query = sinon.stub().returns(null); fakeSettings.query = sinon.stub().returns(null);
fakeSettings.configFuncSettingsFrom = sinon.stub().returns({}); fakeSettings.configFuncSettingsFrom = sinon.stub().returns({});
fakeSettings.isBrowserExtension = sinon.stub().returns(false);
}); });
it('gets the config.app setting', function() { it('gets the config.app setting', function() {
...@@ -188,39 +189,38 @@ describe('annotator.config', function() { ...@@ -188,39 +189,38 @@ describe('annotator.config', function() {
}); });
context('when the client is injected by the browser extension', function() { context('when the client is injected by the browser extension', function() {
beforeEach(function() { beforeEach('configure a browser extension client', function() {
fakeSettings.annotations.returns('SOME_ANNOTATION_ID'); fakeSettings.isBrowserExtension.returns(true);
fakeSharedSettings.jsonConfigsFrom.returns({foo: 'bar'});
}); });
it('ignores the host page config on chrome', function() { it('still reads the config.app setting from the host page', function() {
fakeSettings.app.returns('chrome-extension://abcdef'); fakeSettings.app.returns('SOME_APP_URL');
var config = configFrom(fakeWindow());
assert.equal(config.app, 'chrome-extension://abcdef'); assert.equal(configFrom(fakeWindow()).app, fakeSettings.app());
assert.equal(config.annotations, 'SOME_ANNOTATION_ID');
assert.isUndefined(config.foo);
}); });
it('ignores the host page config on firefox', function() { it('still reads the config.query setting from the host page', function() {
fakeSettings.app.returns('moz-extension://abcdef'); fakeSettings.query.returns('SOME_QUERY');
var config = configFrom(fakeWindow()); assert.equal(configFrom(fakeWindow()).query, fakeSettings.query());
});
assert.equal(config.app, 'moz-extension://abcdef'); it('still reads the config.annotations setting from the host page', function() {
assert.equal(config.annotations, 'SOME_ANNOTATION_ID'); fakeSettings.annotations.returns('SOME_ANNOTATION_ID');
assert.isUndefined(config.foo);
assert.equal(configFrom(fakeWindow()).annotations, fakeSettings.annotations());
}); });
it('ignores the host page config on edge', function() { it('ignores settings from JSON objects in the host page', function() {
fakeSettings.app.returns('ms-browser-extension://abcdef'); fakeSharedSettings.jsonConfigsFrom.returns({foo: 'bar'});
var config = configFrom(fakeWindow()); assert.isUndefined(configFrom(fakeWindow()).foo);
});
assert.equal(config.app, 'ms-browser-extension://abcdef'); it('ignores settings from the hypothesisConfig() function in the host page', function() {
assert.equal(config.annotations, 'SOME_ANNOTATION_ID'); fakeSettings.configFuncSettingsFrom.returns({foo: 'bar'});
assert.isUndefined(config.foo);
assert.isUndefined(configFrom(fakeWindow()).foo);
}); });
}); });
......
...@@ -236,4 +236,39 @@ describe('annotation.config.settings', function() { ...@@ -236,4 +236,39 @@ describe('annotation.config.settings', function() {
}); });
}); });
}); });
describe('#isBrowserExtension', function() {
[
{
url: 'chrome-extension://abcxyz',
returns: true,
},
{
url: 'moz-extension://abcxyz',
returns: true,
},
{
url: 'ms-browser-extension://abcxyz',
returns: true,
},
{
url: 'http://partner.org',
returns: false,
},
{
url: 'https://partner.org',
returns: false,
},
{
url: 'ftp://partner.org',
returns: false,
},
].forEach(function(test) {
it('returns ' + test.returns + ' for ' + test.url, function() {
assert.equal(
settings.isBrowserExtension({app: test.url}),
test.returns);
});
});
});
}); });
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