Update trigger button functionality and remove unused code

This commit is contained in:
kwaroran
2024-04-10 10:34:45 +09:00
parent 1a750dc6c6
commit 3d7d34334c
4 changed files with 54 additions and 23 deletions

View File

@@ -4,7 +4,6 @@ import App from "./App.svelte";
import { loadData } from "./ts/storage/globalApi";
import { initHotkey } from "./ts/hotkey";
import { polyfill } from "./ts/polyfill";
import { watchParamButton } from "./ts/plugins/embedscript";
let app: App;
try {
@@ -16,7 +15,6 @@ try {
loadData()
initHotkey()
watchParamButton()
} catch (error) {
console.error(error, error.stack)
alert(error)

52
src/ts/observer.ts Normal file
View File

@@ -0,0 +1,52 @@
import { get } from "svelte/store";
import { runTrigger } from "./process/triggers";
import { CurrentCharacter, CurrentChat } from "./stores";
import { runCharacterJS } from "./plugins/embedscript";
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(btnEvent){
node.addEventListener('click',async ()=>{
await runCharacterJS({
code: null,
mode: 'onButtonClick',
data: btnEvent
})
}, {passive: true})
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true,
attributeFilter: ['risu-trigger', 'risu-btn'],
});
}

View File

@@ -196,7 +196,7 @@ export async function ParseMarkdown(data:string, charArg:(character|simpleCharac
if(db.automark){
return (DOMPurify.sanitize(autoMarkNew(data), {
ADD_TAGS: ["iframe", "style", "risu-style", "x-em"],
ADD_ATTR: ["allow", "allowfullscreen", "frameborder", "scrolling", "risu-btn"],
ADD_ATTR: ["allow", "allowfullscreen", "frameborder", "scrolling", "risu-btn", 'risu-trigger'],
}))
}
else{
@@ -204,7 +204,7 @@ export async function ParseMarkdown(data:string, charArg:(character|simpleCharac
data = mconverted.parse(data)
return decodeStyle(DOMPurify.sanitize(data, {
ADD_TAGS: ["iframe", "style", "risu-style", "x-em"],
ADD_ATTR: ["allow", "allowfullscreen", "frameborder", "scrolling", "risu-btn"],
ADD_ATTR: ["allow", "allowfullscreen", "frameborder", "scrolling", "risu-btn", 'risu-trigger'],
}))
}
}

View File

@@ -324,23 +324,4 @@ export async function runCharacterJS(arg:{
return arg.data
}
}
export async function watchParamButton() {
while(true){
const qs = document.querySelectorAll('*[risu-btn]:not([risu-btn-run="true"])')
for(let i = 0; i < qs.length; i++){
const q = qs[i]
const code = q.getAttribute('risu-btn')
q.setAttribute('risu-btn-run','true')
q.addEventListener('click',async ()=>{
await runCharacterJS({
code: null,
mode: 'onButtonClick',
data: code
})
})
}
await sleep(100)
}
}