-
Notifications
You must be signed in to change notification settings - Fork 104
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
Correct way to inject ad blocker in React Native WebView #3039
Comments
Update: import {FiltersEngine} from '@cliqz/adblocker';
FiltersEngine.fromPrebuiltAdsAndTracking(fetch).then(engine => {
console.log(engine);
}); However, what is the next step? Where can I do the matching? I need something like an onScriptLoading event. @remusao Any ideas? |
Hi @Alwinator, Unfortunately I am not very familiar with the APIs available in React Native WebView. What you will need is a way to intercept network requests for pages loaded in the WebView, and then ask the adblocker Best, |
@remusao Does it also work by injecting JavaScript? I am able to manipulate the DOM. I need some way to intercept network requests. (Completely independent from the ReactNative WebView) Idea 1I thought of overwriting the XMLHttpRequest.prototype.send, but this does not work for HTML script and iframe elements. Idea 2Another approach would be to get all scripts and iframes and check them, which also does not work fine. function processElement(element) {
// process element.src with engine
// and then element.remove()
}
const observer = new MutationObserver((mutations, observer) => {
for (const mutation of mutations) {
for (const addedNode of mutation.addedNodes) {
if (addedNode.tagName === 'SCRIPT' || addedNode.tagName === 'IFRAME') {
processElement(addedNode);
}
}
}
});
observer.observe(document.body, {childList: true, subtree: true});
document.addEventListener('DOMContentLoaded', () => {
Array.from(document.getElementsByTagName('script')).forEach(processElement);
Array.from(document.getElementsByTagName('iframe')).forEach(processElement);
}); Idea 3Maybe a better approach would be to achieve it with a service worker as described here. I will try this approach in the next few days when I have time. Do you think it is in general possible without a web browser extension? |
@Alwinator i like your Service Worker idea. If you control the website and are able to inject a service worker it should be possible to load the adblocker there and perform the blocking or ads from there. You would likely need to write some amount of boiler plate since the library does not have a nice abstraction for that. |
I am working on a react native app with a web view that should block ads. The web view offers a way to inject javascript code. What is the correct way to inject your ad blocker?
I tried around but found no solution. It could look similar to the code below:
Thanks for your support!
The text was updated successfully, but these errors were encountered: