如下
// ==UserScript==
// @name 洛谷私信自动回复
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 洛谷私信自动回复
// @author d0j1a_1701
// @match https://*.luogu.com.cn/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=luogu.com.cn
// ==/UserScript==
const message = '[自动回复]你好,我现在有事不在,一会再和你联系。';
const usr = window._feInjection.currentUser
async function reply(uid){
await fetch("https://www.luogu.com.cn/api/chat/new", {
headers: [
["content-type", "application/json"],
["referer", "https://www.luogu.com.cn/"],
["x-csrf-token", document.querySelector("meta[name=csrf-token]").content],
],
body: JSON.stringify({
user: uid,
content: message,
}),
method: "POST",
});
}
(function() {
'use strict';
if (window.location.href.search("/chat") != -1) return;
console.log(window._feInjection);
const socket = new WebSocket("wss://ws.luogu.com.cn/ws");
socket.onopen = () => socket.send(JSON.stringify({
type: "join_channel",
channel: "chat",
channel_param: String(usr.uid),
exclusive_key: null,
}));
socket.onmessage = (e) => {
const u = JSON.parse(e.data);
if (u._ws_type === "server_broadcast" && u.message instanceof Object && u.message.sender.uid !== usr.uid) {
reply(u.message.sender.uid)
console.log(`msg from ${u.message.sender.uid}`)
}
};
})();
window._feInjection.currentUser总是取到null
无论怎样修改脚本注入位置都无效
但在调试器中运行又正常