AdguardBrowserExtension icon indicating copy to clipboard operation
AdguardBrowserExtension copied to clipboard

Scriptlets and scripts are executed too late on website reload or navigation in MV2

Open kodiakhub opened this issue 1 year ago • 21 comments

Please answer the following questions for yourself before submitting an issue

  • [X] Filters were updated before reproducing an issue
  • [X] I checked the knowledge base and found no answer
  • [X] I checked to make sure that this issue has not already been filed

AdGuard Extension version

4.3.53

Browser version

Chrome 126.0.6478.127

OS version

Windows 10

Ad Blocking

AdGuard Base filter

Privacy

AdGuard Tracking Protection filter, AdGuard URL Tracking filter

Social

AdGuard Social Media filter

Annoyances

AdGuard Annoyances filter

Security

No response

Other

No response

Language-specific

AdGuard Turkish filter

What Stealth Mode options do you have enabled?

No response

Issue Details

First of all, the issue was that pop-ups were appearing on different sites when they were first opened or in certain situations and when I wanted to verify the issue again at same time, I could not reproduce it, so I did a test.

During the testing phase, I only used AdGuard filters in the uBO extension. There seems to be a issue with the AdGuard extension. It can also be noticed that the uBO extension functions faster and more fluidly than the AdGuard extension.

Steps to reproduce:

  1. Watch compare videos and reproduce the issue.

Expected Behavior

The AdGuard extension does not leak pop-ups.

Actual Behavior

The AdGuard extension leaks pop-ups.

Videos

AdGuard

https://github.com/AdguardTeam/AdguardBrowserExtension/assets/56846906/7aa1571d-e820-4fea-bad0-1d66f58cd751

uBO

https://github.com/AdguardTeam/AdguardBrowserExtension/assets/56846906/d9ce4924-c03b-4d5c-ad04-19329c9049e5

Additional Information

No response

kodiakhub avatar Jun 27 '24 10:06 kodiakhub

I guess that the problem might be like mentioned here - https://github.com/AdguardTeam/AdguardFilters/issues/182479#issuecomment-2198067258 It looks like that on website reload/navigation sometimes scriptlets are injected too late.

Steps to reproduce:

  1. Add these rules:
fiddle.jshell.net#%#//scriptlet('set-constant', 'alert', 'noopFunc')
fiddle.jshell.net#%#debugger;

2 Go to - https://jsfiddle.net/1tmha0g8/

Code:
<script>
debugger;
alert(1);
</script>

Alert dialog should be prevented by scriptlet.

On "hard reload", scriptlet is injected in time, but when I click on "Run" button or reload website then almost always rules are injected too late.

Video

https://github.com/AdguardTeam/AdguardBrowserExtension/assets/29142494/71aa6971-4577-44cb-826f-1d7a29dff90b

I have checked old version 4.2.110 and it doesn't occur in this version (I have checked this version, because it was easy to download it from - https://github.com/AdguardTeam/AdguardBrowserExtension/releases, newer versions require to build them).

AdamWr avatar Jun 29 '24 10:06 AdamWr

website reload/navigation

Also happens on first visit too.


Another issue: https://github.com/AdguardTeam/AdguardFilters/issues/182571

Refresh the page (no hard refresh) anti-adblock appears and some rare popups.

Update: This issue a bit different (may not related), take a look this video:

Video

https://github.com/AdguardTeam/AdguardBrowserExtension/assets/56846906/8ca0588a-cebd-44c4-b91e-e0debcc85409

Update 2: After some test, yes it's related. See the video:

Video 2

https://github.com/AdguardTeam/AdguardBrowserExtension/assets/56846906/4742e2ee-f341-4408-b728-ef280ff383dd

Update 3: On this comment, somehow issue has been fixed, see: https://github.com/AdguardTeam/AdguardFilters/issues/182902

kodiakhub avatar Jun 30 '24 13:06 kodiakhub

Possibility another one: https://github.com/AdguardTeam/AdguardFilters/issues/182802

kodiakhub avatar Jul 03 '24 16:07 kodiakhub

Possibility another one: AdguardTeam/AdguardFilters#182802

Just tested again 4.1.55 vs 4.3.53 see the video:

Video

https://github.com/AdguardTeam/AdguardBrowserExtension/assets/56846906/01019eac-ff97-4c2b-9fcb-aea9e5c7b1a0

This is a serious issue as end user.

kodiakhub avatar Jul 03 '24 16:07 kodiakhub

Another issue: https://github.com/AdguardTeam/AdguardFilters/issues/182795

Tested AdGuard extension v4.1.55 vs v4.3.53 see the video:

Video

https://github.com/AdguardTeam/AdguardBrowserExtension/assets/56846906/c301f3ce-4e38-43b7-b9d6-7d6fb9986026

I think different sites and different tests are enough. 😄

kodiakhub avatar Jul 03 '24 20:07 kodiakhub

Another issue https://github.com/AdguardTeam/AdguardFilters/issues/183315. The rules do not work in the extension 4.3.53. Perhaps they apply too late on the site. However, they work fine in the app.

cety.app#%#//scriptlet('prevent-addEventListener', 'click', 'openPopup')
cety.app#%#//scriptlet('set-constant', 'openPopup', 'noopFunc')
cety.app#%#//scriptlet('remove-node-text', 'script', 'openPopup')

To reproduce this, you should add the exception cety.app#@%#//scriptlet('abort-on-stack-trace', 'document.createElement', 'openPopup').

zloyden avatar Jul 10 '24 11:07 zloyden

https://github.com/AdguardTeam/AdguardFilters/issues/183713

javball.com#%#//scriptlet('prevent-fetch', 'pagead2.googlesyndication.com') javball.com#%#//scriptlet("abort-current-inline-script", "detectAdBlock") javball.com#%#//scriptlet('abort-on-property-write', 'detectAdBlock')

Alex-302 avatar Jul 19 '24 13:07 Alex-302

This is a valid issue. We were trying to be too smart about injecting scriptlets.

Here's what we do now:

  1. Compute cosmeticResult in the blocking handler (onBeforeRequested started).
  2. Try injecting it in onResponseStarted (usually, it fails).
  3. Then go through every lifetime phase of a request and retry injecting.
  4. Clean up on onErrorOccurred / onComplete.

We made this too complicated while the proper solution is much, much easier. There's absolutely no need for listening for every lifetime phase, etc.

Just do this:

        const injectDoTalogo = (tabId, injectDetails, attemptNumber = 0) => {
            console.log(`injecting to ${tabId}, attempt ${attemptNumber}`);

            // Use some reasonably big number, 100 or 1000.
            if (attemptNumber >= 100) {
                console.log('Stop trying, something is very wrong');
            }

            chrome.tabs.executeScript(tabId, injectDetails, () => {
                if (chrome.runtime.lastError) {
                    injectDoTalogo(tabId, injectDetails, attemptNumber + 1);
                } else {
                    console.log('Now we need to clean up');
                }
            });
        };

        chrome.webRequest.onResponseStarted.addListener((details) => {
            if (details.type !== 'main_frame') {
                return;
            }

            injectDoTalogo(details.tabId, {
                code: 'console.log(new Date().getTime() + " executing scripts!")',
                frameId: details.frameId,
                runAt: 'document_start',
                matchAboutBlank: true,
            });
        }, { urls: ['<all_urls>'] });

My testing shows that it guarantees the fastest injection.

It also simplifies the code a lot.

You can test it here: https://ameshkov.w3spaces.com/saved-from-Tryit-2024-07-19-ohc1y.html

Add this rule to AG to see the difference: ameshkov.w3spaces.com#%#console.log(${new Date().getTime()} adguard injection);

image

ameshkov avatar Jul 19 '24 14:07 ameshkov

This issue causes many problems. Like in reporting and even solving the problem.

See: https://github.com/AdguardTeam/AdguardFilters/commit/516850f2ba9148be38a9bbc9b907bbba0d4e3c81

Screenshot

image

I hope it gets fixed as soon as possible. 😄

kodiakhub avatar Aug 02 '24 17:08 kodiakhub

Same problem from time to time https://github.com/AdguardTeam/AdguardFilters/issues/188617

Alex-302 avatar Sep 12 '24 22:09 Alex-302

https://github.com/AdguardTeam/AdguardFilters/issues/188918#issuecomment-2356323687

Alex-302 avatar Sep 17 '24 16:09 Alex-302

Just reporting another case with 4.4.49 on Chrome. For https://github.com/uBlockOrigin/uAssets/issues/27316 uxsyplayer8d1f6edba292.click##+js(set, document.referrer, '') works reliably with uBO, but uxsyplayer8d1f6edba292.click#%#//scriptlet('set-constant', 'document.referrer', '') with AG fails on reload (almost 100%).

Yuki2718 avatar Feb 25 '25 17:02 Yuki2718

fix is to be available in v5.1.62

slavaleleka avatar Mar 09 '25 22:03 slavaleleka

Just reporting another case with 4.4.49 on Chrome. For uBlockOrigin/uAssets#27316 uxsyplayer8d1f6edba292.click##+js(set, document.referrer, '') works reliably with uBO, but uxsyplayer8d1f6edba292.click#%#//scriptlet('set-constant', 'document.referrer', '') with AG fails on reload (almost 100%).

Was the "Filtering log" window open during the test? Because I can only reproduce this issue when the "Filtering log" window is open, and I noticed that it slows down site navigation and causes blocked ads elements to briefly appear during page refresh before disappearing.

Edit: Oh it's related to v4.4.49. my comment is for, AdGuard AdBlocker MV2 v5.1.70 😄

kodiakhub avatar Apr 11 '25 09:04 kodiakhub

Tested on 5.1.70, uxsyplayer1a09531928c5.click#%#//scriptlet('set-constant', 'document.referrer', '') rather breaks the player, but I see this both with the logger opened and not opened.

Yuki2718 avatar Apr 11 '25 11:04 Yuki2718

Tested on 5.1.70, uxsyplayer1a09531928c5.click#%#//scriptlet('set-constant', 'document.referrer', '') rather breaks the player, but I see this both with the logger opened and not opened.

Are you encountering an error as shown in the screenshot?

Screenshot

Image

If so, this is likely due to the site's link tabs being offline. If not, how does it look on your side? On my end, there is no issue with the 'active' link tabs with this filter rule. 🤔

kodiakhub avatar Apr 11 '25 11:04 kodiakhub

Yeah, exactly that.

Yuki2718 avatar Apr 11 '25 11:04 Yuki2718

Yeah, exactly that.

In the 7/24 tab, the first two tab are offline, the 3rd and 4th are online, try it if you want.

kodiakhub avatar Apr 11 '25 12:04 kodiakhub

AdGuard AdBlocker MV2 v5.1.79

Another case: https://github.com/AdguardTeam/AdguardFilters/issues/204450#issuecomment-2847260628

With Filtering logs enabled, on a site that uses tab sections where each tab loads a different video player, switching tabs results in executed too late of the scriptlet filter rule.

kodiakhub avatar May 02 '25 14:05 kodiakhub

@kodiakhub I couldn't reproduce the issue with player with or without Filtering log enabled on 5.1/5.0. But we could reproduce this issue https://github.com/AdguardTeam/AdguardBrowserExtension/issues/3178 only with the Filtering log or Help with the development of AdGuard filters feature enabled and the fix should be available in 5.2.

If the issue is still relevant please wait until we release 5.2 version and check if the problem persists there.

alexx7311 avatar May 12 '25 15:05 alexx7311

If the issue is still relevant please wait until we release 5.2 version and check if the problem persists there.

Okay, I'll wait for 5.2. If it's still happening, I'll upload a video. 😄

kodiakhub avatar May 12 '25 15:05 kodiakhub