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

Add support for Manifest v3 #28

Merged
merged 9 commits into from
Jan 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions demo/background.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
import addDomainPermissionToggle from '..';

addDomainPermissionToggle();

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (!tab.url) {
console.log('No access to tab', tabId);
return;
}

console.log('Access to tab', tabId, tab.url);
chrome.scripting.executeScript({
target: {tabId},
function() {
document.body.style.backgroundColor = 'yellow';
console.log('chrome.tabs.onUpdated was fired');
},
});
});
16 changes: 9 additions & 7 deletions demo/manifest.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{
"$schema": "https://json.schemastore.org/chrome-manifest",
"name": "webext-domain-permission-toggle",
"version": "0.0.0",
"manifest_version": 2,
"manifest_version": 3,
"permissions": [
"https://*.github.com/*",
"activeTab",
"contextMenus"
],
"browser_action": {},
"optional_permissions": [
"host_permissions": [
"https://*.github.com/*"
],
"action": {},
"optional_host_permissions": [
"*://*/*"
],
"background": {
"scripts": [
"background.js"
]
"service_worker": "background.js",
"type": "module"
}
}
6 changes: 6 additions & 0 deletions globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare namespace chrome.runtime {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- Must match the type on DefinitelyTyped
interface ManifestV3 extends ManifestBase {
optional_host_permissions?: string[] | undefined;
}
}
26 changes: 18 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ export default function addDomainPermissionToggle(options?: Options): void {
throw new Error('webext-domain-permission-toggle can only be initialized once');
}

const {name, permissions, optional_permissions: optionalPermissions} = chrome.runtime.getManifest();
const manifest = chrome.runtime.getManifest();

if (!permissions?.includes('contextMenus')) {
if (!manifest.permissions?.includes('contextMenus')) {
throw new Error('webext-domain-permission-toggle requires the `contextMenus` permission');
}

Expand All @@ -151,27 +151,37 @@ export default function addDomainPermissionToggle(options?: Options): void {
}

globalOptions = {
title: `Enable ${name} on this domain`,
title: `Enable ${manifest.name} on this domain`,
reloadOnSuccess: false,
...options,
};

if (globalOptions.reloadOnSuccess === true) {
globalOptions.reloadOnSuccess = `Do you want to reload this page to apply ${name}?`;
globalOptions.reloadOnSuccess = `Do you want to reload this page to apply ${manifest.name}?`;
}

const optionalHosts = optionalPermissions?.filter(permission => /<all_urls>|\*/.test(permission));
if (!optionalHosts || optionalHosts.length === 0) {
throw new TypeError('webext-domain-permission-toggle requires some wildcard hosts to be specified in `optional_permissions`');
const optionalHosts = [
...manifest.optional_permissions ?? [],
...manifest.optional_host_permissions as string[] ?? [],
].filter((permission: string) => permission === '<all_urls>' || permission.includes('*'));

if (optionalHosts.length === 0) {
throw new TypeError('webext-domain-permission-toggle requires some wildcard hosts to be specified in `optional_permissions` or `optional_host_permissions` (MV3)');
}

// Remove any existing context menu item and silence any error
chrome.contextMenus.remove(contextMenuId, () => chrome.runtime.lastError);

const contexts: chromeP.contextMenus.ContextType[] = 'browser_action' in chrome
? ['page_action', 'browser_action']
: ['action'];

chrome.contextMenus.create({
id: contextMenuId,
type: 'checkbox',
checked: false,
title: globalOptions.title,
contexts: ['page_action', 'browser_action'],
contexts,

// Note: This is completely ignored by Chrome and Safari. Great. #14
documentUrlPatterns: optionalHosts,
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webext-domain-permission-toggle",
"version": "4.0.1",
"version": "4.1.0-1",
"description": "Browser-action context menu to request permission for the current tab. Chrome, Firefox, Safari.",
"keywords": [
"browser",
Expand Down Expand Up @@ -40,8 +40,6 @@
"webextensions"
],
"rules": {
"@typescript-eslint/no-implicit-any-catch": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"no-alert": "off"
}
},
Expand Down
36 changes: 34 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

<img width="331" alt="Context menu" src="https://user-images.githubusercontent.com/1402241/32874388-e0c64150-cacc-11e7-9a50-eae3727fd3c2.png" align="right">

> WebExtension module: Browser-action context menu to request permission for the current tab. Chrome, Firefox, Safari.
> WebExtension module: Browser-action context menu to request permission for the current tab.

- Browsers: Chrome, Firefox, and Safari
- Manifest: v2 and v3

Works great when paired with [webext-dynamic-content-scripts](https://github.com/fregante/webext-dynamic-content-scripts/blob/master/how-to-add-github-enterprise-support-to-web-extensions.md) if you want to also inject content scripts on the new domains.

Expand All @@ -29,10 +32,39 @@ import addDomainPermissionToggle from 'webext-domain-permission-toggle';
addDomainPermissionToggle();
```

### manifest.json
### manifest.json v3

```js
// example background.worker.js
navigator.importScripts(
"webext-domain-permission-toggle.js"
)
```
```js
{
"version": 3,
"action": { /* Firefox support */
"default_icon": "icon.png"
},
"permissions": [
"contextMenus",
"activeTab",
"scripting",
],
"optional_host_permissions": [
"*://*/*"
],
"background": {
"service_worker": "background.worker.js"
}
}
```

### manifest.json v2

```js
{
"version": 2,
"browser_action": { /* Firefox support */
"default_icon": "icon.png"
},
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "@sindresorhus/tsconfig",
"files": [
"index.ts"
"index.ts",
"globals.d.ts"
]
}