Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filtering injection types #32

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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