Repository navigation
Expand file tree
/
Copy pathadmin-shortcuts.js
More file actions
363 lines (320 loc) · 10.9 KB
/
Copy pathadmin-shortcuts.js
File metadata and controls
363 lines (320 loc) · 10.9 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// admin-shortcuts.js — WordPress Admin Shortcuts panel
(async function main() {
const { adminShortcuts } = await chrome.storage.sync.get("adminShortcuts");
if (!adminShortcuts) return;
const {
enabled,
panelPosition = "top-left",
domains = [],
shortcuts = [
{ name: "Dashboard", url: "/wp-admin/", icon: "🏠" },
{ name: "Pages", url: "/wp-admin/edit.php?post_type=page", icon: "📄" },
{ name: "Posts", url: "/wp-admin/edit.php", icon: "📝" },
{ name: "Plugins", url: "/wp-admin/plugins.php", icon: "🔌" },
{ name: "Themes", url: "/wp-admin/themes.php", icon: "🎨" },
{ name: "Users", url: "/wp-admin/users.php", icon: "👥" },
{ name: "Settings", url: "/wp-admin/options-general.php", icon: "⚙️" },
{
name: "Debug Log",
url: "/wp-admin/tools.php?page=debug-log",
icon: "🐛",
},
],
} = adminShortcuts;
if (!enabled) return;
// Check if current domain matches allowed domains
function isDomainAllowed() {
if (!domains || domains.length === 0) return true; // Show on all domains if none specified
const currentHost = window.location.hostname;
return domains.some((domain) => {
// Handle wildcard domains like *.example.com
if (domain.startsWith("*.")) {
const baseDomain = domain.substring(2);
return (
currentHost === baseDomain || currentHost.endsWith("." + baseDomain)
);
}
// Handle exact domain matches
return currentHost === domain;
});
}
// Check if current site is WordPress
function isWordPressSite() {
const indicators = [
document.querySelector('meta[name="generator"][content*="WordPress"]'),
document.querySelector('link[href*="wp-content"]'),
document.querySelector('script[src*="wp-content"]'),
window.location.pathname.includes("/wp-"),
window.location.pathname.includes("/wp-admin"),
window.location.pathname.includes("/wp-json"),
document.body && document.body.classList.toString().includes("wp-"),
document.documentElement &&
document.documentElement.classList.toString().includes("wp-"),
document.querySelector("#wpadminbar"),
document.querySelector(".wp-block"),
document.querySelector("[data-wp-theme]"),
typeof window.wp !== "undefined",
typeof window.wpApiSettings !== "undefined",
];
return indicators.some((indicator) => !!indicator);
}
// Create admin shortcuts panel
function createAdminPanel() {
// Remove existing panel if it exists
const existingPanel = document.getElementById("devstack-admin-panel");
if (existingPanel) {
existingPanel.remove();
}
const panel = document.createElement("div");
panel.id = "devstack-admin-panel";
panel.style.cssText = `
position: fixed;
top: 20px;
left: 20px;
background: #1a1a1a;
color: #fff;
padding: 15px;
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0,0,0,0.3);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
font-size: 13px;
z-index: 999999;
min-width: 200px;
max-width: 300px;
border: 1px solid #333;
`;
// Header
const header = document.createElement("div");
header.style.cssText = `
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
padding-bottom: 8px;
border-bottom: 1px solid #333;
`;
const title = document.createElement("h3");
title.textContent = "⚡ Admin Shortcuts";
title.style.cssText = "margin: 0; font-size: 14px; font-weight: 600;";
const closeBtn = document.createElement("button");
closeBtn.textContent = "×";
closeBtn.style.cssText = `
background: none;
border: none;
color: #fff;
font-size: 18px;
cursor: pointer;
padding: 0;
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
`;
closeBtn.onclick = () => panel.remove();
header.appendChild(title);
header.appendChild(closeBtn);
// Shortcuts list
const shortcutsDiv = document.createElement("div");
shortcutsDiv.style.cssText =
"display: flex; flex-direction: column; gap: 4px;";
shortcuts.forEach((shortcut, index) => {
const shortcutButton = document.createElement("button");
shortcutButton.innerHTML = `${shortcut.icon} ${shortcut.name}`;
shortcutButton.style.cssText = `
background: #2c3e50;
border: none;
color: white;
padding: 8px 12px;
border-radius: 4px;
font-size: 12px;
cursor: pointer;
font-weight: 500;
text-align: left;
display: flex;
align-items: center;
gap: 8px;
transition: all 0.2s;
width: 100%;
`;
shortcutButton.addEventListener("mouseenter", () => {
shortcutButton.style.background = "#34495e";
shortcutButton.style.transform = "translateX(4px)";
});
shortcutButton.addEventListener("mouseleave", () => {
shortcutButton.style.background = "#2c3e50";
shortcutButton.style.transform = "translateX(0)";
});
shortcutButton.onclick = () => {
const adminUrl = new URL(shortcut.url, window.location.origin);
window.open(adminUrl.toString(), "_blank");
};
shortcutsDiv.appendChild(shortcutButton);
});
// Assemble panel
panel.appendChild(header);
panel.appendChild(shortcutsDiv);
document.body.appendChild(panel);
// Click outside to close functionality
const handleClickOutside = (event) => {
if (
panel &&
panel.parentNode &&
!panel.contains(event.target) &&
!document
.getElementById("devstack-admin-trigger")
.contains(event.target)
) {
panel.remove();
document.removeEventListener("click", handleClickOutside);
}
};
// Add click outside listener after a small delay to prevent immediate closing
setTimeout(() => {
document.addEventListener("click", handleClickOutside);
}, 100);
// Auto-hide after 15 seconds if no interaction
let hideTimeout = setTimeout(() => {
if (panel.parentNode) {
panel.style.opacity = "0.7";
}
}, 15000);
panel.addEventListener("mouseenter", () => {
clearTimeout(hideTimeout);
panel.style.opacity = "1";
});
panel.addEventListener("mouseleave", () => {
hideTimeout = setTimeout(() => {
if (panel.parentNode) {
panel.style.opacity = "0.7";
}
}, 5000);
});
}
// Create floating trigger button
function createTriggerButton() {
// Remove existing button if it exists
const existingButton = document.getElementById("devstack-admin-trigger");
if (existingButton) {
existingButton.remove();
}
// Get position styles based on panelPosition setting
const getPositionStyles = (position) => {
const baseStyles = `
position: fixed;
width: 50px;
height: 50px;
border-radius: 50%;
background: #e67e22;
color: white;
border: none;
font-size: 20px;
cursor: pointer;
z-index: 999998;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
transition: all 0.2s ease;
display: flex;
align-items: center;
justify-content: center;
`;
switch (position) {
case "top-left":
return baseStyles + "top: 20px; left: 20px;";
case "top-right":
return baseStyles + "top: 20px; right: 20px;";
case "bottom-left":
return baseStyles + "bottom: 20px; left: 20px;";
case "bottom-right":
return baseStyles + "bottom: 20px; right: 20px;";
default:
return baseStyles + "top: 20px; left: 20px;";
}
};
const triggerButton = document.createElement("button");
triggerButton.id = "devstack-admin-trigger";
triggerButton.innerHTML = "⚡";
triggerButton.title = "DevStack Admin Shortcuts (Ctrl+Shift+A)";
triggerButton.style.cssText = getPositionStyles(panelPosition);
triggerButton.addEventListener("mouseenter", () => {
triggerButton.style.transform = "scale(1.1)";
triggerButton.style.background = "#d35400";
});
triggerButton.addEventListener("mouseleave", () => {
triggerButton.style.transform = "scale(1)";
triggerButton.style.background = "#e67e22";
});
triggerButton.onclick = () => {
const existingPanel = document.getElementById("devstack-admin-panel");
if (existingPanel) {
existingPanel.remove();
} else {
createAdminPanel();
}
};
document.body.appendChild(triggerButton);
}
// Keyboard shortcut handler
function handleKeyboardShortcut(e) {
// Ctrl+Shift+A to toggle admin panel
if (e.ctrlKey && e.shiftKey && e.key.toLowerCase() === "a") {
e.preventDefault();
e.stopPropagation();
const existingPanel = document.getElementById("devstack-admin-panel");
if (existingPanel) {
existingPanel.remove();
} else {
createAdminPanel();
}
}
}
// Initialize
function init() {
console.log("⚡ DevStack Admin Shortcuts initialized");
// Check domain filtering first
if (!isDomainAllowed()) {
console.log("⚡ Domain not in allowed list, admin shortcuts disabled");
return;
}
// Only show on WordPress sites
if (!isWordPressSite()) {
console.log("⚡ Not a WordPress site, admin shortcuts disabled");
return;
}
// Create floating trigger button
createTriggerButton();
// Add keyboard shortcut
document.addEventListener("keydown", handleKeyboardShortcut);
// Listen for storage changes
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === "sync" && changes.adminShortcuts) {
const newSettings = changes.adminShortcuts.newValue;
if (newSettings) {
// Update settings without reloading
if (newSettings.enabled) {
// Recreate trigger button with new position if needed
createTriggerButton();
} else {
// Remove trigger button and panel if disabled
const existingButton = document.getElementById(
"devstack-admin-trigger"
);
const existingPanel = document.getElementById(
"devstack-admin-panel"
);
if (existingButton) existingButton.remove();
if (existingPanel) existingPanel.remove();
}
}
}
});
}
// Only run on page load, not on every navigation
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
})().catch((error) => {
console.error("Admin Shortcuts: Error in main function:", error);
});