diff --git a/api-samples/privacy/README.md b/api-samples/privacy/README.md new file mode 100644 index 0000000000..f486795f4d --- /dev/null +++ b/api-samples/privacy/README.md @@ -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. diff --git a/api-samples/privacy/manifest.json b/api-samples/privacy/manifest.json new file mode 100644 index 0000000000..1710510aed --- /dev/null +++ b/api-samples/privacy/manifest.json @@ -0,0 +1,10 @@ +{ + "name": "Privacy API sample", + "version": "1.0", + "manifest_version": 3, + "background": { + "service_worker": "service-worker.js" + }, + "permissions": ["privacy"], + "action": {} +} diff --git a/api-samples/privacy/service-worker.js b/api-samples/privacy/service-worker.js new file mode 100644 index 0000000000..3176419536 --- /dev/null +++ b/api-samples/privacy/service-worker.js @@ -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(); +});