npmclub-permission-toggle

webext-permission-toggle npm version

Context menu

WebExtension module: Browser-action context menu to request permission for the current tab.

This package was recently renamed from webext-domain-permission-toggle to webext-permission-toggle

Install

use npm:

npm install webext-permission-toggle
import addPermissionToggle from 'webext-permission-toggle';

Usage

// In background.js
addPermissionToggle();

manifest.json v3

// example background.worker.js
navigator.importScripts(
	"webext-permission-toggle.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

{
	"version": 2,
	"browser_action": { /* Firefox support */
		"default_icon": "icon.png"
	},
	"permissions": [
		"contextMenus",
		"activeTab"
	],
	"optional_permissions": [
		"*://*/*"
	],
	"background": {
		"scripts": [
			"webext-permission-toggle.js",
			"background.js"
		]
	}
}

API

addPermissionToggle([options])

Context menu

Adds an item to the browser action icon’s context menu (as shown in the screenshot).

The user can access this menu by right clicking the icon. If your extension doesn’t have any action or popup assigned to the icon, it will also appear with a left click.

options

title

Type: string

Default: 'Enable ${extensionName} on this domain'

The title of the action in the context menu.

reloadOnSuccess

Reload confirmation message

Type: boolean string

Default: false

If true or string, when the user accepts the new permission, they will be asked to reload the current tab. Set a string to customize the message or true use the default message: Do you want to reload this page to apply ${extensionName}?