AdguardTeam / Scriptlets

AdGuard scriptlets library
GNU General Public License v3.0
138 stars 27 forks source link

Improve prevent-xhr — modify response #415

Open AdamWr opened 3 months ago

AdamWr commented 3 months ago

Basically the same issue as here - https://github.com/AdguardTeam/Scriptlets/issues/199#issuecomment-1063271758, but for response (currently only responseText is modified).

Steps to reproduce:

  1. Add this rule:
    example.org#%#//scriptlet('prevent-xhr', 'pagead2.googlesyndication.com')
  2. Go to - https://example.org/
  3. Run this script:

    (() => {
    function reqListener() {
    if (this.status !== 200 || this.responseText === "" || this.response === "") {
      alert("adblock");
    } else {
      (() => {
        let video = '<iframe width="560" height="315" src="https://www.youtube.com/embed/Fy2rtb95QhY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
        let createDiv = document.createElement("div");
        document.body.appendChild(createDiv);
        createDiv.innerHTML = video;
      })();
    }
    }
    
    var oReq = new XMLHttpRequest();
    oReq.addEventListener("load", reqListener);
    oReq.open("GET", "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js");
    oReq.send();
    })();

    Alert will be displayed, because this.response returns "". If pagead2.googlesyndication.com/pagead/js/adsbygoogle.js is not blocked then video player is loaded.

In this line: https://github.com/AdguardTeam/Scriptlets/blob/804c53527008e62ffcd296e3f36543c472b96fe5/src/scriptlets/prevent-xhr.js#L206 if response is a text then it probably should be set to modifiedResponseText.