Skip to content
Advertisement

How can I find and access a JavaScript property (‘Blazor’) of ‘window’ with a (GreaseMonkey) user script?

With the following (GreaseMonkey) user script I am trying to find (and later access) a JavaScript object (namely ‘Blazor’):

// ==UserScript==
// @name         Test
// @version      1
// @grant        none
// @run-at       document-end
// ==/UserScript==

window.addEventListener ("load", doStuff);

function doStuff() { 
    setTimeout(function() {
        console.log("window.hasProperty('Blazor'): " + window.hasOwnProperty("Blazor"));
        console.log("'Blazor' in window:           " + ('Blazor' in window));
        console.log(window);
    }, 2000);    
}

When I run the script (i.e., reload the page), I get the following output in Firefox’ console:

Screenshot of the Firefox JavaScript console

So window.Blazor can not be found. However, when I inspect (click on) the window object I can see the Blazor property:

Screenshot of the Firefox JavaScript console

Calling 'Blazor' in window in the console also returns true.

Why is this property of window not (yet?) available and how can I find and access it?

Update:

Based on madalinivascu’s answer I tried the following:

function doStuff() {
  console.log("<doStuff>");
  
  var promise = new Promise(function(resolve, reject) {
    var interval = setInterval(function() {
      console.log("checking for Blazor");
      if ('Blazor' in window) {
        console.log("Blazor found");
        clearInterval(interval);
        resolve(window.Blazor);
      }
    }, 1000);
  });
  
  promise.then(successCallback, failureCallback);
  
  console.log("</doStuff>");
}

function failureCallback(error) {
  console.log("failure: " + error);
}

function successCallback(result) {
  console.log("success: " + result);
}

which results in the following output:

Screenshot of the Firefox console

So the function checking for Blazor is called indefinitely without success in finding it.

Advertisement

Answer

The solution is to use script injection, since the GreaseMonkey script runs in another context and thus gets another window object.

More precisely, attaching the function doStuff with addJS_Node defined in that answer (addJS_Node (null, null, doStuff);) instead of using window.addEventListener ("load", doStuff); works.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement