Skip to content

Add an example to search for multi-byte pattern in an array #39479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update example
  • Loading branch information
def00111 authored May 25, 2025
commit 58c9c75eeac388fb65e35daf514f8cd1c2658adb
Original file line number Diff line number Diff line change
Expand Up @@ -318,54 +318,129 @@ browser.webRequest.onBeforeRequest.addListener(
This example demonstrates, how to search for multi-byte pattern in an array:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is very large now. Should it be on https://github.com/mdn/webextensions-examples?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@def00111 yes, I think this is now getting to the point where we need to move it out of the documentation into a web extensions example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


```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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 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 "<h1>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();
};
}
Expand Down
Loading