-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpanic-key.js
More file actions
128 lines (110 loc) · 5.22 KB
/
panic-key.js
File metadata and controls
128 lines (110 loc) · 5.22 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* panic-key.js
* This script provides a user-configurable panic key functionality for a website using IndexedDB.
* When activated, it redirects the user to a pre-configured destination.
* The user can configure up to 3 separate panic keys.
* The destination can be an external URL (e.g., https://google.com) or an internal page path (e.g., /dashboard/games.html).
* The key press must be a single key without any modifiers (Shift, Ctrl, Alt, etc.).
*
* This version uses IndexedDB for local storage, ensuring privacy and instantaneous redirection.
*/
// This message helps confirm that the script file itself is being loaded by the browser.
console.log("Debug: panic-key.js script has started.");
// --- IndexedDB Configuration ---
const DB_NAME = 'userLocalSettingsDB';
const STORE_NAME = 'panicKeyStore';
/**
* Opens the IndexedDB and creates the object store if needed.
* @returns {Promise<IDBDatabase>} A promise that resolves with the database object.
*/
function openDB() {
return new Promise((resolve, reject) => {
// This will open the latest version of the database.
const request = indexedDB.open(DB_NAME);
// This event handles the creation and updating of the database schema.
request.onupgradeneeded = event => {
const db = event.target.result;
// Create the 'panicKeyStore' object store if it doesn't already exist.
// We use 'id' as the keyPath (e.g., 'panicKey1', 'panicKey2').
if (!db.objectStoreNames.contains(STORE_NAME)) {
db.createObjectStore(STORE_NAME, { keyPath: 'id' });
console.log("Debug: IndexedDB object store 'panicKeyStore' created.");
}
};
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
/**
* Fetches all panic key settings from the IndexedDB.
* @param {IDBDatabase} db - The database instance.
* @returns {Promise<Array<object>>} A promise that resolves with an array of settings objects.
*/
function getSettings(db) {
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_NAME, 'readonly');
const store = transaction.objectStore(STORE_NAME);
// Request all objects from the store. This will return an array of all panic key configs.
const request = store.getAll();
request.onsuccess = () => resolve(request.result || []);
request.onerror = () => reject(request.error);
});
}
/**
* Attaches the 'keydown' event listener to the document with the user's specific settings.
* @param {Array<object>} settingsArray - An array of panic key settings objects { id, key, type, value }.
*/
function addPanicKeyListener(settingsArray) {
if (!settingsArray || settingsArray.length === 0) {
console.log("Debug: No panic key settings found to attach listener.");
return;
}
console.log("Debug: Attaching keydown listener to the document with these settings:", settingsArray);
document.addEventListener('keydown', (event) => {
const activeElement = document.activeElement;
// --- MODIFICATION START ---
// This check prevents the panic key from firing while a user is typing in any form field or the AI chat box.
if (activeElement) {
// Check for the custom AI Mode input box by its ID
if (activeElement.id === 'ai-input') {
return;
}
// Check for standard browser input elements by their tag name
const tagName = activeElement.tagName.toLowerCase();
if (['input', 'select', 'textarea'].includes(tagName)) {
return;
}
}
// --- MODIFICATION END ---
const noModifiersPressed = !event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey;
if (noModifiersPressed) {
// Find if the pressed key matches any of the configured panic keys.
const matchedSetting = settingsArray.find(setting => event.key.toLowerCase() === setting.key);
// If a match is found, we execute the panic action.
if (matchedSetting) {
console.log("SUCCESS: Panic key detected!", matchedSetting);
// This prevents the browser from performing the default action for the key press.
event.preventDefault();
// The 'url' property holds the destination URL.
window.location.href = matchedSetting.url;
}
}
});
}
// --- Main Execution Logic ---
document.addEventListener('DOMContentLoaded', async () => {
console.log("Debug: DOMContentLoaded event fired. The page is ready.");
try {
const db = await openDB();
const settings = await getSettings(db);
if (settings && settings.length > 0) {
console.log(`Debug: ${settings.length} panic key settings FOUND in IndexedDB.`, settings);
addPanicKeyListener(settings);
} else {
console.log("Debug: No panic key settings found in IndexedDB.");
}
db.close();
} catch (error) {
console.error("FATAL ERROR: Could not initialize panic key from IndexedDB:", error);
}
});