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 17, 2025
commit 1a987e4530ec833d1cd0c61722bba4ef4082ef04
Original file line number Diff line number Diff line change
Expand Up @@ -318,26 +318,28 @@ 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
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 ");
Expand Down