diff --git a/README.md b/README.md index 868b934..c207088 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,20 @@ _Free, ultrafast code autocomplete for Chrome_ - [Quadratic](https://www.quadratichq.com/) - [StackBlitz](https://stackblitz.com/) +In addition, any web page can support autocomplete in editors by adding the following meta tag to the `` section of the page: + +```html + +``` + +The `content` attribute accepts a comma-separated list of supported editors. These currently include: `"monaco"` and `"codemirror5"`. + +To disable the extension in a specific page add the following meta tag: + +```html + +``` + Contributions are welcome! Feel free to submit pull requests and issues related to the extension or to add links to supported websites. 🔗 [Original Chrome extension launch announcement](https://codeium.com/blog/codeium-chrome-extension-launch) diff --git a/src/script.ts b/src/script.ts index 9cdc28b..f61b0f5 100644 --- a/src/script.ts +++ b/src/script.ts @@ -225,15 +225,40 @@ const addCodeMirror5LocalInject = () => { getAllowlist(extensionId).then( (allowlist) => { - for (const addr of computeAllowlist(allowlist)) { - const host = new RegExp(addr); - if (host.test(window.location.href) || document.querySelector('meta[name="codeium:type"]')) { - // the url matches the allowlist or meta tag is found - // TODO: restrict injection type by the value of the content attribute (comma-separated list). see https://github.com/Exafunction/codeium-chrome/issues/28 - addMonacoInject(); - addCodeMirror5GlobalInject(); - addCodeMirror5LocalInject(); - return; + const validInjectTypes = ['monaco', 'codemirror5', 'none']; + const metaTag = document.querySelector('meta[name="codeium:type"]'); + const injectionTypes = + metaTag + ?.getAttribute('content') + ?.split(',') + .map((x) => x.toLowerCase().trim()) + .filter((x) => validInjectTypes.includes(x)) ?? []; + + if (injectionTypes.includes('none')) { + // do not inject if specifically disabled + return; + } + + if (injectionTypes.includes('monaco')) { + addMonacoInject(); + } + + if (injectionTypes.includes('codemirror5')) { + addCodeMirror5GlobalInject(); + addCodeMirror5LocalInject(); + } + + if (injectionTypes.length === 0) { + // if no meta tag is found, check the allowlist + for (const addr of computeAllowlist(allowlist)) { + const host = new RegExp(addr); + if (host.test(window.location.href)) { + // the url matches the allowlist + addMonacoInject(); + addCodeMirror5GlobalInject(); + addCodeMirror5LocalInject(); + return; + } } } },