Privacy mv3 sample (#956)

* Implemented mv3 privacy API sample

* Added mv3 privacy API sample

* Expanded sample and updated readme

* Update service-worker.js
This commit is contained in:
IanStanion-google
2023-06-26 09:57:14 -04:00
committed by GitHub
parent be334066a2
commit ef7eb4cd1a
3 changed files with 66 additions and 0 deletions

View File

@@ -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.

View File

@@ -0,0 +1,10 @@
{
"name": "Privacy API sample",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "service-worker.js"
},
"permissions": ["privacy"],
"action": {}
}

View File

@@ -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();
});