Added button to trigger script
This commit is contained in:
@@ -4,43 +4,59 @@ import { CurrentCharacter, CurrentChat } from "./stores";
|
||||
import { runCharacterJS } from "./plugins/embedscript";
|
||||
|
||||
|
||||
function nodeObserve(node:HTMLElement){
|
||||
const triggerName = node.getAttribute('risu-trigger');
|
||||
const btnEvent = node.getAttribute('risu-btn');
|
||||
const observerAdded = node.getAttribute('risu-observer');
|
||||
|
||||
if(observerAdded){
|
||||
return
|
||||
}
|
||||
if(triggerName){
|
||||
node.addEventListener('click', async () => {
|
||||
const currentChar = get(CurrentCharacter)
|
||||
if(currentChar.type === 'group'){
|
||||
return;
|
||||
}
|
||||
const triggerResult = await runTrigger(currentChar, 'manual', {
|
||||
chat: get(CurrentChat),
|
||||
manualName: triggerName,
|
||||
});
|
||||
|
||||
if(triggerResult){
|
||||
CurrentChat.set(triggerResult.chat);
|
||||
}
|
||||
|
||||
}, {
|
||||
passive: true,
|
||||
});
|
||||
node.setAttribute('risu-observer', 'true');
|
||||
return
|
||||
}
|
||||
|
||||
if(btnEvent){
|
||||
node.addEventListener('click',async ()=>{
|
||||
await runCharacterJS({
|
||||
code: null,
|
||||
mode: 'onButtonClick',
|
||||
data: btnEvent
|
||||
})
|
||||
}, {passive: true})
|
||||
node.setAttribute('risu-observer', 'true');
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
export function startObserveDom(){
|
||||
const observer = new MutationObserver((mutations) => {
|
||||
mutations.forEach((mutation) => {
|
||||
const node = mutation.target as HTMLElement;
|
||||
const triggerName = node.getAttribute('risu-trigger');
|
||||
const btnEvent = node.getAttribute('risu-btn');
|
||||
|
||||
if(triggerName){
|
||||
node.addEventListener('click', async () => {
|
||||
const currentChar = get(CurrentCharacter)
|
||||
if(currentChar.type === 'group'){
|
||||
return;
|
||||
}
|
||||
const triggerResult = await runTrigger(currentChar, 'manual', {
|
||||
chat: get(CurrentChat),
|
||||
manualName: triggerName,
|
||||
});
|
||||
|
||||
if(triggerResult){
|
||||
CurrentChat.set(triggerResult.chat);
|
||||
}
|
||||
|
||||
}, {
|
||||
passive: true,
|
||||
});
|
||||
if(node.nodeType !== 1){
|
||||
return;
|
||||
}
|
||||
|
||||
if(btnEvent){
|
||||
node.addEventListener('click',async ()=>{
|
||||
await runCharacterJS({
|
||||
code: null,
|
||||
mode: 'onButtonClick',
|
||||
data: btnEvent
|
||||
})
|
||||
}, {passive: true})
|
||||
}
|
||||
|
||||
node.querySelectorAll('[risu-trigger]').forEach(nodeObserve);
|
||||
node.querySelectorAll('[risu-btn]').forEach(nodeObserve);
|
||||
nodeObserve(node)
|
||||
});
|
||||
});
|
||||
|
||||
@@ -48,5 +64,6 @@ export function startObserveDom(){
|
||||
childList: true,
|
||||
subtree: true,
|
||||
attributeFilter: ['risu-trigger', 'risu-btn'],
|
||||
attributes: true,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user