Skip to content
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

document.write() #33

Open
mindplay-dk opened this issue Sep 25, 2020 · 2 comments
Open

document.write() #33

mindplay-dk opened this issue Sep 25, 2020 · 2 comments
Labels
question Further information is requested

Comments

@mindplay-dk
Copy link

Just wondering, will this capture document.write() statements?

A lot of embeds and other snippets issue document.write() injections synchronously in head or body during page-load.

I'm wondering if MutationObserver would pick these up - and in IE11 in particular.

Might be good to have test-coverage for this?

@elbywan
Copy link
Owner

elbywan commented Sep 25, 2020

Hey @mindplay-dk, good question!

Just wondering, will this capture document.write() statements?

The answer seems to depend on the browser. I made a small test file to check it out:

<!DOCTYPE html>
<html>
  <head>
    <script>
      window.YETT_WHITELIST = [
        new RegExp("https://unpkg.com/yett")
      ]
    </script>
    <script src="https://unpkg.com/yett"></script>
    <script>
      document.write(`
        <script src="https://code.jquery.com/jquery-1.12.4.js"><\/script>
      `)
    </script>
  </head>
  <body>
    <button onclick="window.yett.unblock()">Unblock</button>
    <button onclick="alert(window.jQuery ? 'no' : 'yes')">does yett block scripts injected by document.write?</button>
  </body>
</html>

It works with Firefox v80, but not with Chrome v85.

On a side note, using document.write is very strongly discouraged even though I understand that some tracking companies are still using that.

With that said, a quick solution would be to group document.write contents into a single call:

<!-- instead of: -->
<script src="https://unpkg.com/yett"></script>
<script>
  document.write(`
    <script src="https://code.jquery.com/jquery-1.12.4.js"><\/script>
  `)
</script>

<!-- do: -->
<script>
  document.write(`
    <script src="https://unpkg.com/yett"><\/script>
    <script src="https://code.jquery.com/jquery-1.12.4.js"><\/script>
  `)
</script>

@elbywan elbywan added the question Further information is requested label Sep 25, 2020
@mindplay-dk
Copy link
Author

These are all third-party scripts - whether or how they use document.write is nothing I have control of.

I'm surprised this doesn't work in Chrome - I suspected this would be a problem in IE, but not in Chrome... 😶

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants