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 @@
import manifest from '../../build/manifest.json';
import { bootHypothesisClient, bootSidebarApp } from './boot';
import { isBrowserSupported } from './browser-check';
import { getExtensionId, hasExtensionConfig } from './browser-extension-utils';
import { parseJsonConfig } from './parse-json-config';
import { processUrlTemplate } from './url-template';
......@@ -22,12 +23,10 @@ if (isBrowserSupported()) {
parseJsonConfig(document)
);
// When the boot script is executed from the browser extension, at least one
// config is required
const isExtensionContext = !!(
/** @type {any} */ (window).chrome?.runtime?.id
);
if (!Object.keys(config).length && isExtensionContext) {
// When the boot script is executed from the browser extension, a config
// generated by that specific extension is required
const extensionId = getExtensionId();
if (extensionId && !hasExtensionConfig(extensionId)) {
throw new Error(
'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