可以修改加载共享Web工作者中的工作者的机制吗? [英] Can the mechanism that loads the worker in Shared Web Workers be modified?

查看:130
本文介绍了可以修改加载共享Web工作者中的工作者的机制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅此相关问题以获取此背景信息:

Please see this related question for a background to this:

如何使用用户脚本加载共享Web worker?

考虑到这个问题,我想探索修改Shared Worker构造函数的可能性(在用户脚本的上下文中),以便负责加载Web worker的机制被GM函数替换 GM_xmlhttpRequest ,其工作方式类似于 XMLHttpRequest ,同时忽略相同的来源政策。

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.

为了清楚起见,我正在编写Stack Overflow的用户脚本,以帮助自己和其他人自动执行某个过程,我需要在两个打开的SO标签之间进行通信,这可以与共享Web工作者很好地完成,但是如果你看一下我引用的相关问题,那就有问题了。

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.

是否可以修改加载共享Web工作者中的工作者的机制?它是否使用页面的原生 XMLHttpRequest ,还是我们无法触及的内部函数?如果可以修改它,我如何访问它才能执行修改?

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?

推荐答案


你想用 SharedWorker 来实现什么目标?

在同一域的页面之间传递消息,而不必为
设置一个常量间隔来检查本地存储变量是否更改了
。 / p>

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

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

这篇关于可以修改加载共享Web工作者中的工作者的机制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆