Skip to content

Commit 6b4db6a

Browse files
authored
Add permissions.addSiteAccessRequest() sample (#1322)
* Add permissions.addSiteAccessRequest() demo Adds an example of using the permissions.addSiteAccessRequest() API to request access to a checkout page. This matches the example used in our upcoming blog post. * Add banner to show that the extension has access.
1 parent 4fb578e commit 6b4db6a

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# permissions.addSiteAccessRequest() Demo
2+
3+
This sample demonstrates using the `permissions.addSiteAccessRequest` API to request access to a site.
4+
5+
## Overview
6+
7+
This API allows you to request access to an origin listed in `optional_host_permissions` (or withheld by the user) at runtime.
8+
9+
## Running this extension
10+
11+
1. Clone this repository.
12+
2. Make sure you have the latest version of Chrome Canary installed.
13+
3. At chrome://flags, enable the "Extensions Menu Access Control" flag.
14+
4. Close Chrome Canary.
15+
5. Start Chrome Canary with the `--enable-features=ApiPermissionsSiteAccessRequests` flag.
16+
6. Load this directory as an [unpacked extension](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked).
17+
7. At chrome://extensions, click on "Details" for the extension and unselect "Automatically allow access on the following sites".
18+
8. Visit https://example.com/checkout.
19+
9. Click "Allow 1?"
20+
21+
You will see a banner injected on the page to show that the extension has run.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* Adds a site access request if the user visits https://example.com/checkout.
17+
* This could be useful for an extension that wishes to offer users coupons or
18+
* order tracking but needs access to the site to do so.
19+
*/
20+
chrome.tabs.onUpdated.addListener(async (tabId, changes) => {
21+
if (typeof changes.url !== 'string') return;
22+
23+
const url = new URL(changes.url);
24+
25+
// If we are on the /checkout page of example.com.
26+
if (url.origin === 'https://example.com' && url.pathname === '/checkout') {
27+
const hasPermission = await chrome.permissions.contains({
28+
origins: ['https://example.com/*']
29+
});
30+
31+
// We already have host permissions.
32+
if (hasPermission) {
33+
return;
34+
}
35+
36+
// Add a site access request if the API is available.
37+
if (chrome.permissions.addSiteAccessRequest) {
38+
chrome.permissions.addSiteAccessRequest({ tabId });
39+
}
40+
}
41+
});
42+
43+
chrome.permissions.onAdded.addListener((permissions) => {
44+
if (permissions.origins?.includes('https://example.com/*')) {
45+
console.log('Permission granted.');
46+
// FIXME: Run any code you wanted to run here.
47+
}
48+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const banner = document.createElement('div');
2+
banner.innerText = 'Extension has access to page.';
3+
4+
banner.style.width = '100vw';
5+
banner.style.position = 'fixed';
6+
banner.style.top = '0';
7+
banner.style.left = '0';
8+
banner.style.margin = '0';
9+
banner.style.borderRadius = '0';
10+
banner.style.padding = '20px';
11+
banner.style.background = '#4CAF50';
12+
banner.style.color = 'white';
13+
14+
document.body.prepend(banner);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "Permissions (Add Site Access Request)",
3+
"description": "Uses the `permissions.addSiteAccessRequest()` API to request access to a site.",
4+
"version": "0.3",
5+
"background": {
6+
"service_worker": "background.js"
7+
},
8+
"host_permissions": ["https://example.com/*"],
9+
"permissions": ["tabs", "scripting"],
10+
"content_scripts": [
11+
{
12+
"matches": ["https://example.com/*"],
13+
"js": ["banner.js"]
14+
}
15+
],
16+
"manifest_version": 3
17+
}

0 commit comments

Comments
 (0)