Skip to content

Commit

Permalink
Add canAccessTab (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Jun 26, 2024
1 parent 92e8010 commit a1cf691
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,12 @@ async function catchTargetInjectionErrors(promise: Promise<unknown>): Promise<vo
}
}
}

export async function canAccessTab(
target: number | Target,
): Promise<boolean> {
return executeFunction(castTarget(target), () => true).then(
() => true,
() => false,
);
}
33 changes: 32 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
insertCSS,
injectContentScript,
executeFunction,
canAccessTab,
} from 'webext-content-scripts';
```

Expand Down Expand Up @@ -186,6 +187,36 @@ await executeFunction(tabId, (localCatsAndDogs) => {
}, catsAndDogs); // Argument
```

### `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');
}
```

### `isScriptableUrl(url)`

Browsers block access to some URLs for security reasons. This function will check whether a passed URL is blocked. Permissions and the manifest are not checked, this function is completely static. It will also returns `false` for any URL that doesn't start with `http`.
Expand All @@ -208,7 +239,7 @@ if (isScriptableUrl(url)) {

- [webext-tools](https://github.com/fregante/webext-tools) - Utility functions for Web Extensions.
- [webext-domain-permission-toggle](https://github.com/fregante/webext-domain-permission-toggle) - Browser-action context menu to request permission for the current tab.
- [webext-additional-permissions](https://github.com/fregante/webext-additional-permissions) - Get any optional permissions that users have granted you.
- [webext-permissions](https://github.com/fregante/webext-permissions) - Get any optional permissions that users have granted you.
- [webext-dynamic-content-scripts](https://github.com/fregante/webext-dynamic-content-scripts) - Automatically registers your content_scripts on domains added via permission.request
- [More…](https://github.com/fregante/webext-fun)

Expand Down

0 comments on commit a1cf691

Please sign in to comment.