Skip to content

Commit af9753c

Browse files
committed
chore: Update API usage
1 parent 3b882ed commit af9753c

3 files changed

Lines changed: 93 additions & 47 deletions

File tree

modules/loaders/unified.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Unified API for LiteLoader & QwQNT, main & renderer
1+
// Unified API for ~~LiteLoader &~~ QwQNT, main & renderer
22

33
import {
44
dataPathOrig,

modules/renderer/settings.js

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { setupEasterEggs } from "./eggs.js";
55
import { scriptPath, pluginPath, scriptioVersion } from "../loaders/unified.js";
66

77
/** Scriptio plugin uri */
8-
const pluginUri = window.LiteLoader ? `local:///${pluginPath}` : qwqnt.framework.protocol.pathToStorageUrl(pluginPath);
8+
const pluginUri = qwqnt.framework.protocol.pathToStorageUrl(pluginPath);
99
/** Attribute of `<setting-item>` that stores the script path */
1010
const configDataAttr = "data-scriptio-config";
1111
/** Attribute of `<setting-switch>` that stores the script path */
1212
const switchDataAttr = "data-scriptio-switch";
1313
/** Attribute of `<setting-item>` that indicates the script is deleted */
1414
const deletedDataAttr = "data-deleted";
1515
/** Function to manually trigger the search (re-search) */
16-
let research = () => { }; // Placeholder for the search function
16+
let research = () => {}; // Placeholder for the search function
1717

1818
/** Function to add a button to the right side of the setting item.
1919
* @param {Element} right The right side element.
@@ -43,19 +43,37 @@ function addItem(path, container) {
4343
itemDesc.setAttribute("data-type", "secondary");
4444
const right = item.appendChild(document.createElement("div"));
4545
right.classList.add("scriptio-menu");
46-
const homepage = addScriptioMore(right, { icon: "🔗", title: "打开脚本主页", className: "scriptio-homepage"});
46+
const homepage = addScriptioMore(right, {
47+
icon: "🔗",
48+
title: "打开脚本主页",
49+
className: "scriptio-homepage",
50+
});
4751
homepage.addEventListener("click", () => {
48-
if (!item.hasAttribute(deletedDataAttr) && !homepage.hasAttribute("disabled")) {
49-
scriptio_internal.open("link", homepage.getAttribute("data-homepage-url"));
52+
if (
53+
!item.hasAttribute(deletedDataAttr) &&
54+
!homepage.hasAttribute("disabled")
55+
) {
56+
scriptio_internal.open(
57+
"link",
58+
homepage.getAttribute("data-homepage-url"),
59+
);
5060
}
5161
});
52-
const remove = addScriptioMore(right, { icon: "🗑️", title: "删除此脚本", className: "scriptio-remove"});
62+
const remove = addScriptioMore(right, {
63+
icon: "🗑️",
64+
title: "删除此脚本",
65+
className: "scriptio-remove",
66+
});
5367
remove.addEventListener("click", () => {
5468
if (!item.hasAttribute(deletedDataAttr)) {
5569
scriptio_internal.removeScript(path);
5670
}
5771
});
58-
const showInFolder = addScriptioMore(right, { icon: "📂", title: "在文件夹中显示", className: "scriptio-folder"});
72+
const showInFolder = addScriptioMore(right, {
73+
icon: "📂",
74+
title: "在文件夹中显示",
75+
className: "scriptio-folder",
76+
});
5977
showInFolder.addEventListener("click", () => {
6078
if (!item.hasAttribute(deletedDataAttr)) {
6179
scriptio_internal.open("show", scriptPath + path);
@@ -67,7 +85,10 @@ function addItem(path, container) {
6785
switch_.addEventListener("click", () => {
6886
if (!item.hasAttribute(deletedDataAttr)) {
6987
switch_.parentNode.classList.toggle("is-loading", true);
70-
scriptio_internal.configChange(path, switch_.toggleAttribute("is-active")); // Update the UI immediately, so it would be more smooth
88+
scriptio_internal.configChange(
89+
path,
90+
switch_.toggleAttribute("is-active"),
91+
); // Update the UI immediately, so it would be more smooth
7192
}
7293
});
7394
return item;
@@ -102,17 +123,22 @@ async function initScriptioSettings(view) {
102123
console.log("[Scriptio] Ignored", file.name);
103124
continue;
104125
}
105-
promises.push(new Promise((resolve, reject) => {
106-
cnt++;
107-
console.log("[Scriptio] Importing", file.name);
108-
let reader = new FileReader();
109-
reader.onload = () => {
110-
scriptio_internal.importScript(file.name, reader.result);
111-
console.log("[Scriptio] Imported", file.name);
112-
resolve();
113-
};
114-
reader.readAsText(file);
115-
}));
126+
promises.push(
127+
new Promise((resolve, reject) => {
128+
cnt++;
129+
console.log("[Scriptio] Importing", file.name);
130+
let reader = new FileReader();
131+
reader.onload = () => {
132+
scriptio_internal.importScript(
133+
file.name,
134+
reader.result,
135+
);
136+
console.log("[Scriptio] Imported", file.name);
137+
resolve();
138+
};
139+
reader.readAsText(file);
140+
}),
141+
);
116142
}
117143
await Promise.all(promises);
118144
this.parentNode.classList.toggle("is-loading", false);
@@ -128,36 +154,42 @@ async function initScriptioSettings(view) {
128154
// Dev mode
129155
const dev = $("#scriptio-dev");
130156
dev.addEventListener("click", devMode);
131-
scriptio_internal.queryDevMode().then(enabled => {
157+
scriptio_internal.queryDevMode().then((enabled) => {
132158
log("queryDevMode", enabled);
133159
dev.toggleAttribute("is-active", enabled);
134160
});
135161
// Debug mode
136162
showDebugHint(view);
137163
// Buttons
138-
$("#scriptio-reload").addEventListener("dblclick", scriptio_internal.reload);
164+
$("#scriptio-reload").addEventListener(
165+
"dblclick",
166+
scriptio_internal.reload,
167+
);
139168
$("#scriptio-open-folder").addEventListener("click", () => {
140169
openURI("path", scriptPath);
141170
});
142171
$("#scriptio-import").addEventListener("change", importScript);
143172
// About - Version
144173
$("#scriptio-version").textContent = scriptioVersion;
145174
// About - Backgroud image
146-
["version", "author", "issues", "submit"].forEach(id => {
147-
$(`#scriptio-about-${id}`).style.backgroundImage = `url("${pluginUri}/icons/${id}.svg")`;
175+
["version", "author", "issues", "submit"].forEach((id) => {
176+
$(`#scriptio-about-${id}`).style.backgroundImage =
177+
`url("${pluginUri}/icons/${id}.svg")`;
148178
});
149179
// Logo
150180
const logo = $(".logo");
151181
logo.src = pluginUri + "/icons/icon.svg";
152182
// Links
153-
view.querySelectorAll(".scriptio-link").forEach(link => {
183+
view.querySelectorAll(".scriptio-link").forEach((link) => {
154184
if (!link.getAttribute("title")) {
155185
link.setAttribute("title", link.getAttribute("data-scriptio-url"));
156186
}
157187
link.addEventListener("click", openURL);
158188
});
159189
setupEasterEggs(view);
160-
const container = $("setting-section.snippets > setting-panel > setting-list");
190+
const container = $(
191+
"setting-section.snippets > setting-panel > setting-list",
192+
);
161193
return container;
162194
}
163195
/** Function to handle `updateScript` event on settings view.
@@ -168,7 +200,9 @@ async function initScriptioSettings(view) {
168200
function scriptioSettingsUpdateScript(container, args) {
169201
const { path, meta, enabled } = args;
170202
const isDeleted = meta.name === " [已删除] ";
171-
const item = container.querySelector(`setting-item[${configDataAttr}="${path}"]`) || addItem(path, container);
203+
const item =
204+
container.querySelector(`setting-item[${configDataAttr}="${path}"]`) ||
205+
addItem(path, container);
172206
const itemName = item.querySelector("setting-text");
173207
const optionalVersion = meta.version ? ` (v${meta.version})` : "";
174208
itemName.textContent = meta.name + optionalVersion;
@@ -189,7 +223,9 @@ function scriptioSettingsUpdateScript(container, args) {
189223
homepage.removeAttribute("data-homepage-url");
190224
homepage.toggleAttribute("disabled", true);
191225
}
192-
const switch_ = item.querySelector(`setting-switch[${switchDataAttr}="${path}"]`);
226+
const switch_ = item.querySelector(
227+
`setting-switch[${switchDataAttr}="${path}"]`,
228+
);
193229
switch_.toggleAttribute("is-active", enabled);
194230
switch_.parentNode.classList.toggle("is-loading", false);
195231
if (isDeleted) {

renderer.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
/// <reference types="@qwqnt/types/renderer" />
2-
import { scriptio, scriptHelper, onVueComponentMount, onVueComponentUnmount } from "./modules/renderer/javascript.js";
2+
import {
3+
scriptio,
4+
scriptHelper,
5+
onVueComponentMount,
6+
onVueComponentUnmount,
7+
} from "./modules/renderer/javascript.js";
38

49
Object.defineProperty(window, "scriptio", {
510
value: scriptio,
611
writable: false,
712
enumerable: true,
8-
configurable: false
13+
configurable: false,
914
});
10-
Object.defineProperty(window, "scriptio_toolkit", { // Kept for compatibility - will be deprecated.
15+
Object.defineProperty(window, "scriptio_toolkit", {
16+
// Kept for compatibility - will be deprecated.
1117
value: scriptio,
1218
writable: false,
1319
enumerable: true,
14-
configurable: false
20+
configurable: false,
1521
});
1622

1723
scriptio_internal.onUpdateScript((event, args) => {
18-
scriptHelper(args.path, args.code, args.enabled, args.meta.description, args.meta["run-at"]);
24+
scriptHelper(
25+
args.path,
26+
args.code,
27+
args.enabled,
28+
args.meta.description,
29+
args.meta["run-at"],
30+
);
1931
});
2032
scriptio_internal.rendererReady();
2133

@@ -24,30 +36,28 @@ scriptio_internal.rendererReady();
2436
* @param {Element} view The settings view element.
2537
*/
2638
async function onSettingWindowCreated(view) {
27-
const { initScriptioSettings, scriptioSettingsUpdateScript } = await import("./modules/renderer/settings.js");
39+
const { initScriptioSettings, scriptioSettingsUpdateScript } =
40+
await import("./modules/renderer/settings.js");
2841
const container = await initScriptioSettings(view);
2942
scriptio_internal.onUpdateScript((event, args) => {
3043
scriptioSettingsUpdateScript(container, args);
3144
});
3245
scriptio_internal.rendererReady(); // // Call again to ensure the settings view gets the scripts data.
3346
}
3447

35-
if (typeof qwqnt !== "undefined") {
36-
// https://github.com/qwqnt-community/qwqnt-hako
37-
window.RendererEvents.onSettingsWindowCreated(async () => {
38-
const view = await window.PluginSettings.renderer.registerPluginSettings(qwqnt.framework.plugins.scriptio.meta.packageJson);
48+
// https://github.com/qwqnt-community/qwqnt-hako
49+
PluginSettings.renderer
50+
.registerPluginSettings(qwqnt.framework.plugins.scriptio.meta.packageJson)
51+
.then((view) => {
3952
if (view) {
4053
onSettingWindowCreated(view);
4154
}
4255
});
4356

44-
window.addEventListener("vue:component-mount", e => {
45-
onVueComponentMount(e.detail);
46-
});
47-
48-
window.addEventListener("vue:component-unmount", e => {
49-
onVueComponentUnmount(e.detail);
50-
});
51-
}
57+
window.addEventListener("vue:component-mount", (e) => {
58+
onVueComponentMount(e.detail);
59+
});
5260

53-
export { onSettingWindowCreated, onVueComponentMount, onVueComponentUnmount };
61+
window.addEventListener("vue:component-unmount", (e) => {
62+
onVueComponentUnmount(e.detail);
63+
});

0 commit comments

Comments
 (0)