-
Notifications
You must be signed in to change notification settings - Fork 128
/
vscode-script.js
79 lines (69 loc) · 2.94 KB
/
vscode-script.js
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
document.addEventListener('DOMContentLoaded', function() {
const checkElement = setInterval(() => {
const commandDialog = document.querySelector(".quick-input-widget");
if (commandDialog) {
// Apply the blur effect immediately if the command dialog is visible
if (commandDialog.style.display !== "none") {
runMyScript();
}
// Create an DOM observer to 'listen' for changes in element's attribute.
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
if (commandDialog.style.display === 'none') {
handleEscape();
} else {
// If the .quick-input-widget element (command palette) is in the DOM
// but no inline style display: none, show the backdrop blur.
runMyScript();
}
}
});
});
observer.observe(commandDialog, { attributes: true });
// Clear the interval once the observer is set
clearInterval(checkElement);
} else {
console.log("Command dialog not found yet. Retrying...");
}
}, 500); // Check every 500ms
// Execute when command palette was launched.
document.addEventListener('keydown', function(event) {
if ((event.metaKey || event.ctrlKey) && event.key === 'p') {
event.preventDefault();
runMyScript();
} else if (event.key === 'Escape' || event.key === 'Esc') {
event.preventDefault();
handleEscape();
}
});
// Ensure the escape key event listener is at the document level
document.addEventListener('keydown', function(event) {
if (event.key === 'Escape' || event.key === 'Esc') {
handleEscape();
}
}, true);
function runMyScript() {
const targetDiv = document.querySelector(".monaco-workbench");
// Remove existing element if it already exists
const existingElement = document.getElementById("command-blur");
if (existingElement) {
existingElement.remove();
}
// Create and configure the new element
const newElement = document.createElement("div");
newElement.setAttribute('id', 'command-blur');
newElement.addEventListener('click', function() {
newElement.remove();
});
// Append the new element as a child of the targetDiv
targetDiv.appendChild(newElement);
}
// Remove the backdrop blur from the DOM when esc key is pressed.
function handleEscape() {
const element = document.getElementById("command-blur");
if (element) {
element.click();
}
}
});