【发布时间】:2018-05-11 08:06:55
【问题描述】:
我有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.");
}
}
});
【问题讨论】:
-
你的意思是
pass不是pass1- 如果pass在脚本开头引用var pass,那么,可能 - 你试过了吗?你的其他用户脚本工作正常吗? -
我还将
window.locked更改为locked...并且(不重要,但更好的代码)使用true/false而不是"true"/"false" -
我在这个 pastebin pastebin.com/FtY8bFpg 中用
// <===标记了更改 - 看看是否有帮助 - 我也会进行类似的更改 here in your other script
标签: javascript greasemonkey userscripts tampermonkey