Skip to content
Open
Changes from 3 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,60 @@ browser.webRequest.onBeforeRequest.addListener(
);
```

This example demonstrates, how to search for multi-byte pattern in an array:
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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; 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;
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Modifying built-in prototypes is generally considered an anti-pattern. There's no clear benefit to this approach over using a standalone function.


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();
};
Comment on lines +351 to +448
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Another suggestion that's out of scope for this PR: progressively apply the transform in ondata callbacks rather than buffering the entire file in memory and serially applying the transform at the end. This would improve scenarios where content can progressively render while streaming, such as with HTML responses.

}

browser.webRequest.onBeforeRequest.addListener(
listener,
{ urls: ["https://example.com/"], types: ["main_frame"] },
["blocking"],
);
```

{{WebExtExamples}}

## Browser compatibility
Expand Down