W4G1 / multithreading

⚡ Multithreading functions in JavaScript to speedup heavy workloads, designed to feel like writing vanilla functions.
https://multithreading.io
MIT License
318 stars 10 forks source link

use this library for 3rd party scripts #2

Open andresclua opened 9 months ago

andresclua commented 9 months ago

hey - found this after 20 in search, and is not an issue, is more a question... could this library be used to load 3rd party scripts as google tag manager or analytics? I know that SW cannot access to the page, but I'm not an expert on this item.

I'm thinking something like...

html

<body>

      <!-- Example of custom type script -->
      <script type="type/multithreading">
        console.log('Script 1 is running.');
        // Additional JavaScript code for Script 1...
    </script>

    <script type="type/multithreading">
        console.log('Script 2 is running.');
        // Additional JavaScript code for Script 2...
    </script>

    <!-- Standard script to execute custom type scripts -->

    <div id="app"></div>
    <script type="module" src="/main.js"></script>

  </body>

js

import { threaded } from "multithreading";

const add = threaded(function* (a, b){
    document.querySelectorAll('script[type="type/multithreading"]').forEach(script => {
      try {
          new Function(script.innerText)();
      } catch (e) {
          console.error('Error executing multithreading script:', e);
      }
  });
});
await add()
W4G1 commented 5 months ago

Hi there, I'm afraid not, since Web Workers don't have direct access to the DOM, and analytics scripts often use the DOM to track user behavior.

Not saying it's completely impossible, but it would require a bidirectional Proxy of the whole Document Object Model to be present inside the worker, which would immensely increase the scope of this project.

andresclua commented 5 months ago

thank you so much @W4G1