Commit 97686411 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Improve how we detect if the boot script is run by certain browser extension

parent e186dc8b
/**
* Tries to get the extension ID which is running this script, or undefined if
* this script is not being run by the browser extension
*
* @param {Window} window_
* @return {string | undefined}
*/
export function getExtensionId(window_ = window) {
return /** @type {any} */ (window_).chrome?.runtime?.id;
}
/**
* Verifies if there's a config script in the document that was generated by a
* specific extension
*
* @param {string} extensionId
* @param {Document} document_
* @return {boolean}
*/
export function hasExtensionConfig(extensionId, document_ = document) {
return !!document_.querySelector(
`script.js-hypothesis-config[data-extension-id=${extensionId}]`
);
}
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
import manifest from '../../build/manifest.json'; import manifest from '../../build/manifest.json';
import { bootHypothesisClient, bootSidebarApp } from './boot'; import { bootHypothesisClient, bootSidebarApp } from './boot';
import { isBrowserSupported } from './browser-check'; import { isBrowserSupported } from './browser-check';
import { getExtensionId, hasExtensionConfig } from './browser-extension-utils';
import { parseJsonConfig } from './parse-json-config'; import { parseJsonConfig } from './parse-json-config';
import { processUrlTemplate } from './url-template'; import { processUrlTemplate } from './url-template';
...@@ -22,12 +23,10 @@ if (isBrowserSupported()) { ...@@ -22,12 +23,10 @@ if (isBrowserSupported()) {
parseJsonConfig(document) parseJsonConfig(document)
); );
// When the boot script is executed from the browser extension, at least one // When the boot script is executed from the browser extension, a config
// config is required // generated by that specific extension is required
const isExtensionContext = !!( const extensionId = getExtensionId();
/** @type {any} */ (window).chrome?.runtime?.id if (extensionId && !hasExtensionConfig(extensionId)) {
);
if (!Object.keys(config).length && isExtensionContext) {
throw new Error( throw new Error(
'Could not start Hypothesis extension as configuration is missing' 'Could not start Hypothesis extension as configuration is missing'
); );
......
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