-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoast.js
99 lines (98 loc) · 3.32 KB
/
toast.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// ==UserScript==
// @name Toast
// @description 允许其它脚本调用 scriptio.toast,需要开启 LiteLoader Hook Vue
// @run-at main
// @reactive false
// @version 0.2.0
// @homepageURL https://github.com/PRO-2684/Scriptio-user-scripts/#toast
// @author PRO_2684
// @license gpl-3.0
// ==/UserScript==
(function () {
const $ = document.querySelector.bind(document);
const log = console.log.bind(console, "[Toast]");
// const log = () => {};
let toastEl = $(".q-toast");
let toastFunc = null;
async function waitToastToShow() {
return new Promise((resolve) => {
const observer = new MutationObserver((mut) => {
for (const m of mut) {
log("Mutation", m);
}
// Check if toast is created
toastEl = $(".q-toast");
if (toastEl) {
observer.disconnect();
resolve();
}
});
observer.observe(document.body, { childList: true, subtree: false, attributes: false });
});
}
async function waitToastToHide() {
return new Promise((resolve) => {
const observer = new MutationObserver(() => {
// Check if toast is removed
if (!toastEl.querySelector(".q-toast-item")) {
log("Toast removed");
observer.disconnect();
resolve();
}
});
log("toastEl", toastEl);
observer.observe(toastEl, { childList: true, subtree: false, attributes: false });
});
}
async function initToast() {
if (toastEl) {
toastFunc = toastEl?.__VUE__?.[1]?.proxy?.setNewToast;
if (!toastFunc) {
log("Failed to get toast function - try installing hook-vue.js first");
return false;
}
return true;
}
const btn = $(".version-tip-button");
if (!btn) {
log("Version button not found");
return false;
}
const style = document.head.appendChild(document.createElement("style"));
style.id = "scriptio-toast";
style.textContent = `
.q-toast > .q-toast-item {
display: none;
}
`;
promise = waitToastToShow();
btn.dispatchEvent(new MouseEvent("dblclick"), { bubbles: true });
log("Version button double clicked");
await promise;
log("Toast created");
toastFunc = toastEl?.__VUE__?.[1]?.proxy?.setNewToast;
await waitToastToHide();
log("Toast removed");
style.remove();
if (!toastFunc) {
log("Failed to get toast function - try installing hook-vue.js first");
return false;
}
return true;
}
async function toast(message, duration = 1500, type = "default") {
// type: default, success, error
if (!await initToast()) {
return false;
}
toastFunc({
content: message,
duration: duration,
type: type,
noRepeat: true,
});
};
scriptio.register("toast", toast);
// Example usage:
// scriptio.wait("toast").then(toast => toast("Hello, world!"));
})();