-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
124 lines (110 loc) · 3.52 KB
/
Copy pathcontent.js
File metadata and controls
124 lines (110 loc) · 3.52 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
// Cross-browser API polyfill
if (typeof browser === "undefined") {
window.browser = chrome;
}
function detectIsMac() {
return (
navigator.userAgentData?.platform === "macOS" ||
navigator.userAgent.includes("Macintosh")
);
}
// Show toast when shortcut is blocked
function showToast(message) {
const id = "lc-toast-message";
const existing = document.getElementById(id);
if (existing) existing.remove();
const toast = document.createElement("div");
toast.id = id;
toast.textContent = message;
Object.assign(toast.style, {
position: "fixed",
top: "20px",
left: "50%",
transform: "translateX(-50%)",
backgroundColor: "#333",
color: "#fff",
padding: "10px 20px",
borderRadius: "8px",
zIndex: "2147483647", // maximum value
fontSize: "15px",
fontFamily: "system-ui, sans-serif",
boxShadow: "0 2px 10px rgba(0,0,0,0.4)",
transition: "opacity 0.4s ease-in-out",
opacity: "1",
pointerEvents: "none", // avoid blocking clicks
});
document.body.appendChild(toast);
setTimeout(() => {
toast.style.opacity = "0";
setTimeout(() => {
toast.remove();
}, 500);
}, 2500);
}
// Store latest state for shortcut blocking
window._lcProtectionState = { enabled: true, blockShortcut: true };
function applyProtection(enabled, mode, blockShortcut) {
// Update global state for shortcut handler
window._lcProtectionState.enabled = enabled;
window._lcProtectionState.blockShortcut = blockShortcut;
const observer = new MutationObserver(() => {
const btn = document.querySelector('[data-e2e-locator="console-submit-button"]');
if (btn) {
// Reset to default
btn.disabled = false;
btn.style.opacity = 1;
btn.style.position = "";
btn.style.top = "";
btn.style.left = "";
btn.title = "";
const span = btn.querySelector("span");
if (span) span.textContent = "Submit";
// Apply protection
if (enabled) {
if (mode === "disable") {
btn.disabled = true;
btn.style.opacity = 0.4;
btn.style.fontSize = "10px";
btn.title = "Submit disabled by extension";
if (span) span.textContent = "SUBMIT DISABLED BY EXTENSION";
} else if (mode === "move" || mode === "hide") {
btn.style.display = "none";
btn.title = "Hidden by extension";
}
}
observer.disconnect();
}
});
observer.observe(document.body, { childList: true, subtree: true });
}
// Only one global shortcut handler, always uses latest state
if (!window._lcBlockShortcutHandler) {
window._lcBlockShortcutHandler = function(e) {
const isMac = detectIsMac();
const isSubmitKey =
(e.ctrlKey || (isMac && e.metaKey)) && e.key === "Enter";
const { enabled, blockShortcut } = window._lcProtectionState || {};
if (enabled && blockShortcut && isSubmitKey) {
e.preventDefault();
e.stopPropagation();
const keyLabel = isMac ? "⌘+Enter" : "Ctrl+Enter";
showToast(`${keyLabel} submission disabled by extension`);
}
};
window.addEventListener("keydown", window._lcBlockShortcutHandler, true);
}
// Load from chrome.storage and apply logic
try {
chrome.storage.sync.get(
["enabled", "mode", "blockShortcut"],
({ enabled = true, mode = "disable", blockShortcut = true }) => {
try {
applyProtection(enabled, mode, blockShortcut);
} catch (err) {
console.error("Error in applyProtection:", err);
}
},
);
} catch (err) {
console.error("Error accessing chrome.storage:", err);
}