我决定为 YouTube 实时聊天创建一个用户脚本。这是代码:
const toString = Function.prototype.toString
unsafeWindow.setTimeout = function (fn, t, ...args) {
unsafeWindow.console.log(fn, fn.toString(), toString.call(fn))
unsafeWindow.fns = (unsafeWindow.fns ?? []).concat(fn)
return setTimeout(fn, t, ...args)
}
Run Code Online (Sandbox Code Playgroud)
现在看看输出的样子:
一些函数的输出是可以预测的,但看看其他函数!当你这样做console.log
时,你会看到函数体,但如果你调用fn.toString()
,你会看到function () { [native code] }
。
但为什么?脚本在页面之前加载,因此 YouTube 的脚本无法替换这些方法。
https://doc.rust-lang.org/stable/std/sync/struct.Condvar.html
use std::sync::{Arc, Mutex, Condvar};
use std::thread;
let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair2 = Arc::clone(&pair);
// Inside of our lock, spawn a new thread, and then wait for it to start.
thread::spawn(move|| {
let (lock, cvar) = &*pair2;
let mut started = lock.lock().unwrap(); // #1
*started = true;
// We notify the condvar that the value has changed.
cvar.notify_one();
});
// Wait for the thread to start up.
let (lock, cvar) = &*pair;
let mut started = lock.lock().unwrap(); // …
Run Code Online (Sandbox Code Playgroud) 我有这个代码
from telethon.sync import TelegramClient, events
with TelegramClient('name', api_id, api_hash) as client:
@client.on(events.NewMessage(pattern=pattern))
async def handler(event):
await event.reply("Here should be the Chat or Group name")
Run Code Online (Sandbox Code Playgroud)
如何实施?
我有这样的代码:
enum Packet {
Quit,
Message {
text: String,
time: i32,
is_admin: bool,
},
}
Run Code Online (Sandbox Code Playgroud)
这很方便,但我不喜欢像这样的嵌套结构。想象一下,如果我需要此枚举中的更多项目,那么数据包定义将太大。那么,有没有办法让我将 Message 结构移到外部,然后将其名称写在 Packet 结构定义中的某个位置?我想过做这样的事情:
struct ChatMessage {
text: String,
time: i32,
is_admin: bool,
}
enum Packet {
Quit,
Message(ChatMessage),
}
Run Code Online (Sandbox Code Playgroud)
struct Message
(顺便说一句,我可以将结构命名为与 Packet ( , )中的项目相同的名称吗Message(Message)
?)
但是我必须let msg = message.0
这样做或类似的事情。如果这是唯一的解决方案 - 我同意,但如果有更简洁的解决方案,我会很高兴。
fn foo(ok: bool) -> Result<i32, i32> {
if ok { Ok(0) } else { Err(0) }
}
fn main() {
let Ok(x) | Err(x) = foo(true); // rust-analyzer error: top-level or-patterns are not allowed in `let` bindings
if let Ok(x) | Err(x) = foo(true) { // rust-analyzer warn: irrefutable `if let` pattern
println!("Working!");
}
}
Run Code Online (Sandbox Code Playgroud)
或者这是一个 Rust 分析器错误?我尝试用谷歌搜索但找不到任何东西。
The following is only an example. If there's a native solution for this exact problem with reading bytes - cool, but my goal is to learn how to do it by myself, for any other purpose as well.
I'd like to do something like this: (pseudo-code below)
let mut reader = Reader::new(bytesArr);
let int32: i32 = reader.read(); // separate implementation to read 4 bits and convert into int32
let int64: i64 = reader.read(); // separate implementation to read 8 bits …
Run Code Online (Sandbox Code Playgroud) 我有一个自定义类型,可以让我更方便地使用 API:
type Object map[string]interface{}
Run Code Online (Sandbox Code Playgroud)
这件事不起作用:
var mockResponse = map[string]interface{}{"success": true}
resp, ok := mockResponse.(Object)
// ok = false
Run Code Online (Sandbox Code Playgroud)
我可以做任何事情,这样mockResponse.(Object)
的ok
是true
?基本上都是一样的类型。。。
rust ×5
console.log ×1
docker ×1
function ×1
git ×1
git-rebase ×1
go ×1
javascript ×1
mutex ×1
python ×1
telegram ×1
telethon ×1
tostring ×1
windows-10 ×1