Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/injection/dmm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ import { type GameWindowConfig } from '../models/configs/GameWindowConfig';
});
}

// 設定値をキャッシュする変数
let cachedAlertBeforeClose = true;

function startListeningMessage() {
chrome.runtime.onMessage.addListener(async (msg) => {
if (msg.__action__ === "/injected/dmm/ocr" && msg.url) {
Expand All @@ -144,6 +147,10 @@ import { type GameWindowConfig } from '../models/configs/GameWindowConfig';
if (msg.__action__ === "/injected/dmm/retouch") {
resize();
}
// 設定更新メッセージを受信して、キャッシュを更新
if (msg.__action__ === "/injected/dmm/config:update" && typeof msg.alertBeforeClose === "boolean") {
cachedAlertBeforeClose = msg.alertBeforeClose;
}
});
}

Expand All @@ -160,24 +167,29 @@ import { type GameWindowConfig } from '../models/configs/GameWindowConfig';

/**
* ウィンドウを閉じようとしたときに確認ダイアログを表示
* キャッシュされた設定値をチェックして、有効な場合のみダイアログを表示する
*/
function setupBeforeUnloadHandler() {
window.addEventListener("beforeunload", (event) => {
// 標準的な確認メッセージを表示
event.preventDefault();
// Chrome では returnValue に値を設定することで確認ダイアログが表示される
event.returnValue = "";
// キャッシュされた設定値を確認(同期的に動作)
if (cachedAlertBeforeClose) {
// 標準的な確認メッセージを表示
event.preventDefault();
// Chrome では returnValue に値を設定することで確認ダイアログが表示される
event.returnValue = "";
}
});
}

(async function __main__() {
resize();
setInterval(track, 10 * 1000);
const configs = await fetchNecessaryConfig();
cachedAlertBeforeClose = configs.game.alertBeforeClose ?? true;
startListeningMessage();
InAppActionButtons.create(configs.game);
const shouldAlertBeforeClose = configs.game.alertBeforeClose ?? true;
if (shouldAlertBeforeClose) setupBeforeUnloadHandler();
// beforeunload ハンドラーは常に設定し、イベント発火時にキャッシュ値を確認する
setupBeforeUnloadHandler();
})();

})();
15 changes: 15 additions & 0 deletions src/page/components/options/FrameSettingView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ export function FrameSettingView({
const next = event.target.checked;
await config.update({ alertBeforeClose: next });
setAlertBeforeClose(next);
// 開いているゲーム窓に設定更新を通知
try {
const win = await launcher.find();
const tabId = win?.tabs?.[0]?.id;
if (tabId) {
await chrome.tabs.sendMessage(tabId, {
__action__: "/injected/dmm/config:update",
alertBeforeClose: next
});
}
} catch (error) {
// タブが閉じられている等でメッセージ送信に失敗した場合は無視
// 次回ゲーム窓を開いた際に正しい設定が適用される
console.debug("Failed to send config update message:", error);
}
}}
className="w-4 h-4"
/>
Expand Down