需要页面 javascript 的用户脚本在 JS 控制台中运行,但不在 Tampermonkey 中运行
Userscript, that requires page's javascript, runs in JS console but not in Tampermonkey
我有this userscript (see below), for 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.");
}
}
});
三件事:
- 是的,
window.pass
和window.locked
(共4个地方)是错误的。您将这些设置为脚本中的变量,脚本在不同的范围内运行。
- 令我惊讶的是该脚本竟然能正常工作,因为该脚本可以 运行 之前
MPP.client
是 defined/initialized。
- 正如 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.");
}
}
});
}
我有this userscript (see below), for 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.");
}
}
});
三件事:
- 是的,
window.pass
和window.locked
(共4个地方)是错误的。您将这些设置为脚本中的变量,脚本在不同的范围内运行。 - 令我惊讶的是该脚本竟然能正常工作,因为该脚本可以 运行 之前
MPP.client
是 defined/initialized。 - 正如 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.");
}
}
});
}