需要页面javascript的Userscript在JS控制台中运行,但在Tampermonkey中运行

时间:2018-05-10 22:52:36

标签: javascript greasemonkey userscripts tampermonkey

我有this userscript(见下文),http://multiplayerpiano.com 在控制台中,脚本按预期运行,但是当用作Tampermonkey的脚本时,它不会。

我不知道为什么。命令工作正常,但第21行和第30行之间的禁止功能什么都不做。即使在详细模式下也不会抛出任何错误。非常感谢帮助。

是否与if语句中的window.pass1有关,pass1可能只有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."); } } });

propaga( [(1,1),(1,2),(1,3),(1,4)],(1,3),L).
L = [(1,1),(1,2),(1,3)].

1 个答案:

答案 0 :(得分:1)

三件事:

  1. 是的,window.passwindow.locked(总共4个位置)是错误的。您可以在脚本中将它们设置为vars,并且脚本在不同的范围内运行。
  2. 我对脚本的工作感到惊讶,因为脚本可以在定义/初始化MPP.client之前运行。
  3. 正如Jaromanda X所指出的那样,对布尔语使用布尔值,而不是字符串。
  4. 因此,强有力的做法是在触发依赖于它们的代码之前等待目标页面函数存在。

    以下是您重构的用户脚本:

    // ==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.");
                }
            }
        });
    }
    
相关问题