Skip to content

Commit

Permalink
Allow filtering injection types (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Aug 22, 2023
1 parent 00fe7e6 commit c964bce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<head>` section of the page:

```html
<meta name="codeium:type" content="monaco" />
```

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
<meta name="codeium:type" content="none" />
```

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)
Expand Down
43 changes: 34 additions & 9 deletions src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
},
Expand Down

0 comments on commit c964bce

Please sign in to comment.