唔一定要用class name
用react redux
// ==UserScript==
// @name lihkg-com-custom-1
// @namespace http://example.com/
// @version 0.1
// @description something
// @author me
// @match https://lihkg.com/*
// @grant none
// ==/UserScript==
(function() {
let bindListenr = () => {
let getThread = (state) => {
if (state.thread !== undefined && state.thread.thread !== undefined) {
return state.thread.thread;
}
return undefined;
}
let store = document.getElementById('app')._reactRootContainer._internalRoot.current.memoizedState.element.props.store;
let lastThread = undefined;
let dispatched = false;
let patchReply = (reply, nextFunc) => {
if (reply.quote !== undefined) {
return {
...reply,
user_nickname: reply.user_nickname + `(#${reply.user.user_id})`,
quote: nextFunc(reply.quote, nextFunc)
}
} else {
return {
...reply,
user_nickname: reply.user_nickname + `(#${reply.user.user_id})`,
}
}
}
let handleChange = () => {
let newState = store.getState();
let newThread = getThread(newState);
if (dispatched) {
dispatched = false;
return;
}
if (lastThread != newThread) {
lastThread = newThread;
if (newThread === undefined || newThread.patched == true) {
return
}
store.dispatch({
type: "SET_THREAD",
payload: {
append: false,
thread: {
...newState.thread.thread,
item_data: newState.thread.thread.item_data.map(
item => patchReply(item, patchReply)
),
pages: Object.fromEntries(
Object.entries(newState.thread.thread.pages).map(
([key, value]) => {
return [key, value.map(
item => patchReply(item, patchReply)
)];
}
)
),
patched: true
}
}
});
}
}
handleChange();
store.subscribe(handleChange);
}
window.addEventListener('load', () => {
setTimeout(bindListenr, 3000);
})
})();