Updated contentSettings sample for mv3

This commit is contained in:
Ian Stanion
2023-06-05 03:04:32 -04:00
parent 0e8881c6ce
commit d5e05f32c6
5 changed files with 200 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
# chrome.contentSettings
This sample demonstrates the use of `chrome.contentSettings` to display the settings of a given page in the extension's popup.
## Overview
The extension uses both the `chrome.contentSettings` and `chrome.contentSettings` APIs to check the value of each content setting on the user's currently active tab.
## 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 extenion to the browser's taskbar and click on the popup to see the content settings for a given site.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@@ -0,0 +1,11 @@
{
"name": "Content settings",
"version": "0.3",
"description": "Shows the content settings for the current site.",
"permissions": ["contentSettings", "tabs"],
"action": {
"default_icon": "contentSettings.png",
"default_popup": "popup.html"
},
"manifest_version": 3
}

View File

@@ -0,0 +1,105 @@
<!DOCTYPE html>
<html>
<head>
<title>Popup</title>
<style>
dt {
white-space: nowrap;
}
</style>
<script src="popup.js"></script>
</head>
<body>
<fieldset>
<dl>
<dt><label for="cookies">Cookies: </label></dt>
<dd>
<select id="cookies" disabled>
<option value="allow">Allow</option>
<option value="session_only">Session only</option>
<option value="block">Block</option>
</select>
</dd>
<dt><label for="images">Images: </label></dt>
<dd>
<select id="images" disabled>
<option value="allow">Allow</option>
<option value="block">Block</option>
</select>
</dd>
<dt><label for="javascript">Javascript: </label></dt>
<dd>
<select id="javascript" disabled>
<option value="allow">Allow</option>
<option value="block">Block</option>
</select>
</dd>
<dt><label for="location">Location: </label></dt>
<dd>
<select id="location" disabled>
<option value="allow">Allow</option>
<option value="ask">Ask</option>
<option value="block">Block</option>
</select>
</dd>
<dt><label for="plugins">Plugins: </label></dt>
<dd>
<select id="plugins" disabled>
<option value="allow">Allow</option>
<option value="block">Block</option>
</select>
</dd>
<dt><label for="popups">Pop-ups: </label></dt>
<dd>
<select id="popups" disabled>
<option value="allow">Allow</option>
<option value="block">Block</option>
</select>
</dd>
<dt><label for="notifications">Notifications: </label></dt>
<dd>
<select id="notifications" disabled>
<option value="allow">Allow</option>
<option value="ask">Ask</option>
<option value="block">Block</option>
</select>
</dd>
<dt><label for="microphone">Microphone: </label></dt>
<dd>
<select id="microphone" disabled>
<option value="allow">Allow</option>
<option value="ask">Ask</option>
<option value="block">Block</option>
</select>
</dd>
<dt><label for="camera">Camera: </label></dt>
<dd>
<select id="camera" disabled>
<option value="allow">Allow</option>
<option value="ask">Ask</option>
<option value="block">Block</option>
</select>
</dd>
<dt>
<label for="unsandboxedPlugins">Unsandboxed plugin access: </label>
</dt>
<dd>
<select id="unsandboxedPlugins" disabled>
<option value="allow">Allow</option>
<option value="ask">Ask</option>
<option value="block">Block</option>
</select>
</dd>
<dt><label for="automaticDownloads">Automatic Downloads: </label></dt>
<dd>
<select id="automaticDownloads" disabled>
<option value="allow">Allow</option>
<option value="ask">Ask</option>
<option value="block">Block</option>
</select>
</dd>
</dl>
</fieldset>
</body>
</html>

View File

@@ -0,0 +1,71 @@
// 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.
let incognito;
let url;
function settingChanged() {
let type = this.id;
let setting = this.value;
let pattern = /^file:/.test(url) ? url : url.replace(/\/[^/]*?$/, '/*');
console.log(type + ' setting for ' + pattern + ': ' + setting);
// HACK: [type] is not recognised by the docserver's sample crawler, so
// mention an explicit
// type: chrome.contentSettings.cookies.set - See http://crbug.com/299634
chrome.contentSettings[type].set({
primaryPattern: pattern,
setting: setting,
scope: incognito ? 'incognito_session_only' : 'regular'
});
}
document.addEventListener('DOMContentLoaded', function () {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
let current = tabs[0];
incognito = current.incognito;
url = current.url;
let types = [
'cookies',
'images',
'javascript',
'location',
'popups',
'notifications',
'microphone',
'camera',
'automaticDownloads'
];
types.forEach(function (type) {
// HACK: [type] is not recognised by the docserver's sample crawler, so
// mention an explicit
// type: chrome.contentSettings.cookies.get - See http://crbug.com/299634
chrome.contentSettings[type] &&
chrome.contentSettings[type].get(
{
primaryUrl: url,
incognito: incognito
},
function (details) {
document.getElementById(type).disabled = false;
document.getElementById(type).value = details.setting;
}
);
});
});
let selects = document.querySelectorAll('select');
for (let i = 0; i < selects.length; i++) {
selects[i].addEventListener('change', settingChanged);
}
});