1

Please see this related question for a background to this:

How can I load a shared web worker with a user-script?

With that question in mind, I want to explore the possibility (in context of a user-script) of modifying the Shared Worker constructor so that the mechanism responsible for loading the web worker is replaced with the GM function GM_xmlhttpRequest, which works like XMLHttpRequest while ignoring same origin policies.

To be clear, I'm writing a user script for Stack Overflow to help automate a certain process for myself and others, and I need to communicate between two open SO tabs, which can be done nicely with a Shared Web Worker, however if you'll look at the related question I cited, there are problems with that.

Can the mechanism that loads the worker in Shared Web Workers be modified? Does it use the page's native XMLHttpRequest or is it some internal function which we can't touch? If it can be modified, how can I access it in order to perform the modification?

Community
  • 1
  • 1
  • You can also use `storage` event to communicate between tabs. Have not tried user-script, though should be possible using `.createObjectURL()` if at same origin? – guest271314 Aug 14 '16 at 00:11
  • @guest271314 Oh! My "Plan B" did involve using local or session storage to communicate but I was going to do something shameful to accomplish it: Check for a variable changes at a constant interval :( Thanks, that's much better! –  Aug 14 '16 at 00:13
  • See http://stackoverflow.com/questions/38034647/javascript-session-storage-variable-on-another-page , http://stackoverflow.com/questions/38343802/how-to-pass-data-from-one-html-page-to-second-in-php/ . May also be possible to use `filesystem:` if tabs both at same origin – guest271314 Aug 14 '16 at 00:14
  • What are you trying to achieve using `SharedWorker`? – guest271314 Aug 14 '16 at 00:22
  • @guest271314 just to pass messages between pages from the same domain without having to set a constant interval to check if a local storage variable had changed. I didn't realize the `storage` event reached all tabs for the domain. Much simpler alternative considering the difficulty of using a shared worker in a user-script. However I imagine someone might need to use a shared worker in a user-script, so this question is probably still useful. –  Aug 14 '16 at 00:46

1 Answers1

-1

What are you trying to achieve using SharedWorker?

to pass messages between pages from the same domain without having to set a constant interval to check if a local storage variable had changed.

You can use storage event to communicate messages between tabs at same origin

At index.html

<!DOCTYPE html>
<html>

  <head>
  </head>

  <body>
    <a href="b.html" target="_blank">open b.html</a>
    <textarea></textarea>
    <button>click</button>
    <script>
      var messages = [];
      var button = document.querySelector("button");
      var textarea = document.querySelector("textarea");
      window.addEventListener("storage", function(e) {
        console.log(e.newValue, JSON.parse(localStorage.getItem("messages")));

      });
      button.addEventListener("click", function() {
        messages.push(textarea.value);
        localStorage.setItem("messages", JSON.stringify(messages));
        localStorage.setItem("message", textarea.value);
        textarea.value = "";
      })
    </script>
    </body>
</html>

b.html

<!DOCTYPE html>
<html>

  <head>
  </head>

  <body>
    <textarea></textarea>
    <button>click</button>

    <script>
    var messages = JSON.parse(localStorage.getItem("messages"));
      window.addEventListener("storage", function(e) {
        console.log(e.newValue, JSON.parse(localStorage.getItem("messages")));
      });
      var button = document.querySelector("button");
      var textarea = document.querySelector("textarea");
      button.addEventListener("click", function() {
        messages.push(textarea.value);
        localStorage.setItem("message", textarea.value);
        localStorage.setItem("messages", JSON.stringify(messages));
        textarea.value = "";
      })
    </script>
    </body>
</html>

plnkr http://plnkr.co/edit/4nR7Rpu4zXSgTR831vQW?p=preview

guest271314
  • 1
  • 15
  • 104
  • 177