Skip to content

Commit

Permalink
Privacy mv3 sample (#956)
Browse files Browse the repository at this point in the history
* Implemented mv3 privacy API sample

* Added mv3 privacy API sample

* Expanded sample and updated readme

* Update service-worker.js
  • Loading branch information
IanStanion-google authored Jun 26, 2023
1 parent be33406 commit ef7eb4c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api-samples/privacy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# chrome.privacy

This sample demonstrates using `chrome.privacy.services` to get and set privacy settings.

## Overview

The service worker sets the default value for Autofill using `chrome.privacy.services.autofillEnabled.set()` when the extension is installed. Whenever a page is loaded, or when the action button is clicked, the extension will display the current Autofill setting of `autofillAddressEnabled` by updating the extension badge.

## Running this extension

1. Clone this repository.
2. Load this directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked).
3. Pin the extension to the taskbar and either click the action button or load a webpage.
10 changes: 10 additions & 0 deletions api-samples/privacy/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "Privacy API sample",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "service-worker.js"
},
"permissions": ["privacy"],
"action": {}
}
43 changes: 43 additions & 0 deletions api-samples/privacy/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

chrome.runtime.onInstalled.addListener(() => {
// Set default value for Autofill Enabled
chrome.privacy.services.autofillEnabled.set({ value: true });
});

chrome.runtime.onStartup.addListener(() => {
updateAutofillEnabledStatus();
});

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.active) {
updateAutofillEnabledStatus();
}
});

function updateAutofillEnabledStatus() {
chrome.privacy.services.autofillEnabled.get({}, (details) => {
const autofillEnabled = details.value;
const badgeText = autofillEnabled ? 'Enabled' : 'Disabled';
const badgeColor = autofillEnabled ? '#00FF00' : '#FF0000';

chrome.action.setBadgeText({ text: badgeText });
chrome.action.setBadgeBackgroundColor({ color: badgeColor });
});
}

chrome.action.onClicked.addListener(() => {
updateAutofillEnabledStatus();
});

0 comments on commit ef7eb4c

Please sign in to comment.