From 29f071fe2fefb86204a8471c35eda10d2992985a Mon Sep 17 00:00:00 2001 From: def00111 Date: Sun, 11 May 2025 20:26:57 +0200 Subject: [PATCH 01/24] Add an example to search for multi-byte pattern in an array And manipulate the array directly. --- .../webrequest/streamfilter/ondata/index.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index 27a2bfaf5d01153..fbc13e4736505b6 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -315,6 +315,60 @@ browser.webRequest.onBeforeRequest.addListener( ); ``` +This example demonstrates, how to search for multi-byte pattern in an array: + +```js +Array.prototype.indexOfMulti = function(searchElements, fromIndex) { + let i = this.indexOf(searchElements[0], fromIndex); + if (searchElements.length === 1 || i === -1) { + // Not found or no other elements to check + return i; + } + + const initial = i; + for (let j = 1; j < searchElements.length && i < this.length; j++) { + if (this[++i] !== searchElements[j]) { + return this.indexOfMulti(searchElements, initial + 1); + } + } + + return (i === initial + searchElements.length - 1) ? initial : -1; +}; + +const encoder = new TextEncoder(); +const elements = encoder.encode("WebExtension "); +const bytes = encoder.encode("Example"); + +function listener(details) { + const filter = browser.webRequest.filterResponseData(details.requestId); + + const data = []; + filter.ondata = (event) => { + const buffer = new Uint8Array(event.data); + for (let i = 0, l = buffer.length; i < l; i++) { + data.push(buffer[i]); + } + }; + + filter.onstop = (event) => { + let i, pos; + while ((i = data.indexOfMulti(bytes, pos)) !== -1) { + data.splice(i, 0, ...elements); + pos = i + elements.length + bytes.length; + } + + filter.write(new Uint8Array(data)); + filter.close(); + }; +} + +browser.webRequest.onBeforeRequest.addListener( + listener, + { urls: ["https://example.com/"], types: ["main_frame"] }, + ["blocking"], +); +``` + {{WebExtExamples}} ## Browser compatibility From 163c953c80e942c8866e33aba6230d61881687a5 Mon Sep 17 00:00:00 2001 From: def00111 Date: Sun, 11 May 2025 20:38:46 +0200 Subject: [PATCH 02/24] Update index.md --- .../webextensions/api/webrequest/streamfilter/ondata/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index fbc13e4736505b6..7f9266dbc1dc6c2 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -318,7 +318,7 @@ browser.webRequest.onBeforeRequest.addListener( This example demonstrates, how to search for multi-byte pattern in an array: ```js -Array.prototype.indexOfMulti = function(searchElements, fromIndex) { +Array.prototype.indexOfMulti = function (searchElements, fromIndex) { let i = this.indexOf(searchElements[0], fromIndex); if (searchElements.length === 1 || i === -1) { // Not found or no other elements to check From 3f5014164cd4b7fddf4df2f8c7229e68a5d48709 Mon Sep 17 00:00:00 2001 From: def00111 Date: Sun, 11 May 2025 20:41:04 +0200 Subject: [PATCH 03/24] Update index.md --- .../webextensions/api/webrequest/streamfilter/ondata/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index 7f9266dbc1dc6c2..ddae7048ac8fa46 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -332,7 +332,7 @@ Array.prototype.indexOfMulti = function (searchElements, fromIndex) { } } - return (i === initial + searchElements.length - 1) ? initial : -1; + return i === initial + searchElements.length - 1 ? initial : -1; }; const encoder = new TextEncoder(); From 234d19591539a91c14491b59f1780bedb0bf5eaa Mon Sep 17 00:00:00 2001 From: def00111 Date: Thu, 15 May 2025 18:34:46 +0200 Subject: [PATCH 04/24] Update example --- .../api/webrequest/streamfilter/ondata/index.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index ddae7048ac8fa46..a96282978454d92 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -326,7 +326,11 @@ Array.prototype.indexOfMulti = function (searchElements, fromIndex) { } const initial = i; - for (let j = 1; j < searchElements.length && i < this.length; j++) { + for ( + let j = 1, m = searchElements.length, n = this.length; + j < m && i < n; + j++ + ) { if (this[++i] !== searchElements[j]) { return this.indexOfMulti(searchElements, initial + 1); } From 171cfe2ef6410ef0c639d07c335374e9e4aa82ee Mon Sep 17 00:00:00 2001 From: def00111 Date: Thu, 15 May 2025 20:25:22 +0200 Subject: [PATCH 05/24] Update example --- .../api/webrequest/streamfilter/ondata/index.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index a96282978454d92..77b72e296f82978 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -327,7 +327,9 @@ Array.prototype.indexOfMulti = function (searchElements, fromIndex) { const initial = i; for ( - let j = 1, m = searchElements.length, n = this.length; + let j = 1, + m = searchElements.length, + n = this.length; j < m && i < n; j++ ) { @@ -355,10 +357,14 @@ function listener(details) { }; filter.onstop = (event) => { - let i, pos; - while ((i = data.indexOfMulti(bytes, pos)) !== -1) { + for ( + let i = data.indexOfMulti(bytes), + m = elements.length, + n = bytes.length; + i >= 0; + i = data.indexOfMulti(bytes, i + m + n) + ) { data.splice(i, 0, ...elements); - pos = i + elements.length + bytes.length; } filter.write(new Uint8Array(data)); From 7610ab680379e72ab3c6e7b1838c8c90372ad5e3 Mon Sep 17 00:00:00 2001 From: def00111 Date: Thu, 15 May 2025 20:29:48 +0200 Subject: [PATCH 06/24] Update example --- .../api/webrequest/streamfilter/ondata/index.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index 77b72e296f82978..d9e6d8b9e8e2be6 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -327,9 +327,7 @@ Array.prototype.indexOfMulti = function (searchElements, fromIndex) { const initial = i; for ( - let j = 1, - m = searchElements.length, - n = this.length; + let j = 1, m = searchElements.length, n = this.length; j < m && i < n; j++ ) { @@ -358,9 +356,7 @@ function listener(details) { filter.onstop = (event) => { for ( - let i = data.indexOfMulti(bytes), - m = elements.length, - n = bytes.length; + let i = data.indexOfMulti(bytes), m = elements.length, n = bytes.length; i >= 0; i = data.indexOfMulti(bytes, i + m + n) ) { From 1a987e4530ec833d1cd0c61722bba4ef4082ef04 Mon Sep 17 00:00:00 2001 From: def00111 Date: Sat, 17 May 2025 10:49:11 +0200 Subject: [PATCH 07/24] Update example --- .../webrequest/streamfilter/ondata/index.md | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index d9e6d8b9e8e2be6..3addec398cd4b8f 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -318,26 +318,28 @@ browser.webRequest.onBeforeRequest.addListener( This example demonstrates, how to search for multi-byte pattern in an array: ```js -Array.prototype.indexOfMulti = function (searchElements, fromIndex) { - let i = this.indexOf(searchElements[0], fromIndex); - if (searchElements.length === 1 || i === -1) { - // Not found or no other elements to check - return i; - } - - const initial = i; - for ( - let j = 1, m = searchElements.length, n = this.length; - j < m && i < n; - j++ - ) { - if (this[++i] !== searchElements[j]) { - return this.indexOfMulti(searchElements, initial + 1); +Object.defineProperty(Array.prototype, "indexOfMulti", { + value: function (searchElements, fromIndex) { + let i = this.indexOf(searchElements[0], fromIndex); + if (searchElements.length === 1 || i === -1) { + // Not found or no other elements to check + return i; } - } - return i === initial + searchElements.length - 1 ? initial : -1; -}; + const initial = i; + for ( + let j = 1, m = searchElements.length, n = this.length; + j < m && i < n; + j++ + ) { + if (this[++i] !== searchElements[j]) { + return this.indexOfMulti(searchElements, initial + 1); + } + } + + return i === initial + searchElements.length - 1 ? initial : -1; + }, +}); const encoder = new TextEncoder(); const elements = encoder.encode("WebExtension "); From d376839c1f7eb037fabf785fb056d2e97fae2393 Mon Sep 17 00:00:00 2001 From: def00111 Date: Sat, 17 May 2025 10:54:43 +0200 Subject: [PATCH 08/24] Update example --- .../webextensions/api/webrequest/streamfilter/ondata/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index 3addec398cd4b8f..92ecf855a2e5c86 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -362,6 +362,7 @@ function listener(details) { i >= 0; i = data.indexOfMulti(bytes, i + m + n) ) { + // Insert "WebExtension " at the given index data.splice(i, 0, ...elements); } From d1941c243ca52f1ea310a74d2406514509db980f Mon Sep 17 00:00:00 2001 From: def00111 Date: Sat, 17 May 2025 22:49:10 +0200 Subject: [PATCH 09/24] Update example --- .../api/webrequest/streamfilter/ondata/index.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index 92ecf855a2e5c86..0a1dbee0c93818b 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -326,18 +326,17 @@ Object.defineProperty(Array.prototype, "indexOfMulti", { return i; } + if (i + searchElements.length > this.length) { + return -1; + } + const initial = i; - for ( - let j = 1, m = searchElements.length, n = this.length; - j < m && i < n; - j++ - ) { + for (let j = 1, l = searchElements.length; j < l; j++) { if (this[++i] !== searchElements[j]) { return this.indexOfMulti(searchElements, initial + 1); } } - - return i === initial + searchElements.length - 1 ? initial : -1; + return initial; }, }); From 58c9c75eeac388fb65e35daf514f8cd1c2658adb Mon Sep 17 00:00:00 2001 From: def00111 Date: Sun, 25 May 2025 16:53:40 +0200 Subject: [PATCH 10/24] Update example --- .../webrequest/streamfilter/ondata/index.md | 133 ++++++++++++++---- 1 file changed, 104 insertions(+), 29 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index 0a1dbee0c93818b..7909e31748da167 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -318,54 +318,129 @@ browser.webRequest.onBeforeRequest.addListener( This example demonstrates, how to search for multi-byte pattern in an array: ```js -Object.defineProperty(Array.prototype, "indexOfMulti", { - value: function (searchElements, fromIndex) { - let i = this.indexOf(searchElements[0], fromIndex); - if (searchElements.length === 1 || i === -1) { - // Not found or no other elements to check - return i; +// JavaScript program to search the pattern in given array +// using KMP Algorithm + +function constructLps(pat, lps) { + // len stores the length of longest prefix which + // is also a suffix for the previous index + let len = 0; + + // lps[0] is always 0 + lps[0] = 0; + + let i = 1; + while (i < pat.length) { + // If characters match, increment the size of lps + if (pat[i] === pat[len]) { + len++; + lps[i] = len; + i++; } - - if (i + searchElements.length > this.length) { - return -1; + // If there is a mismatch + else { + if (len !== 0) { + // Update len to the previous lps value + // to avoid redundant comparisons + len = lps[len - 1]; + } else { + // If no matching prefix found, set lps[i] to 0 + lps[i] = 0; + i++; + } } + } +} - const initial = i; - for (let j = 1, l = searchElements.length; j < l; j++) { - if (this[++i] !== searchElements[j]) { - return this.indexOfMulti(searchElements, initial + 1); +function search(pat, arr) { + const n = arr.length; + const m = pat.length; + + const lps = new Array(m); + const res = []; + + constructLps(pat, lps); + + // Pointers i and j, for traversing + // the array and pattern + let i = 0; + let j = 0; + + while (i < n) { + // If characters match, move both pointers forward + if (arr[i] === pat[j]) { + i++; + j++; + + // If the entire pattern is matched + // store the start index in result + if (j === m) { + res.push(i - j); + // Use LPS of previous index to + // skip unnecessary comparisons + j = lps[j - 1]; + } + } + // If there is a mismatch + else { + // Use lps value of previous index + // to avoid redundant comparisons + if (j !== 0) { + j = lps[j - 1]; + } else { + i++; } } - return initial; - }, -}); + } + return res; +} const encoder = new TextEncoder(); + const elements = encoder.encode("WebExtension "); const bytes = encoder.encode("Example"); function listener(details) { const filter = browser.webRequest.filterResponseData(details.requestId); - const data = []; - filter.ondata = (event) => { + const oldData = []; + filter.ondata = event => { const buffer = new Uint8Array(event.data); - for (let i = 0, l = buffer.length; i < l; i++) { - data.push(buffer[i]); + let data = Array.from(buffer); + if (oldData.length) { + data = oldData.concat(data); + oldData.length = 0; } - }; - filter.onstop = (event) => { - for ( - let i = data.indexOfMulti(bytes), m = elements.length, n = bytes.length; - i >= 0; - i = data.indexOfMulti(bytes, i + m + n) - ) { + const res = search(bytes, data); + let len = 0; + for (const i of res) { // Insert "WebExtension " at the given index - data.splice(i, 0, ...elements); + data.splice(i + len, 0, ...elements); + len += elements.length; + } + + const uint8 = new Uint8Array(data); + let i = uint8.lastIndexOf(bytes[0]); + if (i != -1 && i + bytes.length > uint8.length) { + // Handle cases where the data looks like "

Exampl" + const initial = i; + let found = false; + for (let j = 1, l = uint8.length - i; j < l; j++) { + if (uint8[++i] == bytes[j] && j == l - 1) { + found = true; + } + } + if (found) { + oldData.push(...uint8.slice(initial)); + filter.write(uint8.subarray(0, initial)); + return; + } } + filter.write(uint8); + }; - filter.write(new Uint8Array(data)); + filter.onstop = () => { filter.close(); }; } From 221205aea562bf6469e5fb58372928c6f3897d9c Mon Sep 17 00:00:00 2001 From: def00111 Date: Sun, 25 May 2025 17:04:16 +0200 Subject: [PATCH 11/24] Update example --- .../api/webrequest/streamfilter/ondata/index.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index 7909e31748da167..e6dcf338eeaccea 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -318,11 +318,11 @@ browser.webRequest.onBeforeRequest.addListener( This example demonstrates, how to search for multi-byte pattern in an array: ```js -// JavaScript program to search the pattern in given array +// JavaScript program to search the pattern in given array // using KMP Algorithm function constructLps(pat, lps) { - // len stores the length of longest prefix which + // len stores the length of longest prefix which // is also a suffix for the previous index let len = 0; @@ -340,7 +340,7 @@ function constructLps(pat, lps) { // If there is a mismatch else { if (len !== 0) { - // Update len to the previous lps value + // Update len to the previous lps value // to avoid redundant comparisons len = lps[len - 1]; } else { @@ -361,7 +361,7 @@ function search(pat, arr) { constructLps(pat, lps); - // Pointers i and j, for traversing + // Pointers i and j, for traversing // the array and pattern let i = 0; let j = 0; @@ -372,11 +372,11 @@ function search(pat, arr) { i++; j++; - // If the entire pattern is matched + // If the entire pattern is matched // store the start index in result if (j === m) { res.push(i - j); - // Use LPS of previous index to + // Use LPS of previous index to // skip unnecessary comparisons j = lps[j - 1]; } @@ -392,7 +392,7 @@ function search(pat, arr) { } } } - return res; + return res; } const encoder = new TextEncoder(); @@ -404,7 +404,7 @@ function listener(details) { const filter = browser.webRequest.filterResponseData(details.requestId); const oldData = []; - filter.ondata = event => { + filter.ondata = (event) => { const buffer = new Uint8Array(event.data); let data = Array.from(buffer); if (oldData.length) { From 0f18979830ec40b18b129bdd776cf5f72b4acb0f Mon Sep 17 00:00:00 2001 From: def00111 Date: Sun, 25 May 2025 17:22:51 +0200 Subject: [PATCH 12/24] Update example --- .../webextensions/api/webrequest/streamfilter/ondata/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index e6dcf338eeaccea..be755e3e5e10321 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -432,7 +432,7 @@ function listener(details) { } } if (found) { - oldData.push(...uint8.slice(initial)); + oldData.push(...uint8.subarray(initial)); filter.write(uint8.subarray(0, initial)); return; } From a3861f4bcdf401dc59639d10673b3bda4aa5dc47 Mon Sep 17 00:00:00 2001 From: def00111 Date: Sun, 25 May 2025 17:46:06 +0200 Subject: [PATCH 13/24] Update example --- .../webextensions/api/webrequest/streamfilter/ondata/index.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index be755e3e5e10321..bf4a3ed44ac8932 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -427,7 +427,9 @@ function listener(details) { const initial = i; let found = false; for (let j = 1, l = uint8.length - i; j < l; j++) { - if (uint8[++i] == bytes[j] && j == l - 1) { + if (uint8[++i] !== bytes[j]) { + break; + } else if (j === l - 1) { found = true; } } From ec57ec4dc2d893347fe31fb91180ae0a8ab9bd3a Mon Sep 17 00:00:00 2001 From: def00111 Date: Sun, 25 May 2025 18:58:53 +0200 Subject: [PATCH 14/24] Update example --- .../api/webrequest/streamfilter/ondata/index.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index bf4a3ed44ac8932..12884f50f1a7a0c 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -423,14 +423,13 @@ function listener(details) { const uint8 = new Uint8Array(data); let i = uint8.lastIndexOf(bytes[0]); if (i != -1 && i + bytes.length > uint8.length) { - // Handle cases where the data looks like "

Exampl" + // Handle cases where the end of the data looks like "

Exampl" const initial = i; - let found = false; + let found = true; for (let j = 1, l = uint8.length - i; j < l; j++) { if (uint8[++i] !== bytes[j]) { + found = false; break; - } else if (j === l - 1) { - found = true; } } if (found) { From 733a89355521ce3c71c8377773240efc2d4bfb84 Mon Sep 17 00:00:00 2001 From: def00111 Date: Sun, 25 May 2025 19:52:17 +0200 Subject: [PATCH 15/24] Update example --- .../webrequest/streamfilter/ondata/index.md | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index 12884f50f1a7a0c..7b470e64484b1d1 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -405,8 +405,8 @@ function listener(details) { const oldData = []; filter.ondata = (event) => { - const buffer = new Uint8Array(event.data); - let data = Array.from(buffer); + let data = new Uint8Array(event.data); + data = Array.from(data); if (oldData.length) { data = oldData.concat(data); oldData.length = 0; @@ -420,25 +420,25 @@ function listener(details) { len += elements.length; } - const uint8 = new Uint8Array(data); - let i = uint8.lastIndexOf(bytes[0]); - if (i != -1 && i + bytes.length > uint8.length) { + data = new Uint8Array(data); + let i = data.lastIndexOf(bytes[0]); + if (i != -1 && i + bytes.length > data.length) { // Handle cases where the end of the data looks like "

Exampl" const initial = i; let found = true; - for (let j = 1, l = uint8.length - i; j < l; j++) { - if (uint8[++i] !== bytes[j]) { + for (let j = 1, l = data.length - i; j < l; j++) { + if (data[++i] !== bytes[j]) { found = false; break; } } if (found) { - oldData.push(...uint8.subarray(initial)); - filter.write(uint8.subarray(0, initial)); + oldData.push(...data.subarray(initial)); + filter.write(data.subarray(0, initial)); return; } } - filter.write(uint8); + filter.write(data); }; filter.onstop = () => { From c090a5367f714456cc848c6243a4ba8a782eacd9 Mon Sep 17 00:00:00 2001 From: def00111 Date: Wed, 28 May 2025 22:02:09 +0200 Subject: [PATCH 16/24] Update example --- .../webrequest/streamfilter/ondata/index.md | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index 7b470e64484b1d1..fa8c56e6e9f2213 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -421,21 +421,18 @@ function listener(details) { } data = new Uint8Array(data); - let i = data.lastIndexOf(bytes[0]); - if (i != -1 && i + bytes.length > data.length) { - // Handle cases where the end of the data looks like "

Exampl" - const initial = i; - let found = true; - for (let j = 1, l = data.length - i; j < l; j++) { - if (data[++i] !== bytes[j]) { - found = false; - break; + outer: for (let i = data.length - 1, l = data.length - bytes.length; i > l; i--) { + if (bytes[0] === data[i]) { + // Handle cases where the end of the data looks like "

Exampl" + const initial = i; + for (let j = 1, l = data.length - i; j < l; j++) { + if (data[++i] !== bytes[j]) { + break outer; + } } - } - if (found) { - oldData.push(...data.subarray(initial)); - filter.write(data.subarray(0, initial)); - return; + oldData.push(...data.slice(initial)); + data = data.slice(0, initial); + break; } } filter.write(data); From cc8beca0c55f1f8bfe9c3ab8074eaf771714ecb5 Mon Sep 17 00:00:00 2001 From: def00111 Date: Wed, 28 May 2025 22:26:26 +0200 Subject: [PATCH 17/24] Update example --- .../api/webrequest/streamfilter/ondata/index.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index fa8c56e6e9f2213..c3355e43caf34ab 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -405,22 +405,23 @@ function listener(details) { const oldData = []; filter.ondata = (event) => { - let data = new Uint8Array(event.data); + let data = event.data; + data = new Uint8Array(data); data = Array.from(data); + if (oldData.length) { data = oldData.concat(data); oldData.length = 0; } - const res = search(bytes, data); let len = 0; + const res = search(bytes, data); for (const i of res) { // Insert "WebExtension " at the given index data.splice(i + len, 0, ...elements); len += elements.length; } - data = new Uint8Array(data); outer: for (let i = data.length - 1, l = data.length - bytes.length; i > l; i--) { if (bytes[0] === data[i]) { // Handle cases where the end of the data looks like "

Exampl" @@ -435,7 +436,7 @@ function listener(details) { break; } } - filter.write(data); + filter.write(new Uint8Array(data)); }; filter.onstop = () => { From 55d6078afddcbc6c9d534bcdc5b39798ff145dcf Mon Sep 17 00:00:00 2001 From: def00111 Date: Wed, 28 May 2025 22:28:35 +0200 Subject: [PATCH 18/24] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../api/webrequest/streamfilter/ondata/index.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index c3355e43caf34ab..c838931681b623f 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -422,7 +422,11 @@ function listener(details) { len += elements.length; } - outer: for (let i = data.length - 1, l = data.length - bytes.length; i > l; i--) { + outer: for ( + let i = data.length - 1, l = data.length - bytes.length; + i > l; + i-- + ) { if (bytes[0] === data[i]) { // Handle cases where the end of the data looks like "

Exampl" const initial = i; From a57ebc93e5fefb19df75f3ea4abb9f5f6676aa59 Mon Sep 17 00:00:00 2001 From: def00111 Date: Thu, 29 May 2025 14:20:07 +0200 Subject: [PATCH 19/24] Update example --- .../webextensions/api/webrequest/streamfilter/ondata/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index c838931681b623f..97d61731dbf45a8 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -422,7 +422,7 @@ function listener(details) { len += elements.length; } - outer: for ( + mainLoop: for ( let i = data.length - 1, l = data.length - bytes.length; i > l; i-- @@ -432,7 +432,7 @@ function listener(details) { const initial = i; for (let j = 1, l = data.length - i; j < l; j++) { if (data[++i] !== bytes[j]) { - break outer; + break mainLoop; } } oldData.push(...data.slice(initial)); From 2f78c3a71e9e71cc6a0448501fb7ec773ad65205 Mon Sep 17 00:00:00 2001 From: def00111 Date: Fri, 30 May 2025 22:45:06 +0200 Subject: [PATCH 20/24] Update example --- .../api/webrequest/streamfilter/ondata/index.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index 97d61731dbf45a8..427087479347034 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -422,15 +422,16 @@ function listener(details) { len += elements.length; } + // Check if the end of the data looks like "

Exampl" + const n = data.length; mainLoop: for ( - let i = data.length - 1, l = data.length - bytes.length; + let i = n - 1, l = n - bytes.length; i > l; i-- ) { if (bytes[0] === data[i]) { - // Handle cases where the end of the data looks like "

Exampl" const initial = i; - for (let j = 1, l = data.length - i; j < l; j++) { + for (let j = 1, l = n - i; j < l; j++) { if (data[++i] !== bytes[j]) { break mainLoop; } From 66e03120b9fcf24abf662e94c0cdbbd319716e84 Mon Sep 17 00:00:00 2001 From: def00111 Date: Fri, 30 May 2025 22:47:31 +0200 Subject: [PATCH 21/24] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../api/webrequest/streamfilter/ondata/index.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index 427087479347034..caa215daffa6e8d 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -424,11 +424,7 @@ function listener(details) { // Check if the end of the data looks like "

Exampl" const n = data.length; - mainLoop: for ( - let i = n - 1, l = n - bytes.length; - i > l; - i-- - ) { + mainLoop: for (let i = n - 1, l = n - bytes.length; i > l; i--) { if (bytes[0] === data[i]) { const initial = i; for (let j = 1, l = n - i; j < l; j++) { From 9756534ac00a6f5e8b8358360c624cdc5e2bbb37 Mon Sep 17 00:00:00 2001 From: def00111 Date: Fri, 30 May 2025 22:51:40 +0200 Subject: [PATCH 22/24] Update example --- .../webextensions/api/webrequest/streamfilter/ondata/index.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index caa215daffa6e8d..ff49a713ebfdd12 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -424,7 +424,8 @@ function listener(details) { // Check if the end of the data looks like "

Exampl" const n = data.length; - mainLoop: for (let i = n - 1, l = n - bytes.length; i > l; i--) { + const m = bytes.length; + mainLoop: for (let i = n - 1, l = n - m; i > l; i--) { if (bytes[0] === data[i]) { const initial = i; for (let j = 1, l = n - i; j < l; j++) { From 1fc53ceff87ddd41f5ecd9efc16ac6079995cb96 Mon Sep 17 00:00:00 2001 From: def00111 Date: Sun, 1 Jun 2025 14:07:56 +0200 Subject: [PATCH 23/24] Update example --- .../api/webrequest/streamfilter/ondata/index.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index ff49a713ebfdd12..20db4d4bdc02139 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -422,14 +422,18 @@ function listener(details) { len += elements.length; } - // Check if the end of the data looks like "

Exampl" + // Check if the word "Example" is cropped at the end, e.g. "

Exampl" const n = data.length; const m = bytes.length; - mainLoop: for (let i = n - 1, l = n - m; i > l; i--) { + + let i = n - 1; + let j = 1; + + mainLoop: while (i > n - m) { if (bytes[0] === data[i]) { const initial = i; - for (let j = 1, l = n - i; j < l; j++) { - if (data[++i] !== bytes[j]) { + while (j < n - initial) { + if (data[++i] !== bytes[j++]) { break mainLoop; } } @@ -437,6 +441,7 @@ function listener(details) { data = data.slice(0, initial); break; } + i--; } filter.write(new Uint8Array(data)); }; From eef7882a39118f44b68ee517298f246cc02d1834 Mon Sep 17 00:00:00 2001 From: def00111 Date: Sun, 1 Jun 2025 20:45:55 +0200 Subject: [PATCH 24/24] Update example --- .../api/webrequest/streamfilter/ondata/index.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md index 20db4d4bdc02139..dff134495592316 100644 --- a/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/api/webrequest/streamfilter/ondata/index.md @@ -333,9 +333,7 @@ function constructLps(pat, lps) { while (i < pat.length) { // If characters match, increment the size of lps if (pat[i] === pat[len]) { - len++; - lps[i] = len; - i++; + lps[i++] = ++len; } // If there is a mismatch else { @@ -345,8 +343,7 @@ function constructLps(pat, lps) { len = lps[len - 1]; } else { // If no matching prefix found, set lps[i] to 0 - lps[i] = 0; - i++; + lps[i++] = 0; } } }