diff --git a/http-response/README.md b/http-response/README.md new file mode 100755 index 0000000..40c2338 --- /dev/null +++ b/http-response/README.md @@ -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 diff --git a/http-response/background.js b/http-response/background.js new file mode 100755 index 0000000..b7a7880 --- /dev/null +++ b/http-response/background.js @@ -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"] +); diff --git a/http-response/manifest.json b/http-response/manifest.json new file mode 100755 index 0000000..e2a13f4 --- /dev/null +++ b/http-response/manifest.json @@ -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" + } + } + +} diff --git a/http-response/pen.svg b/http-response/pen.svg new file mode 100755 index 0000000..65a3f42 --- /dev/null +++ b/http-response/pen.svg @@ -0,0 +1 @@ +