Add declarativeNetRequest API samples. (#839)
* Add declarativeNetRequest API samples. Adds a number of samples for the declarativeNetRequest API.
This commit is contained in:
Родитель
1229875d30
Коммит
45d8850c1a
|
@ -3,3 +3,4 @@
|
|||
node_modules
|
||||
# Temporary directory for debugging extension samples
|
||||
_debug
|
||||
_metadata
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
# chrome.declarativeNetRequest - No Cookies
|
||||
|
||||
This sample demonstrates using the [`chrome.declarativeNetRequest`](https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/) API to remove the "Cookie" header from requests.
|
||||
|
||||
## Overview
|
||||
|
||||
Once this extension is installed, any main frame requests ending in `?no-cookies=1` will be sent without the "Cookie" header.
|
||||
|
||||
For example, install this extension and try navigating to https://github.com/GoogleChrome/chrome-extensions-samples?no-cookies=1. You should appear signed out.
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
This sample uses the `chrome.declarativeNetRequest.onRuleMatchedDebug` event which is only available in unpacked extensions.
|
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "No Cookies",
|
||||
"description": "Removes 'Cookie' headers.",
|
||||
"version": "1.0",
|
||||
"manifest_version": 3,
|
||||
"permissions": [
|
||||
"declarativeNetRequest",
|
||||
"declarativeNetRequestFeedback"
|
||||
],
|
||||
"host_permissions": [
|
||||
"<all_urls>"
|
||||
],
|
||||
"background": {
|
||||
"service_worker": "service-worker.js"
|
||||
},
|
||||
"declarative_net_request" : {
|
||||
"rule_resources" : [{
|
||||
"id": "ruleset_1",
|
||||
"enabled": true,
|
||||
"path": "rules_1.json"
|
||||
}]
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
[
|
||||
{
|
||||
"id": 1,
|
||||
"priority": 1,
|
||||
"action": {
|
||||
"type": "modifyHeaders",
|
||||
"requestHeaders": [
|
||||
{ "header": "cookie", "operation": "remove" }
|
||||
]
|
||||
},
|
||||
"condition": {
|
||||
"urlFilter": "|*?no-cookies=1",
|
||||
"resourceTypes": ["main_frame"]
|
||||
}
|
||||
}
|
||||
]
|
|
@ -0,0 +1,24 @@
|
|||
// 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.
|
||||
|
||||
'use strict';
|
||||
|
||||
// Simple extension to remove the 'Cookie' request header.
|
||||
|
||||
chrome.declarativeNetRequest.onRuleMatchedDebug.addListener((e) => {
|
||||
const msg = `Cookies removed in request to ${e.request.url} on tab ${e.request.tabId}.`;
|
||||
console.log(msg);
|
||||
});
|
||||
|
||||
console.log('Service worker started.');
|
|
@ -0,0 +1,11 @@
|
|||
# chrome.declarativeNetRequest - URL Blocker
|
||||
|
||||
This sample demonstrates using the [`chrome.declarativeNetRequest`](https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/) API to block requests.
|
||||
|
||||
## Overview
|
||||
|
||||
Once this extension is installed, any requests made in the main frame to example.com will be blocked.
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
This sample uses the `chrome.declarativeNetRequest.onRuleMatchedDebug` event which is only available in unpacked extensions.
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"name": "URL Blocker",
|
||||
"version": "0.1",
|
||||
"manifest_version": 3,
|
||||
"description": "Blocks all main frame requests to example.com.",
|
||||
"background": {
|
||||
"service_worker": "service_worker.js"
|
||||
},
|
||||
"declarative_net_request" : {
|
||||
"rule_resources" : [{
|
||||
"id": "ruleset_1",
|
||||
"enabled": true,
|
||||
"path": "rules_1.json"
|
||||
}]
|
||||
},
|
||||
"permissions": [
|
||||
"declarativeNetRequest",
|
||||
"declarativeNetRequestFeedback"
|
||||
]
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
[
|
||||
{
|
||||
"id" : 1,
|
||||
"priority": 1,
|
||||
"action" : { "type" : "block" },
|
||||
"condition" : {
|
||||
"urlFilter" : "||example.com",
|
||||
"resourceTypes" : ["main_frame"]
|
||||
}
|
||||
}
|
||||
]
|
|
@ -0,0 +1,22 @@
|
|||
// 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.
|
||||
|
||||
'use strict';
|
||||
|
||||
chrome.declarativeNetRequest.onRuleMatchedDebug.addListener((e) => {
|
||||
const msg = `Navigation blocked to ${e.request.url} on tab ${e.request.tabId}.`;
|
||||
console.log(msg);
|
||||
});
|
||||
|
||||
console.log('Service worker started.');
|
|
@ -0,0 +1,17 @@
|
|||
# chrome.declarativeNetRequest - URL Blocker
|
||||
|
||||
This sample demonstrates using the [`chrome.declarativeNetRequest`](https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/) API to redirect requests.
|
||||
|
||||
## Overview
|
||||
|
||||
Once this extension is installed, any requests made in the main frame to the following URLs will be redirected:
|
||||
|
||||
- https://developer.chrome.com/docs/extensions/mv2/
|
||||
- https://developer.chrome.com/docs/extensions/reference/browserAction/
|
||||
- https://developer.chrome.com/docs/extensions/reference/declarativeWebRequest/
|
||||
- https://developer.chrome.com/docs/extensions/reference/pageAction/
|
||||
- https://developer.chrome.com/docs/extensions/reference/webRequest/
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
This sample uses the `chrome.declarativeNetRequest.onRuleMatchedDebug` event which is only available in unpacked extensions.
|
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"name": "URL Redirect",
|
||||
"version": "0.1",
|
||||
"manifest_version": 3,
|
||||
"description": "Redirects MV2 documents to equivalent MV3 documents on developer.chrome.com.",
|
||||
"background": {
|
||||
"service_worker": "service_worker.js"
|
||||
},
|
||||
"declarative_net_request" : {
|
||||
"rule_resources" : [{
|
||||
"id": "ruleset_1",
|
||||
"enabled": true,
|
||||
"path": "rules_1.json"
|
||||
}]
|
||||
},
|
||||
"permissions": [
|
||||
"declarativeNetRequest",
|
||||
"declarativeNetRequestFeedback",
|
||||
"declarativeNetRequestWithHostAccess"
|
||||
],
|
||||
"host_permissions": [
|
||||
"https://developer.chrome.com/"
|
||||
]
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
[
|
||||
{
|
||||
"id" : 1,
|
||||
"priority": 1,
|
||||
"action": {
|
||||
"type": "redirect",
|
||||
"redirect": { "url": "https://developer.chrome.com/docs/extensions/mv3/intro/" }
|
||||
},
|
||||
"condition": {
|
||||
"urlFilter": "https://developer.chrome.com/docs/extensions/mv2/",
|
||||
"resourceTypes": ["main_frame"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id" : 2,
|
||||
"priority": 1,
|
||||
"action": {
|
||||
"type": "redirect",
|
||||
"redirect": { "url": "https://developer.chrome.com/docs/extensions/reference/action/" }
|
||||
},
|
||||
"condition": {
|
||||
"urlFilter": "https://developer.chrome.com/docs/extensions/reference/browserAction/",
|
||||
"resourceTypes": ["main_frame"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id" : 3,
|
||||
"priority": 1,
|
||||
"action": {
|
||||
"type": "redirect",
|
||||
"redirect": { "url": "https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/" }
|
||||
},
|
||||
"condition": {
|
||||
"urlFilter": "https://developer.chrome.com/docs/extensions/reference/declarativeWebRequest/",
|
||||
"resourceTypes": ["main_frame"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id" : 4,
|
||||
"priority": 1,
|
||||
"action": {
|
||||
"type": "redirect",
|
||||
"redirect": { "url": "https://developer.chrome.com/docs/extensions/reference/action/" }
|
||||
},
|
||||
"condition": {
|
||||
"urlFilter": "https://developer.chrome.com/docs/extensions/reference/pageAction/",
|
||||
"resourceTypes": ["main_frame"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id" : 5,
|
||||
"priority": 1,
|
||||
"action": {
|
||||
"type": "redirect",
|
||||
"redirect": { "url": "https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/" }
|
||||
},
|
||||
"condition": {
|
||||
"urlFilter": "https://developer.chrome.com/docs/extensions/reference/webRequest/",
|
||||
"resourceTypes": ["main_frame"]
|
||||
}
|
||||
}
|
||||
]
|
|
@ -0,0 +1,22 @@
|
|||
// 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.
|
||||
|
||||
'use strict';
|
||||
|
||||
chrome.declarativeNetRequest.onRuleMatchedDebug.addListener((e) => {
|
||||
const msg = `Navigation to ${e.request.url} redirected on tab ${e.request.tabId}.`;
|
||||
console.log(msg);
|
||||
});
|
||||
|
||||
console.log('Service worker started.');
|
|
@ -1,10 +1,10 @@
|
|||
# chrome.scripting API
|
||||
|
||||
This sample demonstrates how to use the [chrome.scripting](https://developer.chrome.com/docs/extensions/reference/scripting/) API to inject JavaScript into web pages.
|
||||
This sample demonstrates using the [chrome.scripting](https://developer.chrome.com/docs/extensions/reference/scripting/) API to inject JavaScript into web pages.
|
||||
|
||||
## Overview
|
||||
|
||||
Once installed, clicking this extension's action icon will open an extension page.
|
||||
Once this extension is installed, clicking this extension's action icon will open an extension page.
|
||||
|
||||
<img src="screenshot.png" height=300 alt="Screenshot showing the chrome.scripting API demo running in Chrome.">
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче