SYZYGY-DEV 333
SYZYGY-DEV 333

Reputation: 155

Userscript, that requires page's javascript, runs in JS console but not in Tampermonkey

I have this userscript (see below), for http://multiplayerpiano.com.
In the console, the script runs as expected, but when used as a script for Tampermonkey, it just doesn't.

I don't know why. The commands work fine, but the banning function between lines 21 and 30 does nothing. No errors are thrown, even in verbose mode. Help would be much appreciated.

Does it have to do with the window.pass1 in the if statement, which might should be simply pass1 without the window?

// ==UserScript==
// @name Josh's MPP Room Locker
// @description Lock an MPP room and only allow entrance if the name is set to the passphrase
// @namespace Copyright 2018 SYZYGY-DEV333; licensed under Apache v2
// @version 0.1
// @author Josh (SYZYGY-DEV333)
// @match http://www.multiplayerpiano.com/*
// @match https://www.multiplayerpiano.com/*
// @match http://ourworldofpixels.com/piano/*
// @grant none
// ==/UserScript==

var pass = "passphrase";

var locked = "false";

function kickban(id, ms) {
    MPP.client.sendArray([{m: "kickban", _id: id, ms: ms}]);
}

MPP.client.on("participant added", function(pp) {
    if (locked == "true") {
        if (MPP.client.channel.crown.userId == MPP.client.getOwnParticipant()._id) {
            if (pp.name == window.pass) {
            } else {
                kickban(pp._id, 10000);
            }
        }
    }
});

MPP.client.on('a', function(m) {
    if (m.a == '-lock') {
        if (m.p._id == MPP.client.getOwnParticipant()._id) {
            window.locked = "true";
            MPP.chat.send("Room Locked.");
        }
    } else if (m.a == '-unlock') {
        if (m.p._id == MPP.client.getOwnParticipant()._id) {
            window.locked = "false";
            MPP.chat.send("Room Unlocked.");
        }
    } else if (m.a.startsWith('-setpass')) {
        if (m.p._id == MPP.client.getOwnParticipant()._id) {
            window.pass = m.a.slice(9);
            MPP.chat.send("Passphrase set to: "+m.a.slice(9));
        }
    } else if (m.a == '-help') {
        if (m.p._id == MPP.client.getOwnParticipant()._id) {
            MPP.chat.send("[[ Josh's MPP Room Locker v0.1 ]]");
            MPP.chat.send("-lock -- Locks room.");
            MPP.chat.send("-unlock -- Unlocks room.");
            MPP.chat.send("-setpass [pass] -- sets a passphrase for entry.");
            MPP.chat.send("All users must have this as their name when entering the room.");
            MPP.chat.send("-help -- displays this help message.");
        }
    }
});

Upvotes: 1

Views: 615

Answers (1)

Brock Adams
Brock Adams

Reputation: 93443

Three things:

  1. Yes, window.pass and window.locked (4 places total) is wrong. You set these as vars in the script and the script operates in a different scope.
  2. I'm surprised the script worked at all, since the script could run before MPP.client is defined/initialized.
  3. As Jaromanda X pointed out, use booleans, not strings, for booleans.

So, the robust thing to do is to wait for the target page functions to exist before firing code that depends on them.

Here is your userscript refactored to do all that:

// ==UserScript==
// @name Josh's MPP Room Locker
// @description Lock an MPP room and only allow entrance if the name is set to the passphrase
// @namespace Copyright 2018 SYZYGY-DEV333; licensed under Apache v2
// @version 0.5
// @author Josh (SYZYGY-DEV333)
// @match http://www.multiplayerpiano.com/*
// @match https://www.multiplayerpiano.com/*
// @match http://ourworldofpixels.com/piano/*
// @grant none
// ==/UserScript==

var pass = "passphrase";
var locked = false;

var initTmr = setInterval ( () => {
    if (typeof MPP === "object"  &&  typeof MPP.client === "object") {
        clearInterval (initTmr);
        startMyCode ();
    }
}, 200); 

function kickban (id, ms) {
    MPP.client.sendArray([{m: "kickban", _id: id, ms: ms}]);
}

function startMyCode () {
    MPP.client.on("participant added", function(pp) {
        if (locked === true) {
            if (MPP.client.channel.crown.userId == MPP.client.getOwnParticipant()._id) {
                if (pp.name == pass) {
                } else {
                    kickban(pp._id, 10000);
                }
            }
        }
    });
    MPP.client.on('a', function(m) {
        if (m.a == '-lock') {
            if (m.p._id == MPP.client.getOwnParticipant()._id) {
                locked = true;
                MPP.chat.send("Room Locked.");
            }
        } else if (m.a == '-unlock') {
            if (m.p._id == MPP.client.getOwnParticipant()._id) {
                locked = false;
                MPP.chat.send("Room Unlocked.");
            }
        } else if (m.a.startsWith('-setpass')) {
            if (m.p._id == MPP.client.getOwnParticipant()._id) {
                pass = m.a.slice(9);
                MPP.chat.send("Passphrase set to: "+m.a.slice(9));
            }
        } else if (m.a == '-help') {
            if (m.p._id == MPP.client.getOwnParticipant()._id) {
                MPP.chat.send("[[ Josh's MPP Room Locker v0.1 ]]");
                MPP.chat.send("-lock -- Locks room.");
                MPP.chat.send("-unlock -- Unlocks room.");
                MPP.chat.send("-setpass [pass] -- sets a passphrase for entry.");
                MPP.chat.send("All users must have this as their name when entering the room.");
                MPP.chat.send("-help -- displays this help message.");
            }
        }
    });
}

Upvotes: 1

Related Questions