Skip to content

Commit

Permalink
Update chrome.privacy API sample (GoogleChrome#1086)
Browse files Browse the repository at this point in the history
* Update chrome.privacy API sample

Updates the API sample for chrome.privacy to avoid using the
deprecated autofillEnabled property and to behave in a way which
is more useful. In particular, you can now easily toggle the
setting by clicking the extension icon.

See https://issuetracker.google.com/issues/311072505 for more
context.

* Update api-samples/privacy/README.md

Co-authored-by: Joe Medley <[email protected]>

---------

Co-authored-by: Joe Medley <[email protected]>
  • Loading branch information
oliverdunk and jpmedley authored Feb 22, 2024
1 parent 4ce29a4 commit 72eb0a4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions api-samples/privacy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ This sample demonstrates using `chrome.privacy.services` to get and set privacy

## 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.
The service worker sets the default value for autofill using `chrome.privacy.services.autofillCreditCardEnabled.set()` when the extension is installed. Whenever the action button is clicked, the extension toggles the current autofill setting of `autofillCreditCardEnabled` and updates 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.
3. Pin the extension to the taskbar and click the action button.
34 changes: 19 additions & 15 deletions api-samples/privacy/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,35 @@
// limitations under the License.

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

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

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.active) {
updateAutofillEnabledStatus();
async function updateAutofillEnabledStatus(toggle = false) {
const details = await chrome.privacy.services.autofillCreditCardEnabled.get(
{}
);
let autofillEnabled = details.value;

if (toggle) {
autofillEnabled = !autofillEnabled;
await chrome.privacy.services.autofillCreditCardEnabled.set({
value: autofillEnabled
});
}
});

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

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

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

0 comments on commit 72eb0a4

Please sign in to comment.