| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- (function () {
- 'use strict';
- var invoke = window.__TAURI__.core.invoke;
- var listen = window.__TAURI__.event.listen;
- // ── Rust → JS ──────────────────────────────
- listen('pet://state', function (e) {
- if (window.cfcPetAPI) window.cfcPetAPI.notifyState(e.payload);
- });
- listen('pet://bubble', function (e) {
- if (window.cfcPetAPI) window.cfcPetAPI.showBubble(e.payload.text, e.payload.durationMs);
- });
- listen('pet://config', function (e) {
- if (window.cfcPetAPI) window.cfcPetAPI.loadConfig(e.payload);
- });
- function applyCustomImage(url, dataUrl) {
- if (!window.cfcPetAPI) return;
- if (!dataUrl && url && /^https?:/i.test(url)) {
- // 远程图片:先取 data URL(规避 canvas 跨域污染)再生成动作;失败则仍显示原图
- try {
- invoke('data_url_from_url', { url: url }).then(function (d) {
- window.cfcPetAPI.setCustomImage(url, d);
- }).catch(function () {
- window.cfcPetAPI.setCustomImage(url, null);
- });
- } catch (_) {
- window.cfcPetAPI.setCustomImage(url, null);
- }
- } else {
- window.cfcPetAPI.setCustomImage(url, dataUrl);
- }
- }
- listen('pet://custom-image', function (e) {
- applyCustomImage(e.payload && e.payload.url, e.payload && e.payload.dataUrl);
- });
- // 启动时应用已保存的自定义形象
- try {
- invoke('get_settings').then(function (s) {
- window.__petHasToken = !!(s && s.hasToken);
- if (s && s.customMascotUrl) {
- applyCustomImage(s.customMascotUrl, null);
- }
- }).catch(function () {});
- } catch (_) { /* noop */ }
- // ── JS → Rust(替换 chrome.webview.hostObjects.sync.cfcHost)──
- window.cfcHost = {
- onInteract: function (action, dataJson) {
- try {
- invoke('on_interact', { action: action, data: dataJson });
- } catch (err) {
- console.log('[interaction]', action, dataJson);
- }
- },
- onDragStart: function () {
- try { invoke('drag_start'); } catch (e) { /* noop */ }
- },
- onDragEnd: function () {
- try { invoke('drag_end'); } catch (e) { /* noop */ }
- },
- startPerch: function () {
- try { invoke('start_perch'); } catch (e) { /* noop */ }
- },
- stopPerch: function () {
- try { invoke('stop_perch'); } catch (e) { /* noop */ }
- }
- };
- })();
|