mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
add in simple http-response example (#281)
* add in simple http-response example * move to 57.0a1
This commit is contained in:
11
http-response/README.md
Executable file
11
http-response/README.md
Executable file
@@ -0,0 +1,11 @@
|
||||
# HTTP Response parser
|
||||
|
||||
## What it does
|
||||
|
||||
Listens to HTTP Responses from example.com and changes the body of the response as it comes through. So that the word "Example" on https://example.com becomes "WebExtension Example".
|
||||
|
||||
## What it shows
|
||||
|
||||
How to use the response parser on bytes.
|
||||
|
||||
Icon is from: https://www.iconfinder.com/icons/763339/draw_edit_editor_pen_pencil_tool_write_icon#size=128
|
||||
22
http-response/background.js
Executable file
22
http-response/background.js
Executable file
@@ -0,0 +1,22 @@
|
||||
function listener(details) {
|
||||
let filter = browser.webRequest.filterResponseData(details.requestId);
|
||||
let decoder = new TextDecoder("utf-8");
|
||||
let encoder = new TextEncoder();
|
||||
|
||||
filter.ondata = event => {
|
||||
let str = decoder.decode(event.data, {stream: true});
|
||||
// Just change any instance of Example in the HTTP response
|
||||
// to WebExtension Example.
|
||||
str = str.replace(/Example/g, 'WebExtension Example');
|
||||
filter.write(encoder.encode(str));
|
||||
filter.disconnect();
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
browser.webRequest.onBeforeRequest.addListener(
|
||||
listener,
|
||||
{urls: ["https://example.com/*"], types: ["main_frame"]},
|
||||
["blocking"]
|
||||
);
|
||||
26
http-response/manifest.json
Executable file
26
http-response/manifest.json
Executable file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
|
||||
"description": "Altering HTTP responses",
|
||||
"manifest_version": 2,
|
||||
"name": "http-response-filter",
|
||||
"version": "1.0",
|
||||
"homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/http-response",
|
||||
"icons": {
|
||||
"48": "pen.svg"
|
||||
},
|
||||
|
||||
"permissions": [
|
||||
"webRequest", "webRequestBlocking", "https://example.com/*"
|
||||
],
|
||||
|
||||
"background": {
|
||||
"scripts": ["background.js"]
|
||||
},
|
||||
|
||||
"applications": {
|
||||
"gecko": {
|
||||
"strict_min_version": "57.0a1"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
1
http-response/pen.svg
Executable file
1
http-response/pen.svg
Executable file
@@ -0,0 +1 @@
|
||||
<?xml version="1.0" ?><svg height="24px" version="1.1" viewBox="0 0 24 24" width="24px" xmlns="http://www.w3.org/2000/svg" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns" xmlns:xlink="http://www.w3.org/1999/xlink"><title/><desc/><defs/><g fill="none" fill-rule="evenodd" id="miu" stroke="none" stroke-width="1"><g id="Artboard-1" transform="translate(-899.000000, -227.000000)"><g id="slice" transform="translate(215.000000, 119.000000)"/><path d="M914.000027,248.002414 L914.000464,232.002414 L914.000464,232.002414 L907.001354,232.002414 L907.000079,248.002414 L914.000027,248.002414 Z M913.998311,249.002414 L910.501672,254 L907.00169,249.002414 L913.998311,249.002414 Z M914.000492,231.002414 L914.000574,228.002414 C914.000574,227 912.99816,227 912.99816,227 L908.004086,227 C908.004086,227 907.001672,227 907.001672,228.002414 L907.001433,231.002414 L914.000492,231.002414 L914.000492,231.002414 Z" fill="#000000" id="editor-pencil-pen-edit-write-glyph" transform="translate(910.500326, 240.500000) rotate(45.000000) translate(-910.500326, -240.500000) "/></g></g></svg>
|
||||
|
After Width: | Height: | Size: 1.1 KiB |
Reference in New Issue
Block a user