mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-26 13:19:49 +07:00
Updated contentSettings sample for mv3 (#941)
* Updated contentSettings sample for mv3 * Update api-samples/contentSettings/contentSettings.md Co-authored-by: Joe Medley <jmedley@google.com> * Update contentSettings.md * Update contentSettings.md * Update manifest.json --------- Co-authored-by: Joe Medley <jmedley@google.com>
This commit is contained in:
committed by
GitHub
parent
178e272b08
commit
778403f124
13
api-samples/contentSettings/contentSettings.md
Normal file
13
api-samples/contentSettings/contentSettings.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# chrome.contentSettings
|
||||
|
||||
This sample demonstrates using `chrome.contentSettings` to display the settings of a given page in the extension's popup.
|
||||
|
||||
## Overview
|
||||
|
||||
The extension calls `chrome.contentSettings.get()` and `chrome.contentSettings.set()` to manage 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.
|
||||
BIN
api-samples/contentSettings/contentSettings.png
Normal file
BIN
api-samples/contentSettings/contentSettings.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
11
api-samples/contentSettings/manifest.json
Normal file
11
api-samples/contentSettings/manifest.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "Content settings",
|
||||
"version": "0.3",
|
||||
"description": "Shows the content settings for the current site.",
|
||||
"permissions": ["contentSettings", "activeTab"],
|
||||
"action": {
|
||||
"default_icon": "contentSettings.png",
|
||||
"default_popup": "popup.html"
|
||||
},
|
||||
"manifest_version": 3
|
||||
}
|
||||
105
api-samples/contentSettings/popup.html
Normal file
105
api-samples/contentSettings/popup.html
Normal 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>
|
||||
71
api-samples/contentSettings/popup.js
Normal file
71
api-samples/contentSettings/popup.js
Normal 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);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user