webext-tools
Advanced tools
Comparing version 1.2.5 to 2.0.0
@@ -7,4 +7,4 @@ export type Target = { | ||
export declare function getTabUrl(target: number | Target): Promise<string | undefined>; | ||
export declare function canAccessTab(target: number | Target): Promise<boolean>; | ||
export declare function doesTabExist(tabId: number): Promise<boolean>; | ||
export declare function setActionPopup(getPopupUrl: (tabUrl: string | undefined) => Promise<string | undefined> | string | undefined): void; | ||
export declare function addOptionsContextMenu(): void; |
30
index.js
import chromeP from 'webext-polyfill-kinda'; | ||
import { executeFunction } from 'webext-content-scripts'; | ||
import { isChrome } from 'webext-detect-page'; | ||
export function castTarget(target) { | ||
@@ -27,5 +28,2 @@ return typeof target === 'object' | ||
} | ||
export async function canAccessTab(target) { | ||
return executeFunction(castTarget(target), () => true).then(() => true, () => false); | ||
} | ||
/** Utility to await promises where you only care whether they throw or not */ | ||
@@ -77,1 +75,27 @@ async function isPromiseFulfilled(promise) { | ||
} | ||
const optionsShortcut = 'WEBEXT_TOOLS_OPTIONS'; | ||
function onContextMenuClick({ menuItemId }) { | ||
if (menuItemId === optionsShortcut) { | ||
void chrome.runtime.openOptionsPage(); | ||
} | ||
} | ||
export function addOptionsContextMenu() { | ||
if (isChrome()) { | ||
return; | ||
} | ||
if (!(chrome.action ?? chrome.browserAction)) { | ||
console.warn('Add `action` or `browser_action` to your manifest to enable `addOptionsContextMenu`.'); | ||
return; | ||
} | ||
if (!chrome.contextMenus) { | ||
// Silently ignore if the API is not available, like in Firefox Android | ||
// https://github.com/fregante/webext-permission-toggle/pull/53 | ||
return; | ||
} | ||
chrome.contextMenus.onClicked.addListener(onContextMenuClick); | ||
chrome.contextMenus.create({ | ||
id: optionsShortcut, | ||
title: 'Options…', | ||
contexts: 'action' in chrome ? ['action'] : ['browser_action'], | ||
}); | ||
} |
{ | ||
"name": "webext-tools", | ||
"version": "1.2.5", | ||
"version": "2.0.0", | ||
"description": "Utility functions for Web Extensions, manifest v2 and v3", | ||
@@ -45,5 +45,5 @@ "keywords": [ | ||
"devDependencies": { | ||
"@sindresorhus/tsconfig": "^5.0.0", | ||
"@sindresorhus/tsconfig": "^6.0.0", | ||
"@types/chrome": "^0.0.268", | ||
"typescript": "^5.4.5", | ||
"typescript": "^5.5.2", | ||
"xo": "^0.58.0" | ||
@@ -50,0 +50,0 @@ }, |
@@ -22,3 +22,6 @@ # webext-tools [![npm version](https://img.shields.io/npm/v/webext-tools.svg)](https://www.npmjs.com/package/webext-tools) | ||
// This module is only offered as a ES Module | ||
import {getTabUrl, canAccessTab} from 'webext-tools'; | ||
import { | ||
getTabUrl, | ||
addOptionsContextMenu, | ||
} from 'webext-tools'; | ||
``` | ||
@@ -55,32 +58,2 @@ | ||
### `canAccessTab(tabId)` | ||
### `canAccessTab({tabId, frameId})` | ||
Checks whether the extension has access to a specific tab or frame (i.e. content scripts are allowed to run), either via `activeTab` permission or regular host permissions. | ||
```js | ||
const tabId = 42; | ||
const access = await canAccessTab(tabId); | ||
if (access) { | ||
console.log('We can access this tab'); | ||
chrome.tabs.executeScript(tabId, {file: 'my-script.js'}); | ||
} else { | ||
console.warn('We have no access to the tab'); | ||
} | ||
``` | ||
```js | ||
const access = await canAccessTab({ | ||
tabId: 42, | ||
frameId: 56, | ||
}); | ||
if (access) { | ||
console.log('We can access this frame'); | ||
chrome.tabs.executeScript(42, {file: 'my-script.js', frameId: 56}); | ||
} else { | ||
console.warn('We have no access to the frame'); | ||
} | ||
``` | ||
### `doesTabExist(tabId)` | ||
@@ -117,2 +90,13 @@ | ||
### `addOptionsContextMenu()` | ||
Chrome lets user reach the options via browser action context menu. However in Safari and Firefox you need a few more clicks to reach it. | ||
This helper will automatically add an "Options…" menu item to the browser action context menu to both Safari and Firefox. Nothing happens in Chrome. | ||
Requirements: | ||
- `action` or `browser_action` specified in your manifest.json | ||
- `contextMenu` permission | ||
## Related | ||
@@ -119,0 +103,0 @@ |
9285
108
111