Userscript,需要第'页;s javascript,在JS控制台中运行,但不在Tampermonkey中运行
我已经(见下文)了。Userscript,需要第'页;s javascript,在JS控制台中运行,但不在Tampermonkey中运行,javascript,greasemonkey,userscripts,tampermonkey,Javascript,Greasemonkey,Userscripts,Tampermonkey,我已经(见下文)了。 在控制台中,脚本按预期运行,但当用作Tampermonkey的脚本时,它就不能运行了 我不知道为什么。命令工作正常,但第21行和第30行之间的禁止功能没有任何作用。即使在详细模式下,也不会抛出错误。非常感谢您的帮助 它是否与if语句中的窗口有关。pass1,它可能只是不带窗口的pass1 // ==UserScript== // @name Josh's MPP Room Locker // @description Lock an MPP room and only al
在控制台中,脚本按预期运行,但当用作Tampermonkey的脚本时,它就不能运行了 我不知道为什么。命令工作正常,但第21行和第30行之间的禁止功能没有任何作用。即使在详细模式下,也不会抛出错误。非常感谢您的帮助 它是否与if语句中的
窗口有关。pass1
,它可能只是不带窗口的pass1
// ==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.");
}
}
});
三件事:
是,window.pass
和window.locked
(共4位)错误。在脚本中将这些变量设置为vars,脚本在不同的范围内运行
我很惊讶这个脚本居然能工作,因为脚本可以在定义/初始化MPP.client
之前运行
正如Jaromanda X所指出的,对布尔值使用布尔值,而不是字符串
因此,健壮的做法是等待目标页面函数存在,然后再触发依赖它们的代码
以下是经过重构的用户脚本:
// ==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.");
}
}
});
}
你的意思是pass
不是pass1
——如果pass
在脚本开头提到var pass
,那么,很可能-你试过了吗?您的其他用户脚本工作正常吗?我还需要将窗口.锁定
更改为仅锁定
。。。(不重要,但更好的代码)使用true
/false
而不是“true”
/“false”
我已经用标记了更改//非常感谢!我想我没想清楚。这就行了。我应该在其他用户脚本上也做同样的初始化计时器吗?我想是的,不一定。只是那些依赖于页面JS的页面。(大多数脚本不需要。)根据页面/JS的不同,您可能并不总是需要它。但这从来都不会有什么坏处,而且比赛条件的缺陷可能是最难确定的。