1

我正在编写一个 Greasemonkey 脚本,如下所示:

// ==UserScript==
// @name        Button
// @namespace   http://test.com
// @include     *
// @version     1
// ==/UserScript==

//Creating the button input starts
var input=document.createElement("input");
input.type="button";
input.value="Process";
input.onclick = runProcess;
input.setAttribute("style", "font-size:18px;position:absolute;right: 250px; top: 50px; z-index:10000;");
document.body.appendChild(input);
//creating the button input ends

function runProcess()
{
  
  console.log("The function has started");
   
  for(var j = 0 ; j < 32 ; j++ ){
    var waves = localStorage.getItem("sendingArray");
    console.log("Right before opening new window");
    var w = window.open(waves);
     console.log("Windows object");
    console.log(w);
    
    w.onload = function(){
      console.log("I wish to run a piece of code here, which is ready and working separately. Making this console statement as a representation of the code that I need to run here.");
    }
    w.close();
  }
   console.log("We've reached the end here");
}

我已经抽象出不需要的代码,但本质上,这就是代码的样子。现在,我面临的问题是我希望在onloadwindow.open 事件中运行一些其他代码,而这是我无法实现的。Greasemonkey 一旦遇到w.onload语句就会停止处理,我无法弄清楚我到底做错了什么。

我在 Stack Overflow 上看到了一些答案,这让我认为这可能与权限有关,因为 GM 在沙箱中运行(?)

如果你们中的一个人可以请看一下脚本并告诉我我做错了什么以及如何使它工作。我猜我必须修改顶空,但是如何以及什么?

编辑:我可以打开新窗口,这不是问题。问题是我没有得到新打开的选项卡的参考,因此我无法w.onload在它们上使用,这就是我认为失败的原因。

4

0 回答 0