Greasemonkey Window.open.onload not working

584 Views Asked by At

I am writing a Greasemonkey script which looks like the following:

// ==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");
}

I have abstracted out the code that wasn't required but essentially, this is what the code looks like. Now, the problem that I am facing is that I wish to run some other code inside the onload event of window.open, and that's what I am not able to achieve. Greasemonkey stops processing once it hits the w.onload statement, and i am not able to figure out what exactly am I doing wrong.

I have seen some answers here on Stack Overflow and that has made me think that perhaps it has something to do with permissions as GM runs inside a sandbox(?)

If one of you can please look at the script and tell me what am I doing wrong and how do I make it work. I am guessing I have to modify the headspace, but how and what?

EDIT: I am able to open new windows, that is not a problem. The problem is that I don't get a reference for the new opened tab, because of which I am not able to use w.onload on them, and that's why I think that fails.

0

There are 0 best solutions below