User scripts extensions for PC Editor not working anymore

I was playing a while ago with the Editor API and the example shown on that page and it used to work, it was creating an additional button in the scene view.
I tried it again these days and it doesn’t seem to work.
I created a small user script to test that:

// ==UserScript==
// @name        PC Editor Extension Test
// @namespace   Violentmonkey Scripts
// @match       https://playcanvas.com/editor/scene/*
// @grant       none
// @version     1.0
// @author      -
// @description 20/10/2021, 11:40:21
// ==/UserScript==

(function() {

function onLoad() {
  console.log("Editor loaded.");
}

console.log("IT WORKS");
// Wait until the Editor is available before adding the button
editor.once("load", () => onLoad());
})();

and it only logs the IT WORKS message, the other console log is not shown which makes me believe that either the event is not registered or it is registered too late and the load event was already sent by the time we register for it inside the user script.

I’ve tested with Edge and Firefox browsers and with ViolentMonkey and TamperMoney extensions

A previous user mentioned that you have to load the scene URL directly to get my user scripts to work: [SOLVED] PlayCanvas EditorTools is not work on my project - #8 by n.katsu

I’m not sure what has changed but we have gone through a number of changes over the last few months.

@Leonidas Can you suggest a more robust method?

1 Like

I’ve had a similar issue recently. I tracked it down to the editor load event listener: editor.once('load', () => createButton());

For some reason, the load event isn’t firing inside of the user script. Is it possible the recent editor changes are firing the load event earlier, before the event listener is added inside the user script?

My workaround is to simply call createButton() at the end of my userscript and not wait for the load event. Of course I still have to wait until the editor finishes loading before the button will work, but that’s no big deal.

2 Likes

Hmm, not sure about that but indeed that event didn’t fire for us too.

Instead we wait for the assets load event as an alternative to start our custom scripts:

editor.once('assets:load', function () {
});
3 Likes

Thanks a lot, that works!